Draw a rotating cube: Difference between revisions

m
→‎Zig: Update "works with". Simplify two lines involving casting and math.
(Add Zig solution)
m (→‎Zig: Update "works with". Simplify two lines involving casting and math.)
 
(10 intermediate revisions by 6 users not shown)
Line 522:
 
 
=={{header|BASIC256BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">global escala
global tam
Line 567 ⟶ 568:
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}}==
Line 869 ⟶ 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">
Line 875 ⟶ 960:
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 ] ]
#
funcproc scale f . .
for i = 1 to len node[][]
for d = 1 to 3
node[i][d] *= f
.
.
.
funcproc 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
funcproc draw . .
clear
m = 999
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
.
.
. 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
.
</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>
 
Line 3,222 ⟶ 3,425:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 3,391 ⟶ 3,594:
 
=={{header|Zig}}==
{{libheader|Raylib}}
{{works with|Zig|0.11.0dev}} {{works with|Raylib|4.6dev}}
{{works with|Zig|0.11.0}} {{works with|Raylib|4.6}}
<syntaxhighlight lang="zig">const std = @import("std");
const c = @cImport({
Line 3,409 ⟶ 3,613:
const position = c.Vector3{ .x = 0, .y = 0, .z = 0 };
const x_rot = 45;
const y_center: f32 = std.math.sqrt(@as(f32, 3).0) * cube_side / 2.0;
const z_rot = std.math.radiansToDegrees(f32, std.math.atan(@as(f32, std.math.sqrt1_2)) * 180 / std.math.pi);
 
c.SetConfigFlags(c.FLAG_WINDOW_RESIZABLE | c.FLAG_VSYNC_HINT);
59

edits