Draw a cuboid: Difference between revisions

m
→‎{{header|POV-Ray}}: Changed syntax highlight language to "pov" to make it work
m (→‎{{header|POV-Ray}}: Changed syntax highlight language to "pov" to make it work)
 
(17 intermediate revisions by 10 users not shown)
Line 24:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F cline(n, x, y, cde)
print(String(cde[0]).rjust(n + 1)‘’
(cde[1] * (9 * x - 1))‘’
Line 44:
cuboid(2, 3, 4)
cuboid(1, 1, 1)
cuboid(6, 2, 1)</langsyntaxhighlight>
 
{{out}}
Line 90:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC DrawCuboid(CARD x,y BYTE w,h,d)
BYTE wsize=[10],hsize=[10],dsize=[5]
BYTE i
Line 131:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Draw_a_cuboid.png Screenshot from Atari 8-bit computer]
Line 137:
=={{header|Ada}}==
ASCII-Art output, one width unit is two characters long ('--').
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Main is
Line 191:
begin
Print_Cuboid (2, 3, 4);
end Main;</langsyntaxhighlight>
{{Out}}
<pre> +----+
Line 206:
=={{header|Arturo}}==
{{trans|PicoLisp}}
<langsyntaxhighlight lang="rebol">cline: function [n,a,b,cde][
print (pad to :string first cde n+1) ++
(repeat to :string cde\1 dec 9*a)++
Line 225:
cuboid 2 3 4
cuboid 1 1 1
cuboid 6 2 1</langsyntaxhighlight>
 
{{out}}
Line 272:
=={{header|AutoHotkey}}==
{{libheader|GDIP}}Some portions of code from [http://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/ Gdip examples].
<langsyntaxhighlight AutoHotkeylang="autohotkey">Angle := 45
C := 0.01745329252
W := 200
Line 319:
Exit:
Gdip_Shutdown(pToken)
ExitApp</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DRAW_A_CUBOID.AWK [-v x=?] [-v y=?] [-v z=?]
# example: GAWK -f DRAW_A_CUBOID.AWK -v x=12 -v y=4 -v z=6
Line 382:
if (z+0 < 2) { z = 3 } # front
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 425:
=={{header|BBC BASIC}}==
Uses BBC BASIC's native parallelogram plot.
<langsyntaxhighlight lang="bbcbasic"> ORIGIN 100, 100
PROCcuboid(200, 300, 400)
END
Line 435:
GCOL 4 : PLOT 117, x + z * 0.4, z * 0.4
ENDPROC
</syntaxhighlight>
</lang>
{{Out}}
[[File:Cuboid_BBC.gif]]
Line 443:
Given a width, height, and depth, this produces an approximate isometric representation of the shape using ASCII art.
 
<langsyntaxhighlight lang="befunge">" :htdiW">:#,_>&>00p" :thgieH">:#,_>&>:10p0" :htpeD">:#,_$>&>:20p55+,+:1`*:vv
v\-*`0:-g01\++*`\0:-\-1g01:\-*`0:-g02\+*`\0:-\-1g02<:::::<\g3`\g01:\1\+55\1-1_v
>":"\1\:20g\`!3g:30p\00g2*\::20g\`\20g1-\`+1+3g\1\30g\:20g-::0\`\2*1+*-\48*\:^v
/\_ @_\#!:!#$>#$_\#!:,#-\#1 <+1\<*84g02"_"+1*2g00+551$<</langsyntaxhighlight>
 
{{out}}
Line 466:
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
 
<langsyntaxhighlight 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</langsyntaxhighlight>
 
=={{header|C}}==
Code works fine but only '.' and ':' characters show up on the cuboid.
<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
#include <stdlib.h>
Line 608:
 
return 0;
}</langsyntaxhighlight>
 
Output :
Line 644:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Drawing;
using System.Drawing.Drawing2D;
Line 759:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
Line 765:
This code needs the BGI for Windows available at [http://www.cs.colorado.edu/~main/cs1300/doc/bgi/bgi.html Colorado State University].
 
<langsyntaxhighlight lang="cpp">
#include<graphics.h>
#include<iostream>
Line 786:
}
 
</syntaxhighlight>
</lang>
[[Image:Box.jpg]]
 
=={{header|Clojure}}==
{{libheader|quil}}
<langsyntaxhighlight lang="clojure">
(use 'quil.core)
 
Line 814:
:draw draw
:renderer :opengl)
</syntaxhighlight>
</lang>
 
{{out}}
Line 821:
=={{header|D}}==
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.stdio, std.array;
 
void printCuboid(in int dx, in int dy, in int dz) {
Line 847:
printCuboid(1, 1, 1);
printCuboid(6, 2, 1);
}</langsyntaxhighlight>
{{Out}}
<pre> +-----------------+
Line 890:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Draw_a_cuboid;
 
Line 942:
 
readln;
end.</langsyntaxhighlight>
 
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=jZRNj9owEIbv/hWv2BsoEUkIUKk5rNRjT5V6inKgxFALsFdJVGJ+fTX+2Dhp2C5CTBg/8854PI5UNS+rskKBEiWiBFFK3+r9T+KfQ793h3hAB7BnK8brc5iKFghIsTF2g8zYzGnk2Bq7xd7YPXbG7pA74dzFb13czunsTboXsLdGHdEeD1eOE2LEDMBJNRAokKBTuHIJ6TpAi2699uuZcwIWE1VZV1gWOLkFIxmz2KZqVHfoOA7y3NOP9jlbIXsUZMwauY6qJddRte+uVkg9UNpReqD0Z+onVV9r4p2PwJl550ChQI+lrSnCA0tTscN0EJuGgg8fsjLRQciAo4C2mB6U9QTLqkFNY2UiPEa9NbusywqZbXPdHO6+tccrPzT0cFN/OFKk9NzxvsPitWnUHReuW2qVPZuFQVHgC33MH2pmlHymteIUlvwVt2E6bnMN9upiPCykRKc0n9PfkiGn8fhzuolgJOtS9OT0RBpkFj1WLgcAfg2E0v8KJU+Fhj1Q5XLcrnp0iz7a1szOZF1KUUE14zqtewijEU98tweBcc00trNQOlFKp1A6ozQLTZTM+PUJVsjXlJvsCLgKySmfBdIJEE/fJzTCrXhwZMy+wJKcudfLJscmZ3QJmJI038x288L1rwun62pH/+fbwqm6wLU9RTMLU/ibust/8Og5/52fugkfJVg/5X+I8+9pgOPNns12Ysb+Ag== Run it]
<syntaxhighlight>
node[][] = [ [ -1 -2 -2 ] [ -1 -2 1 ] [ -1 2 -2 ] [ -1 2 1 ] [ 1 -2 -2 ] [ 1 -2 1 ] [ 1 2 -2 ] [ 1 2 1 ] ]
edge[][] = [ [ 1 2 ] [ 2 4 ] [ 4 3 ] [ 3 1 ] [ 5 6 ] [ 6 8 ] [ 8 7 ] [ 7 5 ] [ 1 5 ] [ 2 6 ] [ 3 7 ] [ 4 8 ] ]
#
proc scale f . .
for i = 1 to len node[][]
for d = 1 to 3
node[i][d] *= f
.
.
.
proc rotate angx angy . .
sinx = sin angx
cosx = cos angx
siny = sin angy
cosy = cos angy
for i = 1 to len node[][]
x = node[i][1]
z = node[i][3]
node[i][1] = x * cosx - z * sinx
y = node[i][2]
z = z * cosx + x * sinx
node[i][2] = y * cosy - z * siny
node[i][3] = z * cosy + y * siny
.
.
len nd[] 3
proc draw . .
clear
move 2 2
text "Arrow keys to rotate"
m = 99999
mi = -1
for i = 1 to len node[][]
if node[i][3] < m
m = node[i][3]
mi = i
.
.
ix = 1
for i = 1 to len edge[][]
if edge[i][1] = mi
nd[ix] = edge[i][2]
ix += 1
elif edge[i][2] = mi
nd[ix] = edge[i][1]
ix += 1
.
.
for ni = 1 to len nd[]
for i = 1 to len edge[][]
if edge[i][1] = nd[ni] or edge[i][2] = nd[ni]
x1 = node[edge[i][1]][1]
y1 = node[edge[i][1]][2]
x2 = node[edge[i][2]][1]
y2 = node[edge[i][2]][2]
move x1 + 50 y1 + 50
line x2 + 50 y2 + 50
.
.
.
.
textsize 3
scale 15
rotate 45 45
draw
on key
if keybkey = "ArrowUp"
rotate 0 1
elif keybkey = "ArrowDown"
rotate 0 -1
elif keybkey = "ArrowLeft"
rotate -1 0
elif keybkey = "ArrowRight"
rotate 1 0
.
draw
.
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Cuboid do
@x 6
@y 2
Line 982 ⟶ 1,064:
Cuboid.draw(1,1,1)
Cuboid.draw(2,4,1)
Cuboid.draw(4,2,1)</langsyntaxhighlight>
 
{{out}}
Line 1,036 ⟶ 1,118:
+-----+-----+-----+-----+
</pre>
 
=={{header|Evaldraw}}==
 
=== Solid and stippled lines ===
{{trans|XPL0}}
 
Based on the XPL0 solution, but provides its own stippled line drawing routine, since evaldraw only supports solid lines out of the box.
 
[[File:Evaldraw line cuboid.gif|thumb|alt=Rotating cuboid drawn with solid visible lines and stippled hidden lines|Rotating cuboid. Visible lines solid, hidden edges drawn with stippled lines]]
 
<syntaxhighlight lang="C">
static points_x[8] = {-2.0, +2.0, +2.0, -2.0, -2.0, +2.0, +2.0, -2.0};
static points_y[8] = {-1.5, -1.5, +1.5, +1.5, -1.5, -1.5, +1.5, +1.5};
static points_z[8] = {-1.0, -1.0, -1.0, -1.0, +1.0, +1.0, +1.0, +1.0};
static segment[2*12] = {0,1, 1,2, 2,3, 3,0, 4,5, 5,6, 6,7, 7,4, 0,4, 1,5, 2,6, 3,7};
static size=50, sz=0.008, sx=-0.013; // drawing size and tumbling speeds
() {
mind = 0.0; si=0;
for(i=0; i<8; i++) {
if (points_z[i] < mind) { mind=points_z[i]; si=i;}
}
cls(0); // Clear Color Buffer
for(i=0; i<2*12-1; i+=2) {
j=segment[i];
x0 = points_x[j]*size + xres/2;
y0 = points_y[j]*size + yres/2;
k=segment[i+1];
x1 = points_x[k]*size + xres/2;
y1 = points_y[k]*size + yres/2;
if (j!=si && k!=si) {
setcol(255,0,0);
moveto(x0,y0); lineto(x1,y1);
} else {
setcol(255,255,0);
drawLineStipple(x0,y0,x1,y1,8);
}
}
sleep(16); // Sleep for 16 millis so cube tumbles slowly
for(i=0; i<8; i++) {
points_x[i] = points_x[i] + points_y[i]*Sz; //rotate vertices in X-Y plane
points_y[i] = points_y[i] - points_x[i]*Sz;
points_y[i] = points_y[i] + points_z[i]*Sx; //rotate vertices in Y-Z plane
points_z[i] = points_z[i] - points_y[i]*Sx;
}
}
 
drawLineStipple(x1,y1,x2,y2,stipple_dist) {
xdist = x1-x2; ydist=y1-y2;
stipple_dist2 = stipple_dist / 2;
if ( abs(xdist^2 + ydist^2) < stipple_dist2^2 ) return;
if(xdist < 0) xdist=-xdist;
if(ydist < 0) ydist=-ydist;
mv=0; if(ydist > xdist) mv = ydist; else mv = xdist;
x = x1; y = y1;
stepx = xdist/mv; if(x1 > x2) stepx = -stepx;
stepy = ydist/mv; if(y1 > y2) stepy = -stepy;
for(nc=0; nc<int(mv); nc++) {
if( nc % stipple_dist < stipple_dist2) setpix(x,y);
x+=stepx; y+=stepy;
}
}
</syntaxhighlight>
 
=== OpenGL-like filled polygons ===
 
[[File:Evaldraw textured cuboid.png|thumb|alt=Cuboid, 2 units wide, 3 units high, 4 units deep|Textured cuboid. Each face has its own color. Rotate with mouse position.]]
 
<syntaxhighlight lang="C">// We can define our own vec3 struct
struct vec3{x,y,z;}
 
// Static allows for globals
static modelMatrix[9];
() {
cls(0,0,32); // clear screen
clz(1e32); // clear depth buffer
setcam(0,0,-10,0,0); // set camera some units back
// create two local arrays to hold rotation matrices
double roty[9], rotz[9];
// we can access current mouse position and screen size
rotateZ( rotz, mousy/yres*2*pi );
rotateY( roty, mousx/xres*2*pi );
// evaldraw does support some GL-like drawing
// modes, but any transformations must be done by hand
// Here we use a global model matrix that
// transforms vertices created by the myVertex function
mult(modelMatrix, roty, rotz);
drawcuboid(0,0,0,2,3,4);
sleep(10);
}
 
drawcuboid(x,y,z,sx,sy,sz) {
glBegin(GL_QUADS);
setcol(192,0,0);
glTexCoord(0,0); myVertex(x-sx,y-sy,z-sz);
glTexCoord(1,0); myVertex(x+sx,y-sy,z-sz);
glTexCoord(1,1); myVertex(x+sx,y+sy,z-sz);
glTexCoord(0,1); myVertex(x-sx,y+sy,z-sz);
setcol(0,192,0);
glTexCoord(0,0); myVertex(x-sx,y-sy,z+sz);
glTexCoord(1,0); myVertex(x-sx,y-sy,z-sz);
glTexCoord(1,1); myVertex(x-sx,y+sy,z-sz);
glTexCoord(0,1); myVertex(x-sx,y+sy,z+sz);
setcol(0,0,192);
glTexCoord(0,0); myVertex(x+sx,y-sy,z+sz);
glTexCoord(1,0); myVertex(x-sx,y-sy,z+sz);
glTexCoord(1,1); myVertex(x-sx,y+sy,z+sz);
glTexCoord(0,1); myVertex(x+sx,y+sy,z+sz);
setcol(192,192,0);
glTexCoord(0,0); myVertex(x+sx,y-sy,z-sz);
glTexCoord(1,0); myVertex(x+sx,y-sy,z+sz);
glTexCoord(1,1); myVertex(x+sx,y+sy,z+sz);
glTexCoord(0,1); myVertex(x+sx,y+sy,z-sz);
 
setcol(192,0,192);
glTexCoord(0,0); myVertex(x-sx,y-sy,z+sz);
glTexCoord(1,0); myVertex(x+sx,y-sy,z+sz);
glTexCoord(1,1); myVertex(x+sx,y-sy,z-sz);
glTexCoord(0,1); myVertex(x-sx,y-sy,z-sz);
 
setcol(0,192,192);
glTexCoord(0,0); myVertex(x-sx,y+sy,z-sz);
glTexCoord(1,0); myVertex(x+sx,y+sy,z-sz);
glTexCoord(1,1); myVertex(x+sx,y+sy,z+sz);
glTexCoord(0,1); myVertex(x-sx,y+sy,z+sz);
glEnd();
}
 
myVertex(x,y,z) {
// Initialize a struct value
vec3 v = {x,y,z};
// Apply global model matrix transformation
transformPoint(v, modelMatrix);
// Submit the vertex to draw list
glVertex(v.x, v.y, v.z);
}
 
rotateX(m[9], r) { // structs and arrays are pass-by-ref
c = cos(r); s=sin(r);
m[0] = 1; m[1] = 0; m[2] = 0;
m[3] = 0; m[4] = c; m[5] = -s;
m[6] = 0; m[7] = s; m[8] = c;
}
 
rotateY(m[9], r) {
c = cos(r); s=sin(r);
m[0] = c; m[1] = 0; m[2] = s;
m[3] = 0; m[4] = 1; m[5] = 0;
m[6] = -s; m[7] = 0; m[8] = c;
}
 
rotateZ(m[9], r) {
c = cos(r); s=sin(r);
m[0] = c; m[1] = -s; m[2] = 0;
m[3] = s; m[4] = c; m[5] = 0;
m[6] = 0; m[7] = 0; m[8] = 1;
}
 
transformPoint(vec3 v, m[9]) {
x2 = v.x * m[0] + v.y * m[1] + v.z * m[2];
y2 = v.x * m[3] + v.y * m[4] + v.z * m[5];
z2 = v.x * m[6] + v.y * m[7] + v.z * m[8];
// Mutate the struct v with new values
v.x=x2; v.y=y2; v.z=z2;
}
 
mult(c[9],a[9],b[9]) { // C = AB
// multiply a row in A with a column in B
for(i=0; i<3; i++)
for(j=0; j<3; j++) {
sum = 0.0;
for(k=0; k<3; k++) {
sum += A[k*3+i] * B[k*3+j];
}
C[i*3+j] = sum;
}
}</syntaxhighlight>
 
=={{header|Factor}}==
{{libheader|raylib}}
{{works with|Factor|0.99 development release 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: classes.struct kernel raylib.ffi ;
 
640 480 "cuboid" init-window
Line 1,061 ⟶ 1,332:
end-mode-3d
end-drawing
] until drop close-window</langsyntaxhighlight>
{{out}}
[https://i.imgur.com/JQMPjhk.png]
 
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
=== ASCII lines ===
{{trans|PicoLisp}}
 
<syntaxhighlight lang="Forth">: line ( e dy d dx c n -- )
spaces dup >r emit
9 * 1- 0 do dup emit loop drop
r> emit
spaces emit cr
;
 
: cuboid { dz dy dx -- }
cr
bl 0 '- dx '+ dy 1+ line
dy 0 ?do
'| i bl dx '/ dy i - line loop
'| dy '- dx '+ 0 line
dz 4 * dy - 2 - 0 ?do
'| dy bl dx '| 0 line loop
'+ dy bl dx '| 0 line
dy 0 ?do
'/ dy i - 1- bl dx '| 0 line loop
bl 0 '- dx '+ 0 line
;</syntaxhighlight>
{{Out}}
<pre>
4 3 2 cuboid
+-----------------+
/ /|
/ / |
/ / |
+-----------------+ |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | +
| | /
| | /
| |/
+-----------------+
ok
1 1 1 cuboid
+--------+
/ /|
+--------+ |
| | |
| | +
| |/
+--------+
ok
1 2 6 cuboid
+-----------------------------------------------------+
/ /|
/ / |
+-----------------------------------------------------+ |
| | +
| | /
| |/
+-----------------------------------------------------+
ok
</pre>
 
=== ASCII faces ===
Inspired from X86 Assembly
 
<syntaxhighlight lang="Forth">
: hline ( char len )
0 ?do dup emit loop drop ;
: vline ( char len )
0 ?do dup emit -1 1 at-deltaxy loop drop ;
: cuboid { dz dy dx -- }
page
dy 0 ?do dy i - i at-xy '# dx hline loop
dz 0 ?do 0 dy i + at-xy '+ dx hline loop
dy 0 ?do dx i + dy i - at-xy '/ dz vline loop
;</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="Forth">4 3 2 cuboid</syntaxhighlight>
<pre> ##
##/
##//
++///
++///
++// ok
++/
</pre>
<syntaxhighlight lang="Forth">5 5 5 cuboid</syntaxhighlight>
<pre> #####
#####/
#####//
#####///
#####////
+++++/////
+++++//// ok
+++++///
+++++//
+++++/
</pre>
 
=={{header|FreeBASIC}}==
Adapted from OpenGL code that comes with FreeBASIC distributions.
 
<langsyntaxhighlight lang="freebasic">#include once "GL/gl.bi"
#include once "GL/glu.bi"
 
Line 1,136 ⟶ 1,516:
 
flip
loop while inkey = ""</langsyntaxhighlight>
 
=={{header|Frink}}==
This program not only draws a cube and renders it onscreen projected on the x,y, and z axes but also outputs a <CODE>.stl</CODE> file for 3-D printing or display in a 3-D modeling package like MeshLab! Frink has [https://frinklang.org/3d/frink/graphics/package-summary.html built-in routines for 3-D modeling] and can emit STL files or Wavefront OBJ files natively.
<langsyntaxhighlight lang="frink">res = 254 / in
s = 1/2 inch res
v = callJava["frink.graphics.VoxelArray", "cube", [-s, s, -s, s, -s, s, true]]
Line 1,153 ⟶ 1,533:
w.println[v.toSTLFormat["cube", 1/(res mm)]]
w.close[]
println["done."]</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
This code compiles into a macOS application that allows 360-degree mouse rotation of the cuboid. All six faces of the cuboid have different colors.
<syntaxhighlight lang="futuebasic">
include "Tlbx SceneKit.incl"
 
_window = 1
begin enum output 1
_sceneView
end enum
 
local fn Cuboid as SCNSceneRef
SCNSceneRef scene = fn SCNSceneInit
SCNNodeRef rootNode = fn SCNSceneRootNode( scene )
SCNCameraRef camera = fn SCNCameraInit
SCNNodeRef cameraNode = fn SCNNodeInit
SCNNodeSetCamera( cameraNode, camera )
SCNNodeAddChildNode( rootNode, cameraNode )
SCNVector3 cameraPos = {0.0, 0.0, 10.0}
SCNNodeSetPosition( cameraNode, cameraPos )
SCNNodeRef lightNode = fn SCNNodeInit
SCNLightRef light = fn SCNLightInit
SCNLightSetType( light, SCNLightTypeOmni )
SCNNodeSetPosition( lightNode, fn SCNVector3Make( 0.0, 10.0, 10.0 ) )
SCNNodeAddChildNode( rootNode, lightNode )
SCNNodeRef ambientLightNode = fn SCNNodeInit
SCNLightRef ambientLight = fn SCNLightInit
SCNLightSetType( ambientLight, SCNLightTypeAmbient )
SCNLightSetColor( ambientLight, fn ColorGray )
SCNNodeSetLight( ambientLightNode, ambientLight )
SCNNodeAddChildNode( rootNode, ambientLightNode )
SCNBoxRef boxGeometry = fn SCNBoxInit( 2.0, 3.0, 4.0, 0.0 )
SCNNodeRef boxNode = fn SCNNodeWithGeometry( boxGeometry )
SCNMaterialRef side1 = fn SCNMaterialInit
SCNMaterialRef side2 = fn SCNMaterialInit
SCNMaterialRef side3 = fn SCNMaterialInit
SCNMaterialRef side4 = fn SCNMaterialInit
SCNMaterialRef side5 = fn SCNMaterialInit
SCNMaterialRef side6 = fn SCNMaterialInit
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side1 ), fn ColorBlue )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side2 ), fn ColorOrange )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side3 ), fn ColorRed )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side4 ), fn ColorGreen )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side5 ), fn ColorYellow )
SCNMaterialPropertySetContents( fn SCNMaterialMultiply( side6 ), fn ColorCyan )
SCNGeometrySetMaterials( boxGeometry, @[side1,side2,side3,side4,side5,side6] )
SCNNodeAddChildNode( rootNode, boxNode )
SCNActionableRunAction( boxNode, fn SCNActionRotateByAngle( M_PI, fn SCNVector3Make( 1.0, 1.0, 1.0 ), 0.0 ) )
end fn = scene
 
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 400, 400 )
window _window, @"Rosetta Code 2-3-4 Cuboid", r
scnview _sceneView, fn Cuboid, r
SCNViewSetBackgroundColor( _sceneView, fn ColorBlack )
SCNViewSetAllowsCameraControl( _sceneView, YES )
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
select (ev)
case _windowWillClose : end
end select
end fn
 
on dialog fn DoDialog
 
fn BuildWindow
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:FutureBasic_cuboid.png]]
 
=={{header|Go}}==
{{trans|PicoLisp}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,192 ⟶ 1,651:
cuboid(1, 1, 1)
cuboid(6, 2, 1)
}</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,239 ⟶ 1,698:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
 
Line 1,298 ⟶ 1,757:
createWindow name
displayCallback $= display
mainLoop</langsyntaxhighlight>
[[Image:CuboidHaskell.png]]
 
Line 1,304 ⟶ 1,763:
Hack alert! I haven't even bothered to center the display.
With larger resolutions and the viewmat script, this code can generate reasonable 2D displays with a different color for each face.
<langsyntaxhighlight lang="j"> vectors =. ((% +/&.:*:"1) _1 1 0,:_1 _1 3) +/@:*"1/~ 2 3 4*=i.3
' .*o' {~ +/ 1 2 3* (|:"2 -."_ 1~ vectors) ([:*./ 1 = 0 1 I. %.~)"_ 1"_1 _ ]4j21 ,~"0/&:i: 4j41
Line 1,327 ⟶ 1,786:
*****.....
</langsyntaxhighlight>
 
=={{header|Java}}==
[[File:cuboid_java.png|200px|thumb|right]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import static java.lang.Math.*;
Line 1,445 ⟶ 1,904:
});
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
{{trans|Java}}
<langsyntaxhighlight lang="javascript"><!DOCTYPE html>
<html lang="en">
 
Line 1,547 ⟶ 2,006:
 
</body>
</html></langsyntaxhighlight>
 
=={{header|Julia}}==
Line 1,554 ⟶ 2,013:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">_pr(t::Dict, x::Int, y::Int, z::Int) = join((rstrip(join(t[(n, m)] for n in range(0, 3+x+z))) for m in reverse(range(0, 3+y+z))), "\n")
 
function cuboid(x::Int, y::Int, z::Int)
Line 1,571 ⟶ 2,030:
println("\nCUBOID($x, $y, $z)\n")
println(cuboid(x, y, z))
end</langsyntaxhighlight>
 
{{out}}
Line 1,630 ⟶ 2,089:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1
 
import java.awt.*
Line 1,752 ⟶ 2,211:
f.isVisible = true
}
}</langsyntaxhighlight>
 
=={{header|Lambdatalk}}==
{{trans|javascript}} An adaptation working in the lambdaway project (where wiki pages are editable in realtime and inline:): http://lambdaway.free.fr/lambdawalks/?view=cuboid .
<syntaxhighlight lang="scheme">
1) creating the canvas
 
{canvas {@ width="580" height="580"}}
 
2) calling javascript
 
{script
var canvas, g, width, height;
var mouseX = 0, prevMouseX, mouseY = 0, prevMouseY;
 
function run() {
canvas = document.querySelector("canvas");
width = canvas.width;
height = canvas.height;
g = canvas.getContext("2d");
canvas.addEventListener("mousemove",
function (event) {
prevMouseX = mouseX;
prevMouseY = mouseY;
mouseX = event.x;
mouseY = event.y;
 
var incrX = (mouseX - prevMouseX) * 0.01;
var incrY = (mouseY - prevMouseY) * 0.01;
 
rotateCuboid(incrX, incrY);
drawCuboid();
});
 
scale(80, 120, 160);
rotateCuboid(Math.PI / 5, Math.PI / 9);
drawCuboid()
 
}
 
var nodes = [[-1, -1, -1], [-1, -1, 1], [-1, 1, -1], [-1, 1, 1],
[1, -1, -1], [1, -1, 1], [1, 1, -1], [1, 1, 1]];
 
var edges = [[0, 1], [1, 3], [3, 2], [2, 0], [4, 5], [5, 7], [7, 6],
[6, 4], [0, 4], [1, 5], [2, 6], [3, 7]];
 
function scale(factor0, factor1, factor2) {
nodes.forEach(function (node) {
node[0] *= factor0;
node[1] *= factor1;
node[2] *= factor2;
});
}
 
function rotateCuboid(angleX, angleY) {
 
var sinX = Math.sin(angleX);
var cosX = Math.cos(angleX);
 
var sinY = Math.sin(angleY);
var cosY = Math.cos(angleY);
 
nodes.forEach(function (node) {
var x = node[0];
var y = node[1];
var z = node[2];
 
node[0] = x * cosX - z * sinX;
node[2] = z * cosX + x * sinX;
 
z = node[2];
 
node[1] = y * cosY - z * sinY;
node[2] = z * cosY + y * sinY;
});
}
 
function drawCuboid() {
g.save();
g.clearRect(0, 0, width, height);
g.translate(width / 2, height / 2);
g.strokeStyle = "#000";
g.beginPath();
 
edges.forEach(function (edge) {
var p1 = nodes[edge[0]];
var p2 = nodes[edge[1]];
g.moveTo(p1[0], p1[1]);
g.lineTo(p2[0], p2[1]);
});
g.closePath();
g.stroke();
 
g.restore();
}
 
setTimeout( run, 1 )
}
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
Text solution
<syntaxhighlight lang="lb">
<lang lb>
Call cuboid 1,3,4
 
Line 1,784 ⟶ 2,344:
Locate 1, dp+hi+3: Print w$
End Sub
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,805 ⟶ 2,365:
</pre>
Graphic solution
<syntaxhighlight lang="lb">
<lang lb>
NoMainWin
Global sw, sh
Line 1,865 ⟶ 2,425:
End If
End Sub
</syntaxhighlight>
</lang>
 
=={{header|Logo}}==
Line 1,871 ⟶ 2,431:
{{works with|MSWlogo}}
Simple implementation, just moving to the appropriate points every time.
<langsyntaxhighlight lang="logo">to cuboid :l1 :l2 :l3
cs perspective ;making the room ready to use
setxyz :l1 0 0
Line 1,884 ⟶ 2,444:
setxyz 0 :l2 -:l3
setxyz :l1 :l2 -:l3
end</langsyntaxhighlight>
Example call to achieve task:
<syntaxhighlight lang ="logo">cuboid 50 100 150</langsyntaxhighlight>
 
=={{header|LSL}}==
Rez a box on the ground, raise it up a few meters, add the following as a New Script.
<langsyntaxhighlight LSLlang="lsl">vector vSCALE = <2.0, 3.0, 4.0>;
default {
state_entry() {
llSetScale(vSCALE);
}
}</langsyntaxhighlight>
{{Out}} ''Ahhhhh; I always wondered what a Cuboid looked like, now I know!'' :)<br>
[[File:Draw_A_Cuboid_LSL.jpg|200px|Draw a Cuboid]]<br>
Line 1,903 ⟶ 2,463:
=={{header|Lua}}==
Begin with the code in the [[Draw_a_rotating_cube#Lua|Draw_a_rotating_cube]] task, then extend the cube object as follows:
<langsyntaxhighlight lang="lua">-- needed for actual task
cube.scale = function(self, sx, sy, sz)
for i,v in ipairs(self.verts) do
Line 1,915 ⟶ 2,475:
v[1], v[2], v[3] = v[1]+tx, v[2]+ty, v[3]+tz
end
end</langsyntaxhighlight>
Then replace all of the "demo" code below the empty comment line "--" with:
<langsyntaxhighlight lang="lua">--
bitmap:init(40,40)
cube:scale(2,3,4)
Line 1,925 ⟶ 2,485:
renderer:render(cube, camera, bitmap)
screen:clear()
bitmap:render()</langsyntaxhighlight>
{{out}}
<pre style="font-size:50%">················································································
Line 1,970 ⟶ 2,530:
=={{header|Maple}}==
This creates a cuboid with one corner at (0,0,0) and the opposite at (2,3,4):
<langsyntaxhighlight Maplelang="maple">plots:-display(plottools:-parallelepiped([2, 0, 0], [0, 0, 4], [0, 3, 0]), orientation = [45, 60])</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
This creates a cuboid with one corner at (0,0,0) and the opposite at (2,3,4):
<langsyntaxhighlight Mathematicalang="mathematica">Graphics3D[Cuboid[{0,0,0},{2,3,4}]]</langsyntaxhighlight>
Output would be fully-rendered, rotatable 3D in the notebook.
Also, many aspects of the cuboid's appearance and lighting can be controlled quite easily. For those, see Mathematica's documentation in the program or on the web.
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">load(draw)$
 
draw3d(xu_grid=100, yv_grid=100, surface_hide=true,
palette=gray, enhanced3d=[x - z / 4 - y / 4, x, y, z],
implicit(max(abs(x / 4), abs(y / 6), abs(z / 8)) = 1,
x,-10,10,y,-10,10,z,-10,10))$</langsyntaxhighlight>
 
=={{header|MiniScript}}==
{{works with|Mini Micro}}
 
This uses the ''rotateAndProject'' function from the [[Draw a rotating cube#MiniScript|Draw a rotating cube]] task, but reduces the rest of the code to just drawing three static faces.
 
<syntaxhighlight lang="MiniScript">import "mathUtil"
scale = 20
 
img = file.loadImage("/sys/pics/shapes/SquareThin.png")
clear; gfx.clear color.gray
 
// Rotate the given [x,y,z] point by some number of degrees
// around the Y axis, then project to the screen.
rotateAndProject = function(point3d, rotDegrees)
radians = rotDegrees * pi/180
cosAng = cos(radians); sinAng = sin(radians)
// First, rotate around the Y axis in 3D space
x = point3d[0] * cosAng - point3d[2] * sinAng
y = point3d[1]
z = point3d[0] * sinAng + point3d[2] * cosAng
// Then, project this to the screen
result = [480 + x * scale, 320 + y * scale]
p = (80 - z) / 80 // (perspective factor)
return mathUtil.lerp2d(result, [480,800], 1-p)
end function
 
addFace = function(points3d, tint)
sp = new Face
sp.image = img
corners = []
for p in points3d
corners.push rotateAndProject(p, -45)
end for
sp.setCorners corners
sp.tint = tint
display(4).sprites.push sp
end function
 
w = 3; h = 2; d = 4
addFace [[-w,-h,-d],[w,-h,-d],[w,h,-d],[-w,h,-d]], color.lime
addFace [[w,-h,-d],[w,-h,d],[w,h,d],[w,h,-d]], color.aqua
addFace [[-w,h,-d],[w,h,-d],[w,h,d],[-w,h,d]], color.pink</syntaxhighlight>
 
{{output}}
[[File:MiniScript-cuboid.png|alt=Screen shot of cuboid with red, green, and blue faces]]
 
=={{header|Nim}}==
{{trans|PicoLisp}}
<langsyntaxhighlight lang="nim">import strutils
 
proc cline(n, x, y: int, cde: string) =
Line 2,007 ⟶ 2,613:
cuboid 2, 3, 4
cuboid 1, 1, 1
cuboid 6, 2, 1</langsyntaxhighlight>
{{Out}}
<pre> +-----------------+
Line 2,051 ⟶ 2,657:
Drawing a cuboid is easy in openscad:
 
<langsyntaxhighlight lang="openscad">// This will produce a simple cuboid
cube([2,3,4]);
</syntaxhighlight>
</lang>
 
=={{header|OxygenBasic}}==
Using an OpenGl-based console
<syntaxhighlight lang="text">
% Title "Cuboid 2x3x4"
'% Animated
Line 2,079 ⟶ 2,685:
 
EndScript
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
Line 2,089 ⟶ 2,695:
{{Works with|PARI/GP|2.7.4 and above}}
 
<langsyntaxhighlight lang="parigp">
\\ Simple "cuboid". Try different parameters of this Cuboid() function.
\\ 4/11/16 aev
Line 2,115 ⟶ 2,721:
Cuboid(5,3,1,20); \\Cuboid2.png
}
</langsyntaxhighlight>
 
{{Output}}
Line 2,129 ⟶ 2,735:
{{trans|Ada}}
{{works with|Free_Pascal}}
<langsyntaxhighlight lang="pascal">program Cuboid_Demo(output);
 
procedure DoCuboid(sWidth, sHeight, Depth: integer);
Line 2,197 ⟶ 2,803:
writeln('6, 2, 1:');
DoCuboid(6, 2, 1);
end.</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,240 ⟶ 2,846:
=={{header|Perl}}==
{{trans|Go}}
<langsyntaxhighlight lang="perl">sub cubLine ($$$$) {
my ($n, $dx, $dy, $cde) = @_;
 
Line 2,277 ⟶ 2,883:
cuboid 2, 3, 4;
cuboid 1, 1, 1;
cuboid 6, 2, 1;</langsyntaxhighlight>
 
{{Out}}
Line 2,325 ⟶ 2,931:
 
'''ASCII Art'''
<langsyntaxhighlight lang="perl">use 5.010;
 
# usage: script X Y Z [S]
Line 2,351 ⟶ 2,957:
}
 
cuboid shift // rand 20, shift // rand 10, shift // rand 10;</langsyntaxhighlight>
 
Cuboid(2,3,4)
Line 2,372 ⟶ 2,978:
Simple orthogonal projection, no perspective.
You can run this online [http://phix.x10.mx/p2js/DrawCuboid.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\draw_cuboid.exw
Line 2,527 ⟶ 3,133:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
=== ascii ===
Two versions: the first uses a complete/rectangular grid and outputs at the end,
whereas the second uses a slightly trickier line-by-line approach.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">draw_line</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">,</span><span style="color: #000000;">len</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
Line 2,559 ⟶ 3,165:
<span style="color: #000000;">ascii_cuboid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">ascii_cuboid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,584 ⟶ 3,190:
</pre>
And as promised a line-by-line solution. Same output.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">cuboid</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
Line 2,613 ⟶ 3,219:
<span style="color: #000000;">cuboid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cuboid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
===Using ASCII===
<langsyntaxhighlight PicoLisplang="picolisp">(de cuboid (DX DY DZ)
(cubLine (inc DY) "+" DX "-" 0)
(for I DY
Line 2,635 ⟶ 3,241:
(prin C)
(space DY)
(prinl E) )</langsyntaxhighlight>
{{Out}}
<pre>: (cuboid 2 3 4)
Line 2,681 ⟶ 3,287:
===Using OpenGL===
{{libheader|GLUT}}
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/openGl.l")
 
(setq *AngleX -26.0 *AngleY 74.0)
Line 2,739 ⟶ 3,345:
(glutSwapBuffers) )
 
(glutMainLoop)</langsyntaxhighlight>
 
=={{header|POV-Ray}}==
<langsyntaxhighlight POV-Raylang="pov">camera { perspective location <2.6,2.2,-4.2> look_at <0,-.5,0>
aperture .05 blur_samples 100 variance 1/100000 focal_point <2,1,-2>}
Line 2,756 ⟶ 3,362:
}
translate <-1,-.5,-2>
}</langsyntaxhighlight>
 
[[FILE:PovRay-cuboid.jpg‎]]
Line 2,763 ⟶ 3,369:
A cuboid in Processing is created with box(). It may be styled with stroke, fill, and lighting.
 
<langsyntaxhighlight Processinglang="processing">size(500, 500, P3D);
background(0);
// position
Line 2,775 ⟶ 3,381:
pointLight(255, 255, 255, 400, -400, 400);
// draw box
box(200, 300, 400);</langsyntaxhighlight>
 
=={{header|Prolog}}==
Works with SWI-Prolog and XPCE.
<langsyntaxhighlight Prologlang="prolog">cuboid(D1,D2,D3) :-
W is D1 * 50,
H is D2 * 50,
Line 2,856 ⟶ 3,462:
send(P, fill_pattern, Color).
 
:- pce_end_class.</langsyntaxhighlight>
{{Out}}
<pre>?- cuboid(2,3,4).
Line 2,866 ⟶ 3,472:
=={{header|Pure Data}}==
Requires `Gem`
<syntaxhighlight lang="pure data">
<lang Pure Data>
#N canvas 1 51 450 300 10;
#X obj 66 67 gemwin;
Line 2,888 ⟶ 3,494:
#X connect 8 1 7 0;
#X connect 9 0 1 0;
</syntaxhighlight>
</lang>
Displays a rotating cuboid.
 
Line 2,896 ⟶ 3,502:
=={{header|PureBasic}}==
Using generic PureBasic 2D-library.
<langsyntaxhighlight PureBasiclang="purebasic">Procedure Draw_a_Cuboid(Window, X,Y,Z)
w=WindowWidth(Window)
h=WindowHeight(Window)
Line 2,961 ⟶ 3,567:
If respons=#PB_MessageRequester_Yes
SaveImage(0, SaveFileRequester("","","",0),#PB_ImagePlugin_PNG,9)
EndIf</langsyntaxhighlight>
[[Image:PB_Cuboid.png]]
 
=={{header|Python}}==
===Ascii-Art===
<langsyntaxhighlight lang="python">def _pr(t, x, y, z):
txt = '\n'.join(''.join(t[(n,m)] for n in range(3+x+z)).rstrip()
for m in reversed(range(3+y+z)))
Line 2,991 ⟶ 3,597:
if __name__ == '__main__':
for dim in ((2,3,4), (3,4,2), (4,2,3)):
print("CUBOID%r" % (dim,), cuboid(*dim), sep='\n')</langsyntaxhighlight>
 
{{Out}}
Line 3,029 ⟶ 3,635:
{{works with|Python|2.7.5}}
====Short version====
<langsyntaxhighlight lang="python">from visual import *
mybox = box(pos=(0,0,0), length=4, height=2, width=3, axis=(-0.1,-0.1,0.1) )
scene.title = "VPython: cuboid"</langsyntaxhighlight>
 
====Cuboid viewer====
Line 3,039 ⟶ 3,645:
show infos about scene and object,
plus a selfrunning demo-mode that cycles thru everything.
<langsyntaxhighlight lang="python">
from __future__ import print_function, division
from visual import *
Line 3,301 ⟶ 3,907:
if autoDemo>0 :
demoStep()
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ $ "turtleduck.qky" loadfile ] now!
 
[ ' [ 192 192 192 ]
fill
[ 2 times
[ 300 1 walk
1 4 turn
400 1 walk
1 4 turn ] ] ] is front ( --> )
 
[ ' [ 128 128 128 ]
fill
[ 2 times
[ 300 1 walk
-1 3 turn
200 1 walk
-1 6 turn ] ] ] is top ( --> )
 
[ ' [ 64 64 64 ]
fill
[ 1 4 turn
2 times
[ 400 1 walk
5 12 turn
200 1 walk
1 12 turn ]
-1 4 turn ] ] is side ( --> )
 
[ front top side ] is cuboid ( --> )
 
turtle
-1 4 turn
120 1 fly
1 4 turn
cuboid
1 4 turn
120 1 fly
-1 4 turn</syntaxhighlight>
 
{{out}}
 
[[File:Quackery Draw a cuboid.png|thumb|center]]
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket/gui
(require sgl/gl)
 
Line 3,370 ⟶ 4,021:
(define gl (new my-canvas% (parent win) (x 2) (y 3) (z 4)))
 
(send win show #t)</langsyntaxhighlight>
 
[[Image:Racket_cuboid.png]]
Line 3,377 ⟶ 4,028:
(formerly Perl 6)
{{works with|moar|2015-11-27}}
<syntaxhighlight lang="raku" perl6line>sub braille-graphics (%a) {
my ($ylo, $yhi, $xlo, $xhi);
for %a.keys -> $y {
Line 3,420 ⟶ 4,071:
}
cuboid $_ for [2,3,4], [3,4,2], [4,2,3], [1,1,1], [8,1,1], [1,8,1], [1,1,8];</langsyntaxhighlight>
 
{{out}}
Line 3,426 ⟶ 4,077:
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">3 elements d h w
 
: spaces ( n- ) &space times ;
Line 3,444 ⟶ 4,095:
face ;
 
2 3 4 cuboid</langsyntaxhighlight>
 
{{Out}}
Line 3,461 ⟶ 4,112:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program displays a cuboid (dimensions, if specified, must be positive integers).*/
parse arg x y z indent . /*x, y, z: dimensions and indentation.*/
x=p(x 2); y=p(y 3); z=p(z 4); in=p(indent 0) /*use the defaults if not specified. */
Line 3,477 ⟶ 4,128:
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: parse arg #,$,a 2 b 3 c 4 /*get the arguments (or parts thereof).*/
say pad || right(a, p(# 1) )copies(b, 4*x)a || right(c, p($ 0) + 1); return</langsyntaxhighlight>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 2 &nbsp; 3 &nbsp; 4 &nbsp; 35 </tt>
<pre>
Line 3,516 ⟶ 4,167:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Draw a cuboid
 
Line 3,575 ⟶ 4,226:
label1 { setpicture(p1) show() }
return
</syntaxhighlight>
</lang>
Output:
 
Line 3,581 ⟶ 4,232:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">X, Y, Z = 6, 2, 3
DIR = {"-" => [1,0], "|" => [0,1], "/" => [1,1]}
 
Line 3,607 ⟶ 4,258:
cuboid(1, 1, 1)
cuboid(6, 2, 1)
cuboid(2, 4, 1)</langsyntaxhighlight>
{{out}}
<pre>
Line 3,663 ⟶ 4,314:
=={{header|Scala}}==
===Java Swing Interoperability===
<langsyntaxhighlight Scalalang="scala">import java.awt._
import java.awt.event.{MouseAdapter, MouseEvent}
 
Line 3,753 ⟶ 4,404:
}
})
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">const dirs = Hash("-" => [1,0], "|" => [0,1], "/" => [1,1])
 
func cuboid(nx, ny, nz) {
Line 3,787 ⟶ 4,438:
cuboid(1, 1, 1)
cuboid(6, 2, 1)
cuboid(2, 4, 1)</langsyntaxhighlight>
 
<b>A faster approach:</b>
<langsyntaxhighlight lang="ruby">func cuboid (x=1,y=1,z=1,s=' ',c='+',h='-',v='|',d='/') {
say("cuboid %d %d %d:" % (x, y, z))
' ' * z+1 + c + h*x + c -> say
Line 3,819 ⟶ 4,470:
cuboid(1, 1, 1)
cuboid(6, 2, 1)
cuboid(2, 4, 1)</langsyntaxhighlight>
{{out}}
<pre>cuboid 2 3 4:
Line 3,859 ⟶ 4,510:
{{tcllib|math::linearalgebra}}
{{tcllib|math::constants}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
package require math::linearalgebra
Line 3,937 ⟶ 4,588:
set cuboid [makeCuboid {2 3 4}]
set projection [transform::make 15 50 {100 100 100}]
drawShape .c $cuboid</langsyntaxhighlight>
[[File:Cuboid tcl.png|Output (cropped)]]
 
This becomes more engaging if the drawing is animated with a final driver piece like this (the definitions part of the code is identical to above):
<langsyntaxhighlight lang="tcl">pack [canvas .c -width 400 -height 400]
set cuboid [makeCuboid {2 3 4}]
wm protocol . WM_DELETE_WINDOW { exit }
Line 3,951 ⟶ 4,602:
update
after 50
}</langsyntaxhighlight>
 
=={{header|VBScript}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="vb">x = 6 : y = 2 : z = 3
 
Sub cuboid(nx, ny, nz)
Line 4,002 ⟶ 4,653:
End Sub
 
cuboid 2,3,4</langsyntaxhighlight>
{{Out}}
<pre>Cuboid 2 3 4:
Line 4,029 ⟶ 4,680:
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var cubLine = Fn.new { |n, dx, dy, cde|
Line 4,051 ⟶ 4,702:
cuboid.call(2, 3, 4)
cuboid.call(1, 1, 1)
cuboid.call(6, 2, 1)</langsyntaxhighlight>
 
{{out}}
Line 4,100 ⟶ 4,751:
=={{header|X86 Assembly}}==
Sixty bytes does it.
<langsyntaxhighlight lang="asm"> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
3 0000 .code
Line 4,153 ⟶ 4,804:
52
53 end start
</syntaxhighlight>
</lang>
{{Out}}
[[File:Cuboid_BBC.gif]]
Line 4,159 ⟶ 4,810:
=={{header|XPL0}}==
[[File:CuboidXPL0.gif|right]]
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
real X, Y, Z, Farthest; \arrays: 3D coordinates of vertices
int I, J, K, SI, Segment;
Line 4,188 ⟶ 4,839:
until KeyHit; \run until a key is struck
SetVid(3); \restore normal text mode (for DOS)
]</langsyntaxhighlight>
 
=={{header|Zig}}==
{{trans|Factor}}
{{libheader|raylib}}
<syntaxhighlight lang="zig">
const std = @import("std");
const c = @cImport({
@cInclude("raylib.h");
});
 
pub fn main() !void {
c.SetConfigFlags(c.FLAG_WINDOW_RESIZABLE | c.FLAG_VSYNC_HINT);
c.InitWindow(600, 480, "cuboid");
defer c.CloseWindow();
 
const camera = c.Camera3D{
.position = .{ .x = 4.5, .y = 4.5, .z = 4.5 },
.target = .{ .x = 0, .y = 0, .z = 0 },
.up = .{ .x = 0, .y = 1, .z = 0 },
.fovy = 45.0,
.projection = c.CAMERA_PERSPECTIVE,
};
 
c.SetTargetFPS(60);
 
while (!c.WindowShouldClose()) {
c.BeginDrawing();
defer c.EndDrawing();
 
c.ClearBackground(c.BLACK);
 
{
c.BeginMode3D(camera);
defer c.EndMode3D();
 
c.DrawCubeWires(.{ .x = 0, .y = 0, .z = 0 }, 2, 3, 4, c.LIME);
}
}
}
</syntaxhighlight>
 
=={{header|zkl}}==
Draws a wire frame PPM image, no hidden/dotted lines.<br/>
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">var [const] M=50.0;
fcn cuboid(w,h,z){
w*=M; h*=M; z*=M; // relative to abs dimensions
Line 4,213 ⟶ 4,904:
 
bitmap.write(File("foo.ppm","wb"));
}(2,3,4);</langsyntaxhighlight>
{{out}}
http://www.zenkinetic.com/Images/RosettaCode/cuboid.jpg
 
=={{header|ZX Spectrum Basic}}==
<langsyntaxhighlight lang="zxbasic">10 LET width=50: LET height=width*1.5: LET depth=width*2
20 LET x=80: LET y=10
30 PLOT x,y
40 DRAW 0,height: DRAW width,0: DRAW 0,-height: DRAW -width,0: REM Front
50 PLOT x,y+height: DRAW depth/2,height: DRAW width,0: DRAW 0,-height: DRAW -width,-height
60 PLOT x+width,y+height: DRAW depth/2,height</langsyntaxhighlight>
 
[[Category:Geometry]]
12

edits