Jump to content

Draw a cuboid: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|POV-Ray}}: Add Processing section)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 240:
+-----+-----+-----+-----+-----+-----+
</pre>
 
=={{header|Brlcad}}==
In brlcad, we use the rpp (rectangular parallelepiped) primitive to create the cuboid. This defines the cuboid area using the parameters xmin,xmax,ymin,ymax,zmin,zmax
 
<lang brlcad>opendb cuboid.g y # Create a database to hold our shapes
units cm # Set the unit of measure
in cuboid.s rpp 0 2 0 3 0 4 # Create a 2 x 3 x 4 cuboid named cuboid.s</lang>
 
=={{header|BBC BASIC}}==
Line 287 ⟶ 280:
\\\\\\::/
\\\\\\/</pre>
 
=={{header|Brlcad}}==
In brlcad, we use the rpp (rectangular parallelepiped) primitive to create the cuboid. This defines the cuboid area using the parameters xmin,xmax,ymin,ymax,zmin,zmax
 
<lang brlcad>opendb cuboid.g y # Create a database to hold our shapes
units cm # Set the unit of measure
in cuboid.s rpp 0 2 0 3 0 4 # Create a 2 x 3 x 4 cuboid named cuboid.s</lang>
 
=={{header|C}}==
Line 460:
</pre>
 
