Draw a rotating cube: Difference between revisions

m
→‎Zig: Update "works with". Simplify two lines involving casting and math.
m (→‎Zig: Update "works with". Simplify two lines involving casting and math.)
 
(46 intermediate revisions by 17 users not shown)
Line 15:
{{libheader|SDLAda}}
{{trans|Go}}
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Elementary_Functions;
 
with SDL.Video.Windows.Makers;
Line 119:
Window.Finalize;
SDL.Finalise;
end Rotating_Cube;</langsyntaxhighlight>
 
=={{header|Amazing Hopper}}==
{{Trans|BASIC256}}
{{Trans|FreeBASIC}}
<p>El programa requiere de la ejecución con "rxvt" de Linux:
 
rxvt -g 500x250 -fn "xft:FantasqueSansMono-Regular:pixelsize=1" -e ./bin/cubo
</p>
[[File:Captura_de_pantalla_de_2022-10-11_12-38-33.png|200px|thumb|rigth|Caption]]
<syntaxhighlight lang="txt">
#context-free Draw a cube
Loop for (i=1, #(i<=3), ++i)
Draw a line (size_2, {size_2} Minus(scale_zoff), [i] Get 'x',\
{size_2} Minus(scale x zoff) )
Draw a line (size_2, {size_2} Plus(scale_zoff), [{7}Minus(i)] Get 'x' ,\
{size_2} Plus(scale x zoff) )
Draw a line ([i] Get 'x', {size_2} Minus(scale x zoff),\
[Minusone(i) Module(3) Plus(4)] Get 'x', {size_2} Plus(scale x zoff) )
Draw a line ([i] Get 'x', {size_2} Minus(scale x zoff), \
[{i} Module(3) Plus(4)] Get 'x', {size_2} Plus(scale x zoff) )
Next
Return\\
 
#context-free Delete old cube
Color back '0', Draw a cube
Return\\
 
#context-free Setting values of program
Set( Div(M_PI,6), Mul(5,Div(M_PI,6)), Mul(3,M_PI_2), Mul(11,Div(M_PI,6)),\
M_PI_2, Mul(7,Div(M_PI,6)) )
Append to list 'cylphi'
/* pre-cálculos */
Let ( dt := Div(1,30 ))
Let (size_2 := Div( SIZE, 2))
Let (scale_zoff := Div( SCALE,zoff))
Let (scale x zoff := Mul (SCALE, zoff))
Return \\
 
#include <jambo.h>
 
/*
Execute with:
$ rxvt -g 250x250 -fn "xft:FantasqueSansMono-Regular:pixelsize=1" -e hopper jm/cubo.jambo
*/
 
#define SCALE 50
#define SIZE 200
#define zoff 0.5773502691896257645091487805019574556
#define cylr 1.6329931618554520654648560498039275946
 
Main
Set break
theta=0, dtheta=1.5, lasttime=0, dt=0 , timer=0
size_2=0, scale_zoff=0, scale x zoff=0, cylphi = {}
 
Dim (6) as zeros (x)
Setting values of program
Cls
/* Draw a cube */
Loop while ( Not (Keypressed))
Tic(lasttime)
Loop for( i=1, #(i<=6), ++i )
Add( size_2, Mul( Mul(SCALE,cylr), Cos( [i] Get 'cylphi' Plus 'theta')) )
Put 'x'
Next
Color back '15', Draw a cube
Loop
Timecpu(timer)
While ( This 'timer' Compared to 'Add(lasttime, dt)' Is less )
Let ( theta := Add( theta, Mul( dtheta, Sub(timer, lasttime))))
Sleep (0.01)
Delete old cube
Back
End
 
Subrutines
 
</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Requires [https://www.autohotkey.com/boards/viewtopic.php?f=6&t=6517&start=320 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">; ---------------------------------------------------------------
cubeSize := 200
deltaX := A_ScreenWidth/2
Line 436 ⟶ 519:
ExitApp
Return
; ---------------------------------------------------------------</langsyntaxhighlight>
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">global escala
global tam
global zoff
global cylr
 
escala = 50
tam = 320
zoff = 0.5773502691896257645091487805019574556
cylr = 1.6329931618554520654648560498039275946
 
clg
graphsize tam, tam
 
dim x(6)
theta = 0.0
dtheta = 1.5
dt = 1.0 / 30
dim cylphi = {PI/6, 5*PI/6, 3*PI/2, 11*PI/6, PI/2, 7*PI/6}
 
while key = ""
lasttime = msec
for i = 0 to 5
x[i] = tam/2 + escala *cylr * cos(cylphi[i] + theta)
next i
clg
call drawcube(x)
 
while msec < lasttime + dt
end while
theta += dtheta*(msec-lasttime)
pause .4
call drawcube(x)
end while
 
subroutine drawcube(x)
for i = 0 to 2
color rgb(0,0,0) #black
line tam/2, tam/2 - escala / zoff, x[i], tam/2 - escala * zoff
line tam/2, tam/2 + escala / zoff, x[5-i], tam/2 + escala * zoff
line x[i], tam/2 - escala * zoff, x[(i % 3) + 3], tam/2 + escala * zoff
line x[i], tam/2 - escala * zoff, x[((i+1)%3) + 3], tam/2 + escala * zoff
next i
end subroutine</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 graphics 0
120 graphics color 0,0,0
130 while true
140 graphics cls
150 x = cos(t)*20
160 y = sin(t)*18
170 r = sin(t+t)
180 moveto (x+40),(y+40-r)
190 lineto (-y+40),(x+40-r)
200 moveto (-y+40),(x+40-r)
210 lineto (-x+40),(-y+40-r)
220 moveto (-x+40),(-y+40-r)
230 lineto (y+40),(-x+40-r)
240 moveto (y+40),(-x+40-r)
250 lineto (x+40),(y+40-r)
260 moveto (x+40),(y+20+r)
270 lineto (-y+40),(x+20+r)
280 moveto (-y+40),(x+20+r)
290 lineto (-x+40),(-y+20+r)
300 moveto (-x+40),(-y+20+r)
310 lineto (y+40),(-x+20+r)
320 moveto (y+40),(-x+20+r)
330 lineto (x+40),(y+20+r)
340 moveto (x+40),(y+40-r)
350 lineto (x+40),(y+20+r)
360 moveto (-y+40),(x+40-r)
370 lineto (-y+40),(x+20+r)
380 moveto (-x+40),(-y+40-r)
390 lineto (-x+40),(-y+20+r)
400 moveto (y+40),(-x+40-r)
410 lineto (y+40),(-x+20+r)
420 for i = 1 to 1000 : next i
430 t = t+0.15
440 wend</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="qbasic">100 SCREEN 2
110 WHILE 1
120 CLS
130 X = COS(T)*20
140 Y = SIN(T)*18
150 R = SIN(T+T)
160 LINE (( X+40),( Y+40-R))-((-Y+40),( X+40-R))
170 LINE ((-Y+40),( X+40-R))-((-X+40),(-Y+40-R))
180 LINE ((-X+40),(-Y+40-R))-(( Y+40),(-X+40-R))
190 LINE (( Y+40),(-X+40-R))-(( X+40),( Y+40-R))
200 LINE (( X+40),( Y+20+R))-((-Y+40),( X+20+R))
210 LINE ((-Y+40),( X+20+R))-((-X+40),(-Y+20+R))
220 LINE ((-X+40),(-Y+20+R))-(( Y+40),(-X+20+R))
230 LINE (( Y+40),(-X+20+R))-(( X+40),( Y+20+R))
240 LINE (( X+40),( Y+40-R))-(( X+40),( Y+20+R))
250 LINE ((-Y+40),( X+40-R))-((-Y+40),( X+20+R))
260 LINE ((-X+40),(-Y+40-R))-((-X+40),(-Y+20+R))
270 LINE (( Y+40),(-X+40-R))-(( Y+40),(-X+20+R))
280 T = T+.15
290 WEND</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">100 SCREEN 2
110 COLOR 15
120 CLS
130 X = COS(T)*20
140 Y = SIN(T)*18
150 R = SIN(T+T)
160 LINE (( X+40),( Y+40-R))-((-Y+40),( X+40-R))
170 LINE ((-Y+40),( X+40-R))-((-X+40),(-Y+40-R))
180 LINE ((-X+40),(-Y+40-R))-(( Y+40),(-X+40-R))
190 LINE (( Y+40),(-X+40-R))-(( X+40),( Y+40-R))
200 LINE (( X+40),( Y+20+R))-((-Y+40),( X+20+R))
210 LINE ((-Y+40),( X+20+R))-((-X+40),(-Y+20+R))
220 LINE ((-X+40),(-Y+20+R))-(( Y+40),(-X+20+R))
230 LINE (( Y+40),(-X+20+R))-(( X+40),( Y+20+R))
240 LINE (( X+40),( Y+40-R))-(( X+40),( Y+20+R))
250 LINE ((-Y+40),( X+40-R))-((-Y+40),( X+20+R))
260 LINE ((-X+40),(-Y+40-R))-((-X+40),(-Y+20+R))
270 LINE (( Y+40),(-X+40-R))-(( Y+40),(-X+20+R))
280 FOR I = 1 TO 40 : NEXT I
290 T = T+0.15
300 GOTO 120</syntaxhighlight>
 
=={{header|C}}==
Rotating wireframe cube in [https://www.opengl.org/ OpenGL], windowing implementation via [http://freeglut.sourceforge.net/ freeglut]
<syntaxhighlight lang="c">
<lang C>
#include<gl/freeglut.h>
 
Line 504 ⟶ 719:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Drawing;
using System.Drawing.Drawing2D;
Line 604 ⟶ 819:
}
}
}</langsyntaxhighlight>
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
Line 614 ⟶ 829:
{{libheader| System.Classes}}
{{Trans|Javascript}}
<syntaxhighlight lang="delphi">
<lang Delphi>
unit main;
 
Line 722 ⟶ 937:
end;
 
end.</langsyntaxhighlight>
Resource Form
<syntaxhighlight lang="delphi">
<lang Delphi>
object Form1: TForm1
OnCreate = FormCreate
Line 732 ⟶ 947:
end
end
</syntaxhighlight>
</lang>
 
 
Line 739 ⟶ 954:
Draws only the visible edges
 
[https://easylang.dev/show/#cod=jZTbboMwDIbv8xSWdteqaITSg7Q9ScUFKmkVCcJG0Ub29LNN0gTGtqZVTe3Pvx0TMG2lTsWpgFc44WeTum9x/3O/jv3eHeMRHcGeLYSqrnGpFCQDErZst5CxzZxGDju2OziwPcCe7R4jo3Du8ncub+90DlzuCcRb157hdi5rBRdIIBEAcGk70NhCCn0LtTJg3AQo6OKVj2fOiYsxXZyqAlavcHEBlkxEMpbq2r7sFZTmOtCP9TVv2gwoiYZj5Dq3N3KhubswbANlHWUDZR/pn1R9r6l3fkXOzDsDhdEBVmNPG4RX3LHDbJQrY8Evn7Lm7Cgl4IjZEbNB2c6wrAhqFtVshNFseZcVHpxsHHPVlZ9+tOdalR1dNKhxPB75kuazSR+Zlr7EXbxAE254szQzr66n95+UaPDLNf3BDzXZ40ff6CCO29QDOT0ho8pYYu1q4FJ1JCT/FUp/FQp7oM7NdFzV5MH4a1vwc2eYbXQBmDjpc3SHNFxD6qcdBKY947KLkJwpyTkkF5QWoZlS034oamwN+TPVJjsBam0U1RsBOQOS6XgTMb6IZC7ca2KbQ9mXBm7vXQ9S0KkWLT37usEwZTkwBZZlIBHf Run it]
[https://easylang.online/apps/_rotating-cube.html Run it]
 
<syntaxhighlight lang="text">
<lang>node[][] &= [ -1 -1 -1 ]
node[][] &= [ [ -1 -1 -1 ] [ -1 -1 1 ] [ -1 1 -1 ] [ -1 1 1 ] [ 1 -1 -1 ] [ 1 -1 1 ] [ 1 1 -1 ] [ 1 1 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 ] ]
node[][] &= [ -1 1 -1 ]
node[][] &= [ -1 1 1 ]
node[][] &= [ 1 -1 -1 ]
node[][] &= [ 1 -1 1 ]
node[][] &= [ 1 1 -1 ]
node[][] &= [ 1 1 1 ]
#
proc scale f . .
edge[][] &= [ 0 1 ]
for i = 1 to len node[][]
edge[][] &= [ 1 3 ]
for d = 1 to 3
edge[][] &= [ 3 2 ]
edge node[i][d] &*= [ 2 0 ]f
edge[][] &= [ 4 5 ] .
.
edge[][] &= [ 5 7 ]
edge[][] &= [ 7 6 ]
edge[][] &= [ 6 4 ]
edge[][] &= [ 0 4 ]
edge[][] &= [ 1 5 ]
edge[][] &= [ 2 6 ]
edge[][] &= [ 3 7 ]
#
func scale f . .
for i range len node[][]
swap n[] node[i][]
n[0] *= f
n[1] *= f
n[2] *= f
swap n[] node[i][]
.
.
funcproc rotate angx angy . .
sinx = sin angx
cosx = cos angx
siny = sin angy
cosy = cos angy
for i range= 1 to len node[][]
swap n[] x = node[i][1]
x z = nnode[i][03]
y = nnode[i][1] = x * cosx - z * sinx
z y = nnode[i][2]
n[0] z = xz * cosx -+ zx * sinx
n node[i][2] = zy * cosxcosy +- xz * sinxsiny
node[i][3] = z * cosy + y * siny
z = n[2]
.
n[1] = y * cosy - z * siny
n[2] = z * cosy + y * siny
swap n[] node[i][]
.
.
len nd[] 3
funcproc draw . .
clear
clear_screen
m = 999
mi = -1
for i range= 1 to len node[][]
if node[i][23] < m
m = node[i][23]
mi = i
.
.
ix = 0
for i range len edge[][]
if edge[i][0] = mi
nd[ix] = edge[i][1]
ix += 1
elif edge[i][1] = mi
nd[ix] = edge[i][0]
ix += 1
.
.
for ni range len nd[]
for i range len edge[][]
if edge[i][0] = nd[ni] or edge[i][1] = nd[ni]
x1 = node[edge[i][0]][0]
y1 = node[edge[i][0]][1]
x2 = node[edge[i][1]][0]
y2 = node[edge[i][1]][1]
move_pen x1 + 50 y1 + 50
draw_line x2 + 50 y2 + 50
.
.
. 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
.
.
.
.
call scale 25
call rotate 45 atan sqrt 2
call draw
on animate
call rotate 1 0
call draw
.
.</lang>
</syntaxhighlight>
 
=={{header|Evaldraw}}==
 
Based on the solution in draw cuboid.
Draws a filled cube with a texture on each face.
 
[[File:Evaldraw cube rotate.gif|thumb|alt=Rotating 3D cube|Rotating cube with texture. Makes use of rudimentary glBegin(GL_QUADS) function]]
 
<syntaxhighlight lang="C">
// We can define our own vec3 struct
struct vec3{x,y,z;}
static modelMatrix[9];
() {
cls(0x828282); // clear screen
clz(1e32); // clear depth buffer
setcam(0,0,-3,0,0); // set camera some units back
// create two local arrays to hold rotation matrices
double roty[9], rotz[9];
static otim;
tim=klock(0); dt=tim-otim; otim=tim;
static degrees = 0;
degrees+=200*dt;
rads = degrees/180*pi;
rotateZ( rotz, rads );
rotateY( roty, rads );
// 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);
glSetTex("cloud.png");
drawcuboid(0,0,0,1,1,1);
}
 
drawcuboid(x,y,z,sx,sy,sz) {
glBegin(GL_QUADS);
setcol(192,32,32);
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(32,192,32);
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(32,32,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,32);
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,32,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(32,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);
}
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|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define PI 3.14159265358979323
#define SCALE 50
#define SIZE 320
Line 865 ⟶ 1,176:
drawcube x(),0
wend
end</langsyntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
[[File:rotating_cube.jpg|200px|thumb|right]]
include "Tlbx SceneKit.incl"
Among the capabilities of FutureBasic (or FB as it's called by its developers) is the ability to compile Open GL code as demonstrated here.
 
_window = 1
<lang futurebasic>
begin enum output 1
include "Tlbx agl.incl"
_sceneView
include "Tlbx glut.incl"
end enum
 
local fn RotatingCubeScene as SCNSceneRef
output file "Rotating Cube"
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( 4.0, 4.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 SCNActionRepeatActionForever( fn SCNActionRotateByAngle( M_PI, fn SCNVector3Make( 0.0, 25.0, 5.0 ), 5.0 ) ) )
end fn = scene
 
void local fn AnimateCubeBuildWindow
window _window, @"Rosetta Code Rotating Cube", ( 0, 0, 600, 600 )
'~'1
scnview _sceneView, fn RotatingCubeScene, ( 0, 0, 600, 600 )
begin globals
SCNViewSetBackgroundColor( _sceneView, fn ColorBlack )
dim as double  sRotation
SCNViewSetAllowsCameraControl( _sceneView, YES )
end globals
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
// Speed of rotation
select (ev)
sRotation += 2.9
case _windowWillClose : end
glMatrixMode( _GLMODELVIEW )
end select
 
glLoadIdentity()
glTranslated( 0.0, 0.0, 0.0 )
glRotated( sRotation, -0.45, -0.8, -0.6 )
glColor3d( 1.0, 0.0, 0.3 )
glLineWidth( 1.5 )
glutWireCube( 1.0 )
end fn
 
on dialog fn DoDialog
// Main program
dim as GLint           attrib(2)
dim as CGrafPtr        port
dim as AGLPixelFormat  fmt
dim as AGLContext      glContext
dim as EventRecord     ev
dim as GLboolean       yesOK
 
fn BuildWindow
window 1, @"Rotating Cube", (0,0) - (500,500)
 
attrib(0) = _AGLRGBA
attrib(1) = _AGLDOUBLEBUFFER
attrib(2) = _AGLNONE
 
fmt = fn aglChoosePixelFormat( 0, 0, attrib(0) )
glContext = fn aglCreateContext( fmt, 0 )
aglDestroyPixelFormat( fmt )
 
port = window( _wndPort )
yesOK = fn aglSetDrawable( glContext, port )
yesOK = fn aglSetCurrentContext( glContext )
 
glClearColor( 0.0, 0.0, 0.0, 0.0 )
 
poke long event - 8, 1
do
glClear( _GLCOLORBUFFERBIT )
fn AnimateCube
aglSwapBuffers( glContext )
HandleEvents
</syntaxhighlight>
until gFBQuit
{{output}}
</lang>
[[File:FutureBasic Rotating Cube.png]]
 
=={{header|Go}}==
As of Go 1.9, it looks as if the only standard library supporting animated graphics is image/gif - so we create an animated GIF...
<langsyntaxhighlight Golang="go">package main
 
import (
Line 1,034 ⟶ 1,362:
}
return x
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
This implementation compiles to JavaScript that runs in a browser using the [https://github.com/ghcjs/ghcjs ghcjs compiler ] . The [https://github.com/reflex-frp/reflex-dom reflex-dom ] library is used to help with svg rendering and animation.
 
<langsyntaxhighlight Haskelllang="haskell">{-# LANGUAGE RecursiveDo #-}
import Reflex.Dom
import Data.Map as DM (Map, lookup, insert, empty, fromList)
Line 1,218 ⟶ 1,546:
elDynAttrSVG a2 a3 a4 = do
elDynAttrNS' (Just "http://www.w3.org/2000/svg") a2 a3 a4
return ()</langsyntaxhighlight>
 
Link to live demo: https://dc25.github.io/drawRotatingCubeHaskell/
 
=={{header|J}}==
 
Derived from J's [https://github.com/jsoftware/demos_qtdemo/blob/master/shader.ijs qt shader demo]:
 
<syntaxhighlight lang="j">require'gl2 gles ide/qt/opengl'
coinsert'jgl2 jgles qtopengl'
 
rotcube=: {{
if.0=nc<'sprog'do.return.end.
fixosx=. 'opengl';'opengl',('DARWIN'-:UNAME)#' version 4.1'
wd 'pc rot; minwh 300 300; cc cube opengl flush' rplc fixosx
HD=: ".wd 'qhwndc cube'
wd 'ptimer 17; pshow'
}}
 
rot_close=: {{
wd 'ptimer 0'
glDeleteBuffers ::0: 2; vbo
glDeleteProgram ::0: sprog
erase 'sprog'
wd 'pclose'
}}
 
cstr=: {{if.y do.memr y,0 _1 2 else.EMPTY end.}}
gstr=: {{cstr>{.glGetString y}}
diag=: {{p[echo y,': ',p=.gstr".y}}
 
blitf=: {{
dat=. 1 fc,y NB. short floats
glBindBuffer GL_ARRAY_BUFFER; x{vbo
glBufferData GL_ARRAY_BUFFER; (#dat); (symdat<'dat'); GL_STATIC_DRAW
}}
 
rot_cube_initialize=: {{
erase'sprog'
if.0=#diag 'GL_VERSION' do.echo 'cannot retrieve GL_VERSION' return.end.
diag each;:'GL_VENDOR GL_RENDERER GL_SHADING_LANGUAGE_VERSION'
GLSL=:wglGLSL''
wglPROC''
'err program'=. gl_makeprogram VSRC ;&fixversion FSRC
if.#err do. echo 'err: ', err return.end.
if. GLSL>120 do.vao=: >{:glGenVertexArrays 1;,_1 end.
assert _1~:vertexAttr=: >{.glGetAttribLocation program;'vertex'
assert _1~:colorAttr=: >{.glGetAttribLocation program;'color'
assert _1~:mvpUni=: >{.glGetUniformLocation program;'mvp'
vbo=: >{:glGenBuffers 2;2#_1
0 blitf vertexData
1 blitf colorData
sprog=: program
}}
 
VSRC=: {{)n
#version $version
$v_in $highp vec3 vertex;
$v_in $lowp vec3 color;
$v_out $lowp vec4 v_color;
uniform mat4 mvp;
void main(void) {
gl_Position= mvp * vec4(vertex,1.0);
v_color= vec4(color,1.0);
}
}}
 
FSRC=: {{)n
#version $version
$f_in $lowp vec4 v_color;
$fragColor
void main(void) {
$gl_fragColor= v_color;
}
}}
 
fixversion=: {{
NB. cope with host shader language version
r=. '$version';GLSL,&":;(GLSL>:300)#(*GLES_VERSION){' core';' es'
f1=. GLSL<:120
r=.r, '$v_in';f1{'in';'attribute'
r=.r, '$v_out';f1{'out';'varying'
r=.r, '$f_in';f1{'in';'varying'
r=.r, '$highp ';f1#(*GLES_VERSION)#'highp'
r=.r, '$lowp ';f1#(*GLES_VERSION)#'lowp'
f2=.(330<:GLSL)+.(300<:GLSL)**GLES_VERSION
r=.r, '$gl_fragColor';f2{'gl_FragColor';'fragColor'
r=.r, '$fragColor';f2#'out vec4 fragColor;'
y rplc r
}}
 
rot_timer=: {{
try.
gl_sel HD
gl_paint''
catch.
echo 'error in rot_timer',LF,13!:12''
wd'ptimer 0'
end.
}}
 
zeroVAttr=: {{
glEnableVertexAttribArray y
glBindBuffer GL_ARRAY_BUFFER; x{vbo
glVertexAttribPointer y; 3; GL_FLOAT; 0; 0; 0
}}
 
mp=: +/ .*
ref=: (gl_Translate 0 0 _10) mp glu_LookAt 0 0 1,0 0 0,1 0 0
rot_cube_paint=: {{
try.
if.nc<'sprog' do.return.end.
wh=. gl_qwh''
glClear GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT [glClearColor 0 0 0 0+%3
glUseProgram sprog
glEnable each GL_DEPTH_TEST, GL_CULL_FACE, GL_BLEND
glBlendFunc GL_SRC_ALPHA; GL_ONE_MINUS_SRC_ALPHA
mvp=. (gl_Rotate (360|60*6!:1''),1 0 0)mp ref mp gl_Perspective 30, (%/wh),1 20
glUniformMatrix4fv mvpUni; 1; GL_FALSE; mvp
if. GLSL>120 do. glBindVertexArray {.vao end.
0 zeroVAttr vertexAttr
1 zeroVAttr colorAttr
glDrawArrays GL_TRIANGLES; 0; 36
glUseProgram 0
catch.
echo 'error in rot_cube_paint',LF,13!:12''
wd'ptimer 0'
end.
}}
 
NB. oriented triangle representation of unit cube
unitCube=: #:(0 1 2, 2 1 3)&{@".;._2 {{)n
2 3 0 1 NB. unit cube corner indices
3 7 1 5 NB. 0: origin
4 0 5 1 NB. 1, 2, 4: unit distance along each axis
6 2 4 0 NB. 3, 5, 6, 7: combinations of axes
7 6 5 4
7 3 6 2
}}
 
NB. orient cube so diagonal is along first axis
daxis=: (_1^5 6 e.~i.3 3)*%:6%~2 0 4,2 3 1,:2 3 1
vertexData=:(_1^unitCube)mp daxis NB. cube with center at origin
colorData=: unitCube NB. corresponding colors
 
rotcube''</syntaxhighlight>
 
A variation which did not use opengl would probably be much more concise.
 
=={{header|Java}}==
[[File:rotating_cube_java.png|200px|thumb|right]]
<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.ActionEvent;
import static java.lang.Math.*;
Line 1,315 ⟶ 1,788:
});
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
{{trans|Java}}
<langsyntaxhighlight lang="javascript"><!DOCTYPE html>
<html lang="en">
<head>
Line 1,407 ⟶ 1,880:
 
</body>
</html></langsyntaxhighlight>
 
=={{header|Julia}}==
Run at the Julia REPL command line.
<langsyntaxhighlight Julialang="julia">using Makie, LinearAlgebra
 
N = 40
Line 1,423 ⟶ 1,896:
Makie.rotate!(rect, Quaternionf0(arr[1], arr[2], arr[3], arr[4]))
sleep(interval)
end</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1
 
import java.awt.*
Line 1,527 ⟶ 2,000:
f.isVisible = true
}
}</langsyntaxhighlight>
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local abs,atan,cos,floor,pi,sin,sqrt = math.abs,math.atan,math.cos,math.floor,math.pi,math.sin,math.sqrt
local bitmap = {
init = function(self, w, h, value)
Line 1,621 ⟶ 2,094:
screen:clear()
bitmap:render()
end</langsyntaxhighlight>
{{out}}
<pre style="font-size:50%">Frame 1:
Line 1,705 ⟶ 2,178:
················································································
················································································</pre>
 
=={{header|M2000 Interpreter}}==
[[File:Cube.png|thumb|Cube Example]]
Draw on M2000 console. Using of GDI+ for smooth lines. Using Every {} structure and Refresh 100 to immediate refresh double buffer, and set 100ms for the next auto refresh. So we erase the screen without refresh and draw again after dt time. Cube has 6 more lines for fancy drawing. Also time displayed. We can move the cube (and accelarate the rotation as we press a mouse button).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<syntaxhighlight lang="m2000 interpreter">
Module Cube3D {
form 80, 32
smooth on // enable GDI+ smooth lines
zoff=0.5773502691896257645091487805019574556@
cylr=1.6329931618554520654648560498039275946@
oX=scale.x div 2 : oY=scale.y div 2
SCALE=min.data(oX, oY)/2*.6
gradient 0
theta = 0.0 : dtheta = 1.5 : dt = 1000/60
ScZof=SCALE/zoff
ScZofM=SCALE*zoff
dim cylphi(), x()
c =(PI/6, 5*PI/6, 3*PI/2, 11*PI/6, PI/2, 7*PI/6) : c*=180/Pi : cylphi()=c // cos() take Degree
every DT {
if mouse then (oX, oY)=(mouse.x,mouse.y) : dtheta*=1.05 else dtheta = 1.5
dim x(6)=oX : for i=0 to 5: x(i) += SCALE*cylr*cos(cylphi(i)+theta):next
drawcube() : refresh 100 : IF keypress(32) then exit
theta += dtheta : gradient 0, 5: cursor 0,0
Print "Press space to exit",,"Press any mouse button to move cube",,Time$(Now)
}
sub drawcube()
for i= 0 to 2
move x(i), oY-ScZofM:draw to oX,oY-ScZof, 11
move oX,ScZof+oY:draw to x(i),oY-ScZofM, 9
move oX,oY+ ScZof:draw to x(5-i),ScZofM+oY
move x(i),oY-ScZofM:draw to x(i mod 3 + 3),oY+ScZofM, 15
move oX,oY-ScZof:draw to x(i mod 3 + 3), oY+ScZofM, 7
move x(i),oY-ScZofM:draw to x((i+1) mod 3 + 3),oY+ScZofM, 13
next
end sub
}
Cube3D
</syntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">plots:-display(
seq(
plots:-display(
Line 1,713 ⟶ 2,241:
axes=none, scaling=constrained, orientation=[0,45,i] ),
i = 0..360, 20 ),
insequence=true );</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Dynamic[
Graphics3D[
GeometricTransformation[
Line 1,722 ⟶ 2,250:
RotationTransform[Clock[2 Pi], {0, 0, 1}]
],
Boxed -> False]]</langsyntaxhighlight>
 
=={{header|MiniScript}}==
{{works with|Mini Micro}}
 
<syntaxhighlight lang="MiniScript">import "mathUtil"
scale = 250
radius = sqrt(scale^2)
 
Face = new Sprite
Face.image = file.loadImage("/sys/pics/shapes/SquareThin.png")
 
clear; gfx.clear color.gray
sprites = display(4).sprites
 
// build a sprite for each side
for i in range(0, 3)
sp = new Face
sp.x = 480; sp.y = 320
yBot = -sin(pi/4)
yTop = sin(pi/4)
cosAngL = cos(i*pi/2); sinAngL = sin(i*pi/2)
cosAngR = cos((i+1)*pi/2); sinAngR = sin((i+1)*pi/2)
sp.corners3d = [
[cosAngL, yBot, sinAngL], [cosAngR, yBot, sinAngR],
[cosAngR, yTop, sinAngR], [cosAngL, yTop, sinAngL] ]
sp.color = [color.yellow, color.aqua, color.pink, color.lime][i]
sprites.push sp
end for
// ...and one for the top
top = new Face
top.x = 480; top.y = 320
top.corners3d = []
for i in range(0, 3)
top.corners3d.push [cos(i*pi/2), sin(pi/4), sin(i*pi/2)]
end for
sprites.push top
 
// 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 + z*0]
p = (8 - z) / 8 // (perspective factor)
return mathUtil.lerp2d(result, [480,800], 1-p)
end function
 
// Position all the sprites where they should be on screen for the given rotation.
positionSprites = function(rotDegrees)
for sp in sprites
corners = []
for i in range(0,3)
corners.push rotateAndProject(sp.corners3d[i], rotDegrees)
end for
sp.setCorners corners
if sp == top then continue
if corners[1][0] > corners[0][0] then
sp.tint = sp.color
else
sp.tint = color.clear
end if
end for
end function
 
// Main program
rot = 0
while not key.pressed("escape") and not key.pressed("q")
yield
positionSprites rot
rot = rot + 1
end while
key.clear</syntaxhighlight>
 
{{output}}
[[File:MiniScript-spnning-cube.gif|alt=MiniScript spinning cube solution|MiniScript spinning cube solution]]
 
=={{header|Nim}}==
{{trans|Ada}}
{{libheader|SDL2}}
<langsyntaxhighlight Nimlang="nim">import math
import sdl2
 
Line 1,807 ⟶ 2,415:
while not endSimulation:
endSimulation = drawCube()
window.destroy()</langsyntaxhighlight>
 
=={{header|Objeck}}==
{{libheader|SDL2}}
<lang objeck>
{{trans|Ada}}
<syntaxhighlight lang="objeck">#~
Rotating Cube
~#
 
use Collection.Generic;
use Game.SDL2;
Line 1,854 ⟶ 2,467:
# process input
while(e->Poll() <> 0) {
# joystick
if(e->GetType() = EventType->SDL_QUIT) {
quit := true;
Line 1,890 ⟶ 2,502:
cosY := angleY->Cos();
node_sizes := @nodes->Size();
size := node_sizes[0];
 
Line 1,929 ⟶ 2,541:
SCREEN_HEIGHT := 600,
DRAW_OFFSET := 300
}</langsyntaxhighlight>
 
=={{header|OxygenBasic}}==
Using An OpenGl-based console
<syntaxhighlight lang="text">
% Title "Rotating Cube"
% Animated
% PlaceCentral
uses ConsoleG
 
sub main
========
cls 0.0, 0.5, 0.7
shading
scale 7
pushstate
GoldMaterial.act
static float ang
rotateX ang
rotateY ang
go cube
popstate
ang+=.5 : if ang>=360 then ang-=360
end sub
 
EndScript
</syntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # http://www.rosettacode.org/wiki/Draw_a_rotating_cube
Line 1,962 ⟶ 2,600:
$c->createLine( $mid, $height, @points[10, 11], -width => 5,);
$mw->after($wait, \&draw);
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
<lang Phix>-- demo\rosetta\DrawRotatingCube.exw
You can run this online [http://phix.x10.mx/p2js/drawrotatingcube.htm here].
include pGUI.e
<!--<syntaxhighlight lang="phix">(phixonline)-->
 
<span style="color: #000080;font-style:italic;">--
Ihandle canvas
-- demo\rosetta\DrawRotatingCube.exw
cdCanvas cd_canvas
-- =================================
 
--
-- credits: http://petercollingridge.appspot.com/3D-tutorial/rotating-objects
-- define 8 corners equidistant from {0,0,0}:
-- https://github.com/ssloy/tinyrenderer/wiki/Lesson-4:-Perspective-projection
--
--
-- 6-----2
-- Aside: low CPU usage, at least when using a 30ms timer (33 FPS, which is plenty).
-- 5-----1 3
--</span>
-- 8-----4
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
--
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
-- ie the right face is 1-2-3-4 clockwise, and the left face
-- is 5-6-7-8 counter-clockwise (unless using x-ray vision).
<span style="color: #008080;">constant</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Draw a Rotating Cube"</span>
--
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
enum X, Y, Z
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cd_canvas</span>
constant l = 100
constant corners = {{+l,+l,+l},
<span style="color: #000080;font-style:italic;">--
{+l,+l,-l},
-- First, define 8 corners equidistant from {0,0,0}:
{+l,-l,-l},
--
{+l,-l,+l},
-- {6-----l,+l,+l},2
-- 5-----1 {-l,+l,-l},3
-- {8-l,-l,-l},--4
--
{-l,-l,+l}}
-- ie the right face is 1-2-3-4 clockwise, and the left face
 
-- is 5-6-7-8 counter-clockwise (unless using x-ray vision).
constant faces = {{CD_RED, 1,2,3,4}, -- right
-- (since this is not drawing textures, clockwise-ness does
{CD_YELLOW, 1,5,6,2}, -- top
-- not matter, as shown by the corrected orange face, but
{CD_GREEN, 1,4,8,5}, -- front
-- it will if you (figure out how to) apply any textures.)
{CD_BLUE, 2,3,7,6}, -- back
-- (a quick (online) study of opengl texture documentation
{CD_WHITE, 3,4,8,7}, -- btm
-- should convince you that stuff is best left to opengl.)
{CD_ORANGE, 5,6,7,8}} -- left
--</span>
 
<span style="color: #008080;">enum</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>
atom ry = 0 -- rotation angle, 0..359, on a timer
<span style="color: #008080;">constant</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span>
 
<span style="color: #008080;">constant</span> <span style="color: #000000;">corners</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 1 (front top right)</span>
constant naxes = {{Y,Z}, -- (rotate about the X-axis)
<span style="color: #0000FF;">{+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 2 (back top "right")</span>
{X,Z}, -- (rotate about the Y-axis)
<span style="color: #0000FF;">{+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 3 (back btm "right")</span>
{X,Y}} -- (rotate about the Z-axis)
<span style="color: #0000FF;">{+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 4 (front btm right)</span>
 
<span style="color: #0000FF;">{-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 5 (front top left)</span>
function rotate(sequence points, atom angle, integer axis)
<span style="color: #0000FF;">{-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 6 (back top "left")</span>
--
<span style="color: #0000FF;">{-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- 7 (back btm "left")</span>
-- rotate points by the specified angle about the given axis
<span style="color: #0000FF;">{-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">l</span><span style="color: #0000FF;">}}</span> <span style="color: #000080;font-style:italic;">-- 8 (front btm left)
--
-- I put left/right in quotes for the back face as a reminder
atom radians = angle*CD_DEG2RAD,
-- those match the above diagram, but of course they would be
sin_t = sin(radians),
-- swapped were you looking "at" the face/rotated it by 180.</span>
cos_t = cos(radians)
integer {nx,ny} = naxes[axis]
<span style="color: #008080;">constant</span> <span style="color: #000000;">faces</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #004600;">CD_RED</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;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- right</span>
for i=1 to length(points) do
<span style="color: #0000FF;">{</span><span style="color: #004600;">CD_YELLOW</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- top</span>
atom x = points[i][nx],
<span style="color: #0000FF;">{</span><span style="color: #004600;">CD_DARK_GREEN</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- front</span>
y = points[i][ny]
<span style="color: #0000FF;">{</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- back</span>
points[i][nx] = x * cos_t - y * sin_t
<span style="color: #0000FF;">{</span><span style="color: #004600;">CD_WHITE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- bottom
points[i][ny] = y * cos_t + x * sin_t
-- {CD_ORANGE, 5,6,7,8}} -- left</span>
end for
<span style="color: #0000FF;">{</span><span style="color: #004600;">CD_ORANGE</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}}</span> <span style="color: #000080;font-style:italic;">-- left
return points
end function
-- rotation angles, 0..359, on a timer</span>
 
<span style="color: #004080;">atom</span> <span style="color: #000000;">rx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">45</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- initially makes cube like a H</span>
function projection(sequence points, atom d)
<span style="color: #000000;">ry</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">35</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- " " " italic H</span>
--
<span style="color: #000000;">rz</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
-- project points from {0,0,d} onto the perpendicular plane through {0,0,0}
--
<span style="color: #008080;">constant</span> <span style="color: #000000;">naxes</span> <span style="color: #0000FF;">=</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> <span style="color: #000080;font-style:italic;">-- (rotate about the X-axis)</span>
for i=1 to length(points) do
<span style="color: #0000FF;">{</span><span style="color: #000000;">X</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Z</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- (rotate about the Y-axis)</span>
atom {x,y,z} = points[i]
<span style="color: #0000FF;">{</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: #000080;font-style:italic;">-- (rotate about the Z-axis)</span>
points[i][X] = x/(1-z/d)
points[i][Y] = y/(1-z/d)
<span style="color: #008080;">function</span> <span style="color: #000000;">rotate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">axis</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000080;font-style:italic;">--
return points
-- rotate points by the specified angle about the given axis
end function
--</span>
 
<span style="color: #004080;">atom</span> <span style="color: #000000;">radians</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">*</span><span style="color: #004600;">CD_DEG2RAD</span><span style="color: #0000FF;">,</span>
function nearest(sequence points)
<span style="color: #000000;">sin_t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">radians</span><span style="color: #0000FF;">),</span>
--
<span style="color: #000000;">cos_t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">radians</span><span style="color: #0000FF;">)</span>
-- return the index of the nearest point (highest z value)
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">nx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ny</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">naxes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">axis</span><span style="color: #0000FF;">]</span>
--
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer np = 1
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">nx</span><span style="color: #0000FF;">],</span>
atom maxz = points[1][Z]
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ny</span><span style="color: #0000FF;">]</span>
for i=2 to length(points) do
<span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">nx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">*</span><span style="color: #000000;">cos_t</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">*</span><span style="color: #000000;">sin_t</span>
atom piz = points[i][Z]
<span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ny</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">*</span><span style="color: #000000;">cos_t</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">*</span><span style="color: #000000;">sin_t</span>
if piz>maxz then
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
maxz = piz
<span style="color: #008080;">return</span> <span style="color: #000000;">points</span>
np = i
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end if
end for
<span style="color: #008080;">function</span> <span style="color: #000000;">projection</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
return np
<span style="color: #000080;font-style:italic;">--
end function
-- project points from {0,0,d} onto the perpendicular plane through {0,0,0}
 
--</span>
procedure vertices(integer wx, wh, sequence points, face)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
-- (common code for line/fill drawing)
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</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> <span style="color: #0000FF;">=</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
for i=2 to length(face) do
<span style="color: #000000;">denom</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">z</span><span style="color: #0000FF;">/</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
integer fi = face[i]
<span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">X</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">/</span><span style="color: #000000;">denom</span>
cdCanvasVertex(cd_canvas,wx+points[fi][X],wh-points[fi][Y])
<span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">/</span><span style="color: #000000;">denom</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end procedure
<span style="color: #008080;">return</span> <span style="color: #000000;">points</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure draw_cube(integer wx, wh)
sequence points = corners
<span style="color: #008080;">function</span> <span style="color: #000000;">nearest</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">)</span>
points = rotate(points,45,X) -- (cube should now look like a H)
<span style="color: #000080;font-style:italic;">--
atom zr = 90-arctan(sqrt(2))*CD_RAD2DEG -- (about 35 degrees)
-- return the index of the nearest point (highest z value)
points = rotate(points,zr,Z) -- (cube should now look like an italic H)
--</span>
points = rotate(points,ry,Y) -- (timed, two corners should remain static)
<span style="color: #008080;">return</span> <span style="color: #7060A8;">largest</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Z</span><span style="color: #0000FF;">),</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
points = projection(points,1000)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
integer np = nearest(points)
--
<span style="color: #008080;">procedure</span> <span style="color: #000000;">draw_cube</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cy</span><span style="color: #0000FF;">)</span>
-- find the three faces that contain the nearest point,
<span style="color: #000080;font-style:italic;">-- {cx,cy} is the centre point of the canvas</span>
-- then order by/draw them furthest diag away first.
<span style="color: #004080;">sequence</span> <span style="color: #000000;">points</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">corners</span><span style="color: #0000FF;">)</span>
-- (one of them, and theoretically two but not at the
<span style="color: #000000;">points</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rotate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">X</span><span style="color: #0000FF;">)</span>
-- rotations in use, may be completely obscured, due
<span style="color: #000000;">points</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rotate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">)</span>
-- to the effects of the perspective projection.)
<span style="color: #000000;">points</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rotate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rz</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Z</span><span style="color: #0000FF;">)</span>
--
<span style="color: #000000;">points</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">projection</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)</span>
sequence faceset = {}
<span style="color: #004080;">integer</span> <span style="color: #000000;">np</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nearest</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">)</span>
for i=1 to length(faces) do
<span style="color: #000080;font-style:italic;">--
sequence fi = faces[i]
-- find the three faces that contain the nearest point,
integer k = find(np,fi)
-- then for each of those faces let diag be the point
if k then
-- that is diagonally opposite said nearest point, and
integer diag = mod(k,4)+2
-- order by/draw those faces furthest diag =away fi[diag]first.
-- (one or two of them may be completely obscured due
faceset = append(faceset,{points[diag][Z],i})
-- to the effects of the perspective projection.)
end if
-- (you could of course draw all six faces, as long as
end for
-- the 3 furthest are draw first/obliterated, which
faceset = sort(faceset)
-- is what that commented-out "else" would achieve.)
for i=1 to length(faceset) do
--</span>
integer fdx = faceset[i][2]
<span style="color: #004080;">sequence</span> <span style="color: #000000;">faceset</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
sequence fi = faces[fdx]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">faces</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
cdCanvasSetForeground(cd_canvas,fi[1])
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">faces</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
-- draw edges (anti-aliased)
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">np</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fi</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- k:=2..5, or 0</span>
cdCanvasBegin(cd_canvas,CD_CLOSED_LINES)
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
vertices(wx,wh,points,fi)
<span style="color: #004080;">integer</span> <span style="color: #000000;">diag</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">2</span> <span style="color: #000080;font-style:italic;">-- {2,3,4,5} --&gt; {4,5,2,3}
cdCanvasEnd(cd_canvas)
-- aka swap 2&lt;=&gt;4 & 3&lt;=&gt;5</span>
-- fill sides (else would get bresenham edges)
<span style="color: #000000;">diag</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fi</span><span style="color: #0000FF;">[</span><span style="color: #000000;">diag</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- 1..8, diagonally opp. np</span>
cdCanvasBegin(cd_canvas,CD_FILL)
<span style="color: #000000;">faceset</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">faceset</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">diag</span><span style="color: #0000FF;">][</span><span style="color: #000000;">Z</span><span style="color: #0000FF;">],</span><span style="color: #000000;">i</span><span style="color: #0000FF;">})</span>
vertices(wx,wh,points,fi)
<span style="color: #000080;font-style:italic;">-- else
cdCanvasEnd(cd_canvas)
-- faceset = append(faceset,{-9999,i})</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #000000;">faceset</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">faceset</span><span style="color: #0000FF;">)</span>
function canvas_action_cb(Ihandle canvas)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">faceset</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
cdCanvasActivate(cd_canvas)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">face</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">faces</span><span style="color: #0000FF;">[</span><span style="color: #000000;">faceset</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]]</span>
cdCanvasClear(cd_canvas)
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">,</span><span style="color: #000000;">face</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
integer {wx, wh} = sq_floor_div(IupGetIntInt(canvas, "DRAWSIZE"),2)
<span style="color: #000080;font-style:italic;">-- first fill sides (with bresenham edges), then
draw_cube(wx,wh)
-- redraw edges, but anti-aliased aka smoother</span>
cdCanvasFlush(cd_canvas)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">modes</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #004600;">CD_FILL</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_CLOSED_LINES</span><span style="color: #0000FF;">}</span>
return IUP_DEFAULT
<span style="color: #008080;">for</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">modes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end function
<span style="color: #7060A8;">cdCanvasBegin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">,</span><span style="color: #000000;">modes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">])</span>
 
<span style="color: #008080;">for</span> <span style="color: #000000;">fdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
function canvas_map_cb(Ihandle canvas)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">pt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">points</span><span style="color: #0000FF;">[</span><span style="color: #000000;">face</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fdx</span><span style="color: #0000FF;">]]</span>
atom res = IupGetDouble(NULL, "SCREENDPI")/25.4
<span style="color: #7060A8;">cdCanvasVertex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">X</span><span style="color: #0000FF;">],</span><span style="color: #000000;">cy</span><span style="color: #0000FF;">-</span><span style="color: #000000;">pt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">])</span>
IupGLMakeCurrent(canvas)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
cd_canvas = cdCreateCanvas(CD_GL, "10x10 %g", {res})
<span style="color: #7060A8;">cdCanvasEnd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">)</span>
cdCanvasSetBackground(cd_canvas, CD_PARCHMENT)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return IUP_DEFAULT
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
 
function canvas_unmap_cb(Ihandle canvas)
<span style="color: #008080;">function</span> <span style="color: #000000;">canvas_action_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
cdKillCanvas(cd_canvas)
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
 
<span style="color: #000000;">draw_cube</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
function canvas_resize_cb(Ihandle /*canvas*/)
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">)</span>
integer {canvas_width, canvas_height} = IupGetIntInt(canvas, "DRAWSIZE")
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
atom res = IupGetDouble(NULL, "SCREENDPI")/25.4
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
cdCanvasSetAttribute(cd_canvas, "SIZE", "%dx%d %g", {canvas_width, canvas_height, res})
return IUP_DEFAULT
<span style="color: #008080;">function</span> <span style="color: #000000;">canvas_map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">IupGLMakeCurrent</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
function timer_cb(Ihandle /*ih*/)
<span style="color: #000000;">cd_canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
ry = mod(ry+359,360)
<span style="color: #008080;">else</span>
IupRedraw(canvas)
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetDouble</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"SCREENDPI"</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">25.4</span>
return IUP_IGNORE
<span style="color: #000000;">cd_canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_GL</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"10x10 %g"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_PARCHMENT</span><span style="color: #0000FF;">)</span>
procedure main()
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
IupOpen()
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
IupImageLibOpen()
canvas = IupGLCanvas()
<span style="color: #008080;">function</span> <span style="color: #000000;">canvas_resize_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*canvas*/</span><span style="color: #0000FF;">)</span>
IupSetAttribute(canvas, "RASTERSIZE", "640x480")
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">canvas_width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas_height</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
IupSetCallback(canvas, "ACTION", Icallback("canvas_action_cb"))
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetDouble</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"SCREENDPI"</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">25.4</span>
IupSetCallback(canvas, "MAP_CB", Icallback("canvas_map_cb"))
<span style="color: #7060A8;">cdCanvasSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cd_canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"SIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%dx%d %g"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">canvas_width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas_height</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
IupSetCallback(canvas, "UNMAP_CB", Icallback("canvas_unmap_cb"))
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
IupSetCallback(canvas, "RESIZE_CB", Icallback("canvas_resize_cb"))
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Ihandle dlg = IupDialog(IupVbox({canvas}))
IupSetAttribute(dlg,"TITLE","Draw a Rotating Cube");
<span style="color: #008080;">function</span> <span style="color: #000000;">timer_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandln</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
IupShow(dlg)
<span style="color: #000080;font-style:italic;">-- (feel free to add a bit more randomness here, maybe)</span>
IupSetAttribute(canvas, "RASTERSIZE", NULL)
<span style="color: #000000;">rx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">359</span><span style="color: #0000FF;">,</span><span style="color: #000000;">360</span><span style="color: #0000FF;">)</span>
Ihandle hTimer = IupTimer(Icallback("timer_cb"), 40)
<span style="color: #000000;">ry</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">+</span><span style="color: #000000;">359</span><span style="color: #0000FF;">,</span><span style="color: #000000;">360</span><span style="color: #0000FF;">)</span>
IupMainLoop()
<span style="color: #000000;">rz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rz</span><span style="color: #0000FF;">+</span><span style="color: #000000;">359</span><span style="color: #0000FF;">,</span><span style="color: #000000;">360</span><span style="color: #0000FF;">)</span>
IupClose()
<span style="color: #7060A8;">IupRedraw</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
main()</lang>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGLCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=640x480"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"canvas_action_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"canvas_map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"RESIZE_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"canvas_resize_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="%s"`</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">title</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">hTimer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupTimer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"timer_cb"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">30</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|PostScript}}==
'''Don't send this to your printer!'''
<langsyntaxhighlight lang="postscript">%!PS-Adobe-3.0
%%BoundingBox: 0 0 400 400
 
Line 2,193 ⟶ 2,850:
 
0 {3.2 add dup page } loop
%%EOF</langsyntaxhighlight>
 
=={{header|Processing}}==
Create a cube in Processing with box(), rotate the scene with rotate(), and drive the rotation with either the built-in millis() or frameCount timers.
 
<langsyntaxhighlight Processinglang="processing">void setup() {
size(500, 500, P3D);
}
Line 2,214 ⟶ 2,871:
// draw box
box(300, 300, 300);
}</langsyntaxhighlight>
 
=={{header|Python}}==
Line 2,223 ⟶ 2,880:
 
====Short version====
<langsyntaxhighlight lang="python">from visual import *
scene.title = "VPython: Draw a rotating cube"
 
Line 2,241 ⟶ 2,898:
rate(50)
cube.rotate( angle=0.005, axis=(0,1,0) )
</syntaxhighlight>
</lang>
 
<!--
Line 2,248 ⟶ 2,905:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket/gui
(require math/matrix math/array)
 
Line 2,317 ⟶ 2,974:
(send c refresh))
 
(define t (new timer% [notify-callback refresh] [interval 35] [just-once? #f]))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 2,324 ⟶ 2,981:
Raku has no native graphics libraries built in, but makes it fairly easy to bind to third party libraries. Here we'll use bindings to [[wp:Libcaca|Libcaca]], the '''C'''olor '''A'''S'''C'''II '''A'''rt library to generate a rotating cube in an ASCII terminal.
 
<syntaxhighlight lang="raku" perl6line>use Terminal::Caca;
given my $canvas = Terminal::Caca.new {
.title('Rosetta Code - Rotating cube - Press any key to exit');
Line 2,416 ⟶ 3,073:
.cleanup;
}
}</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
#===================================================================#
# Based on Original Sample from RayLib (https://www.raylib.com/)
Line 2,497 ⟶ 3,154:
 
CloseWindow()
</syntaxhighlight>
</lang>
[https://www.mediafire.com/view/xjimb82x8lvfwd9/RotatingCube.jpg/file Rotating a Cube]
 
Line 2,505 ⟶ 3,162:
{{libheader|Scala GUI Animation}}
{{works with|Scala|2.13}}
<langsyntaxhighlight Scalalang="scala">import java.awt.event.ActionEvent
import java.awt._
 
Line 2,592 ⟶ 3,249:
}
 
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 2,603 ⟶ 3,260:
See [http://wiki.tcl.tk/14283 this wiki page] (and others linked from it) for many similar examples.
 
<langsyntaxhighlight Tcllang="tcl"># matrix operation support:
package require math::linearalgebra
namespace import ::math::linearalgebra::matmul
Line 2,735 ⟶ 3,392:
}
set ::world [make_cube 100]
tick</langsyntaxhighlight>
 
=={{header|TI-83 BASIC}}==
<langsyntaxhighlight lang="ti83b">:-1→Xmin:1→Xmax
:-1→Ymin:1→Ymax
:AxesOff
Line 2,760 ⟶ 3,417:
:Line(.3,FV,-.3,-PV
:End
:End</langsyntaxhighlight>
 
I%, PV, and FV are all finance variables that can be found in the finance menu (inside the APPS menu on TI-83+ and up).
Finance variables are much faster than normal variables.
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
 
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]
]
 
class RotatingCube {
construct new(width, height) {
Window.title = "Rotating cube"
Window.resize(width, height)
Canvas.resize(width, height)
_width = width
_height = height
_fore = Color.blue
}
 
init() {
scale(100)
rotateCube(Num.pi / 4, Math.atan(2.sqrt))
drawCube()
}
 
update() {
rotateCube(Num.pi / 180, 0)
}
 
draw(alpha) {
drawCube()
}
 
scale(s) {
for (node in Nodes) {
node[0] = node[0] * s
node[1] = node[1] * s
node[2] = node[2] * s
}
}
 
drawCube() {
Canvas.cls(Color.white)
Canvas.offset(_width / 2, _height / 2)
for (edge in Edges) {
var xy1 = Nodes[edge[0]]
var xy2 = Nodes[edge[1]]
Canvas.line(Math.round(xy1[0]), Math.round(xy1[1]),
Math.round(xy2[0]), Math.round(xy2[1]), _fore)
}
for (node in Nodes) {
Canvas.rectfill(Math.round(node[0]) - 4, Math.round(node[1]) - 4, 8, 8, _fore)
}
}
 
rotateCube(angleX, angleY) {
var sinX = Math.sin(angleX)
var cosX = Math.cos(angleX)
var sinY = Math.sin(angleY)
var cosY = Math.cos(angleY)
for (node in Nodes) {
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
}
}
}
 
var Game = RotatingCube.new(640, 640)</syntaxhighlight>
 
=={{header|XPL0}}==
The main challenge was figuring out the initial coordinates of the cube.
Zometool came to the rescue. The program runs much smoother than the animated gif.
<langsyntaxhighlight XPL0lang="xpl0">def Size=100., Speed=0.05; \drawing size and rotation speed
real X, Y, Z, Farthest; \arrays: 3D coordinates of vertices
int I, J, K, ZI, Edge;
Line 2,796 ⟶ 3,552:
until KeyHit; \run until a key is struck
SetVid(3); \restore normal text mode
]</langsyntaxhighlight>
 
{{out}}
http://www.xpl0.org/rotcube2.gif
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Draw_a_rotating_cube
// adapted to Yabasic by Galileo, 05/2022
 
// GFA Punch (code from tigen.ti-fr.com/)
// Carré 3D en rotation
 
open window 50, 70
backcolor 0,0,0
clear window
color 255,255,255
 
do
clear window
x = COS(T) * 20
y = SIN(T) * 18
r = SIN(T + T)
line (x + 40), (y + 40 - r), (-y + 40), (x + 40 - r)
line (-y + 40), (x + 40 - r), (-x + 40), (-y + 40 - r)
line (-x + 40), (-y + 40 - r), (y + 40), (-x + 40 - r)
line (y + 40), (-x + 40 - r), (x + 40), (y + 40 - r)
line (x + 40), (y + 20 + r), (-y + 40), (x + 20 + r)
line (-y + 40), (x + 20 + r), (-x + 40), (-y + 20 + r)
line (-x + 40), (-y + 20 + r), (y + 40), (-x + 20 + r)
line (y + 40), (-x + 20 + r), (x + 40), (y + 20 + r)
line (x + 40), (y + 40 - r), (x + 40), (y + 20 + r)
line (-y + 40), (x + 40 - r), (-y + 40), (x + 20 + r)
line (-x + 40), (-y + 40 - r), (-x + 40), (-y + 20 + r)
line (y + 40), (-x + 40 - r), (y + 40), (-x + 20 + r)
 
pause 0.02
T = T + 0.15
loop</syntaxhighlight>
 
=={{header|Zig}}==
{{libheader|Raylib}}
{{works with|Zig|0.11.0}} {{works with|Raylib|4.6}}
<syntaxhighlight lang="zig">const std = @import("std");
const c = @cImport({
@cInclude("raylib.h");
@cInclude("rlgl.h");
});
 
const dark_mode = true;
const show_grid = false;
 
pub fn main() !void {
const screen_width = 640;
const screen_height = 360;
 
const cube_side = 1;
const size = c.Vector3{ .x = cube_side, .y = cube_side, .z = cube_side };
const position = c.Vector3{ .x = 0, .y = 0, .z = 0 };
const x_rot = 45;
const y_center: f32 = std.math.sqrt(3.0) * cube_side / 2.0;
const z_rot = std.math.radiansToDegrees(f32, std.math.atan(@as(f32, std.math.sqrt1_2)));
 
c.SetConfigFlags(c.FLAG_WINDOW_RESIZABLE | c.FLAG_VSYNC_HINT);
c.InitWindow(screen_width, screen_height, "Draw a Rotating Cube");
defer c.CloseWindow();
 
var camera = c.Camera{
.position = .{ .x = 3, .y = 3, .z = 3 },
.target = .{ .x = 0, .y = y_center, .z = 0 }, // Center of cube
.up = .{ .x = 0, .y = 1, .z = 0 },
.fovy = 45, // Camera field-of-view Y
.projection = c.CAMERA_PERSPECTIVE,
};
 
c.SetTargetFPS(60);
 
while (!c.WindowShouldClose()) // Detect window close button or ESC key
{
c.UpdateCamera(&camera, c.CAMERA_ORBITAL);
 
c.BeginDrawing();
defer c.EndDrawing();
 
c.ClearBackground(if (dark_mode) c.BLACK else c.RAYWHITE);
{
c.BeginMode3D(camera);
defer c.EndMode3D();
{
c.rlPushMatrix();
defer c.rlPopMatrix();
c.rlTranslatef(0, y_center, 0);
c.rlRotatef(z_rot, 0, 0, 1);
c.rlRotatef(x_rot, 1, 0, 0);
c.DrawCubeWiresV(position, size, if (dark_mode) c.LIME else c.BLACK);
}
if (show_grid) c.DrawGrid(12, 0.75);
}
}
}</syntaxhighlight>
 
[[Category:Geometry]]
59

edits