Draw a rotating cube: Difference between revisions

m
→‎Zig: Update "works with". Simplify two lines involving casting and math.
(Major rewrite to include SceneKit library functions.)
m (→‎Zig: Update "works with". Simplify two lines involving casting and math.)
 
(17 intermediate revisions by 8 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 1,975 ⟶ 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}}==
Line 1,993 ⟶ 2,251:
],
Boxed -> False]]</syntaxhighlight>
 
=={{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}}==
Line 3,087 ⟶ 3,425:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 3,254 ⟶ 3,592:
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