=={{header|C++ sharp|C#}}==
 
This code needs the BGI for Windows available at [http://www.cs.colorado.edu/~main/cs1300/doc/bgi/bgi.html Colorado State University].
 
<lang cpp>
#include<graphics.h>
#include<iostream>
 
int main()
{
int k;
initwindow(1500,810,"Rosetta Cuboid");
do{
std::cout<<"Enter ratio of sides ( 0 or -ve to exit) : ";
std::cin>>k;
if(k>0){
bar3d(100, 100, 100 + 2*k, 100 + 4*k, 3*k, 1);
}
}while(k>0);
return 0;
}
 
</lang>
[[Image:Box.jpg]]
 
=={{header|C#}}==
{{trans|Java}}
<lang csharp>using System;
Line 606 ⟶ 578:
}
}</lang>
 
=={{header|C++}}==
 
This code needs the BGI for Windows available at [http://www.cs.colorado.edu/~main/cs1300/doc/bgi/bgi.html Colorado State University].
 
<lang cpp>
#include<graphics.h>
#include<iostream>
 
int main()
{
int k;
initwindow(1500,810,"Rosetta Cuboid");
do{
std::cout<<"Enter ratio of sides ( 0 or -ve to exit) : ";
std::cin>>k;
if(k>0){
bar3d(100, 100, 100 + 2*k, 100 + 4*k, 3*k, 1);
}
}while(k>0);
return 0;
}
 
</lang>
[[Image:Box.jpg]]
 
=={{header|Clojure}}==
Line 907:
+-----------------------------------------------------+
</pre>
 
=={{header|Haskell}}==
<lang haskell>import Graphics.Rendering.OpenGL
Line 1,811 ⟶ 1,812:
| |/
+------------------------------------------------+ </pre>
 
=={{header|Perl}}==
{{trans|Go}}
Line 1,937 ⟶ 1,939:
| |/
+--+</pre>
 
=={{header|Perl 6}}==
{{works with|moar|2015-11-27}}
<lang perl6>sub braille-graphics (%a) {
my ($ylo, $yhi, $xlo, $xhi);
for %a.keys -> $y {
$ylo min= +$y; $yhi max= +$y;
for %a{$y}.keys -> $x {
$xlo min= +$x; $xhi max= +$x;
}
}
 
for $ylo, $ylo + 4 ...^ * > $yhi -> \y {
for $xlo, $xlo + 2 ...^ * > $xhi -> \x {
my $cell = 0x2800;
$cell += 1 if %a{y + 0}{x + 0};
$cell += 2 if %a{y + 1}{x + 0};
$cell += 4 if %a{y + 2}{x + 0};
$cell += 8 if %a{y + 0}{x + 1};
$cell += 16 if %a{y + 1}{x + 1};
$cell += 32 if %a{y + 2}{x + 1};
$cell += 64 if %a{y + 3}{x + 0};
$cell += 128 if %a{y + 3}{x + 1};
print chr($cell);
}
print "\n";
}
}
 
sub cuboid ( [$x, $y, $z] ) {
my \x = $x * 4;
my \y = $y * 4;
my \z = $z * 2;
my %t;
sub horz ($X, $Y) { %t{$Y }{$X + $_} = True for 0 .. x }
sub vert ($X, $Y) { %t{$Y + $_}{$X } = True for 0 .. y }
sub diag ($X, $Y) { %t{$Y - $_}{$X + $_} = True for 0 .. z }
horz(0, z); horz(z, 0); horz( 0, z+y);
vert(0, z); vert(x, z); vert(z+x, 0);
diag(0, z); diag(x, z); diag( x, z+y);
say "[$x, $y, $z]";
braille-graphics %t;
}
cuboid $_ for [2,3,4], [3,4,2], [4,2,3], [1,1,1], [8,1,1], [1,8,1], [1,1,8];</lang>
 
{{out}}
[[File:Cuboid_Perl_6.png]]
 
=={{header|Phix}}==
Line 2,495 ⟶ 2,447:
 
[[FILE:PureData-cuboid.png‎]]
 
=={{header|PureBasic}}==
Using generic PureBasic 2D-library.
<lang PureBasic>Procedure Draw_a_Cuboid(Window, X,Y,Z)
w=WindowWidth(Window)
h=WindowHeight(Window)
diag.f=1.9
If Not (w And h): ProcedureReturn: EndIf
xscale.f = w/(x+z/diag)*0.98
yscale.f = h/(y+z/diag)*0.98
If xscale<yscale
Scale.f = xscale
Else
Scale = yscale
EndIf
x*Scale: Y*Scale: Z*Scale
CreateImage(0,w,h)
If StartDrawing(ImageOutput(0))
c= RGB(250, 40, 5)
;- Calculate the cones in the Cuboid
xk = w/50 : yk = h/50
x0 = Z/2 + xk : y0 = yk
x1 = x0 + X : y1 = y0
x2 = xk : y2 = y0 + Z/2
x3 = x2 + X : y3 = y2
x4 = x2 : y4 = y2 + Y
x5 = x4 + X : y5 = y4
x6 = x5 + Z/2 : y6 = y5 - Z/2
;- Draw it
LineXY(x0,y0,x1,y1,c)
LineXY(x0,y0,x2,y2,c)
LineXY(x2,y2,x3,y3,c)
LineXY(x1,y1,x3,y3,c)
LineXY(x2,y2,x4,y4,c)
LineXY(x4,y4,x5,y5,c)
LineXY(x5,y5,x4,y4,c)
LineXY(x5,y5,x6,y6,c)
LineXY(x5,y5,x3,y3,c)
LineXY(x6,y6,x1,y1,c)
;- Fill the areas
FillArea(x,y,-1,RGB(255, 0, 0))
FillArea(x,y-z/2,-1,RGB(0, 0, 255))
FillArea(x+z/2,y,-1,RGB(0, 255, 0))
StopDrawing()
EndIf
;- Update the graphic
ImageGadget(0,0,0,w,h,ImageID(0))
EndProcedure
 
#WFlags = #PB_Window_SystemMenu|#PB_Window_SizeGadget
#title = "PureBasic Cuboid"
MyWin = OpenWindow(#PB_Any, 0, 0, 200, 250, #title, #WFlags)
 
Repeat
WEvent = WaitWindowEvent()
If WEvent = #PB_Event_SizeWindow
Draw_a_Cuboid(MyWin, 2, 3, 4)
EndIf
Until WEvent = #PB_Event_CloseWindow
 
;- Save the image?
UsePNGImageEncoder()
respons = MessageRequester("Question","Save the image?",#PB_MessageRequester_YesNo)
If respons=#PB_MessageRequester_Yes
SaveImage(0, SaveFileRequester("","","",0),#PB_ImagePlugin_PNG,9)
EndIf</lang>
[[Image:PB_Cuboid.png]]
 
=={{header|Python}}==
Line 2,834 ⟶ 2,856:
demoStep()
</lang>
 
=={{header|PureBasic}}==
Using generic PureBasic 2D-library.
<lang PureBasic>Procedure Draw_a_Cuboid(Window, X,Y,Z)
w=WindowWidth(Window)
h=WindowHeight(Window)
diag.f=1.9
If Not (w And h): ProcedureReturn: EndIf
xscale.f = w/(x+z/diag)*0.98
yscale.f = h/(y+z/diag)*0.98
If xscale<yscale
Scale.f = xscale
Else
Scale = yscale
EndIf
x*Scale: Y*Scale: Z*Scale
CreateImage(0,w,h)
If StartDrawing(ImageOutput(0))
c= RGB(250, 40, 5)
;- Calculate the cones in the Cuboid
xk = w/50 : yk = h/50
x0 = Z/2 + xk : y0 = yk
x1 = x0 + X : y1 = y0
x2 = xk : y2 = y0 + Z/2
x3 = x2 + X : y3 = y2
x4 = x2 : y4 = y2 + Y
x5 = x4 + X : y5 = y4
x6 = x5 + Z/2 : y6 = y5 - Z/2
;- Draw it
LineXY(x0,y0,x1,y1,c)
LineXY(x0,y0,x2,y2,c)
LineXY(x2,y2,x3,y3,c)
LineXY(x1,y1,x3,y3,c)
LineXY(x2,y2,x4,y4,c)
LineXY(x4,y4,x5,y5,c)
LineXY(x5,y5,x4,y4,c)
LineXY(x5,y5,x6,y6,c)
LineXY(x5,y5,x3,y3,c)
LineXY(x6,y6,x1,y1,c)
;- Fill the areas
FillArea(x,y,-1,RGB(255, 0, 0))
FillArea(x,y-z/2,-1,RGB(0, 0, 255))
FillArea(x+z/2,y,-1,RGB(0, 255, 0))
StopDrawing()
EndIf
;- Update the graphic
ImageGadget(0,0,0,w,h,ImageID(0))
EndProcedure
 
#WFlags = #PB_Window_SystemMenu|#PB_Window_SizeGadget
#title = "PureBasic Cuboid"
MyWin = OpenWindow(#PB_Any, 0, 0, 200, 250, #title, #WFlags)
 
Repeat
WEvent = WaitWindowEvent()
If WEvent = #PB_Event_SizeWindow
Draw_a_Cuboid(MyWin, 2, 3, 4)
EndIf
Until WEvent = #PB_Event_CloseWindow
 
;- Save the image?
UsePNGImageEncoder()
respons = MessageRequester("Question","Save the image?",#PB_MessageRequester_YesNo)
If respons=#PB_MessageRequester_Yes
SaveImage(0, SaveFileRequester("","","",0),#PB_ImagePlugin_PNG,9)
EndIf</lang>
[[Image:PB_Cuboid.png]]
 
=={{header|Racket}}==
Line 2,975 ⟶ 2,927:
 
[[Image:Racket_cuboid.png]]
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|moar|2015-11-27}}
<lang perl6>sub braille-graphics (%a) {
my ($ylo, $yhi, $xlo, $xhi);
for %a.keys -> $y {
$ylo min= +$y; $yhi max= +$y;
for %a{$y}.keys -> $x {
$xlo min= +$x; $xhi max= +$x;
}
}
 
for $ylo, $ylo + 4 ...^ * > $yhi -> \y {
for $xlo, $xlo + 2 ...^ * > $xhi -> \x {
my $cell = 0x2800;
$cell += 1 if %a{y + 0}{x + 0};
$cell += 2 if %a{y + 1}{x + 0};
$cell += 4 if %a{y + 2}{x + 0};
$cell += 8 if %a{y + 0}{x + 1};
$cell += 16 if %a{y + 1}{x + 1};
$cell += 32 if %a{y + 2}{x + 1};
$cell += 64 if %a{y + 3}{x + 0};
$cell += 128 if %a{y + 3}{x + 1};
print chr($cell);
}
print "\n";
}
}
 
sub cuboid ( [$x, $y, $z] ) {
my \x = $x * 4;
my \y = $y * 4;
my \z = $z * 2;
my %t;
sub horz ($X, $Y) { %t{$Y }{$X + $_} = True for 0 .. x }
sub vert ($X, $Y) { %t{$Y + $_}{$X } = True for 0 .. y }
sub diag ($X, $Y) { %t{$Y - $_}{$X + $_} = True for 0 .. z }
horz(0, z); horz(z, 0); horz( 0, z+y);
vert(0, z); vert(x, z); vert(z+x, 0);
diag(0, z); diag(x, z); diag( x, z+y);
say "[$x, $y, $z]";
braille-graphics %t;
}
cuboid $_ for [2,3,4], [3,4,2], [4,2,3], [1,1,1], [8,1,1], [1,8,1], [1,1,8];</lang>
 
{{out}}
[[File:Cuboid_Perl_6.png]]
 
=={{header|Retro}}==
Line 3,305 ⟶ 3,308:
})
}</lang>
 
=={{header|Sidef}}==
{{trans|Ruby}}
10,349

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.