Πέμπτη 12 Μαρτίου 2026

Cube 3D


From an old program here: https://rosettacode.org/wiki/Draw_a_rotating_cube#M2000_Interpreter

Adding a second rotation (transform cartesian to polar coordinates) and hidden line removal using a simple algorithm. It is not a free rotation cube program although. A simple rotation plus a rotation in Z axis. 

Module Cube3D {
form 80, 32
smooth on // enable GDI+ smooth lines
pi=3.1415926
single zoff=0.577350
single cylr=1.632993
oX=scale.x div 2 : oY=scale.y div 2
SCALE=min.data(oX, oY)/2*.6
gradient 0, 5
single theta = 0, dtheta = 0.015, dt = 1000/60, phi = 0
single ScZof=SCALE/zoff
single ScZofM=SCALE*zoff
dim cylphi(6) as single, x() as single
cylphi(0)=pi/6, 5*pi/6, 3*pi/2, 11*pi/6, pi/2, 7*pi/6
every DT {
if mouse then
(oX, oY)=(mouse.x,mouse.y)
if dtheta<2.8 then dtheta*=1.05
else
dtheta = 0.15
end if
theta=theta mod (2*pi)
aa=theta div (pi/32)
rem pen 15 {print @(5,5),aa}
dim x(6)=oX : for i=0 to 5: x(i) += SCALE*cylr*rcos(cylphi(i)+theta):next
width 4{drawcube()}: refresh 100 : IF keypress(32) then exit
theta += pi/32*dtheta
phi+=.015
gradient 0, 5: cursor 0,0
Print "Press space to exit",,"Press A for wire frame",,"Press any mouse button to move cube",,Time$(Now)
}
sub drawcube()
local boolean skip, skip2, wire
local complex mm
if keypress(65) then wire=true : local aa=5000
' move oX,oY
' circle 300
for i= 0 to 2
skip2=0
skip=0
if aa>=16 and aa<=27 and i=1 then
skip=1
skip2=1
else.if aa>=58 or aa<=5 and i=2 then
skip=1
skip2=1
else.if aa>=37 and aa<=48 and i=0 then
skip=1
skip2=1
end if
if wire then skip=false:skip2=false
if skip else
      mm=(x(i)-oX, ScZofM i)
move oX, oY
step angle arg(mm)+phi, mod(mm)
mm=(0, -ScZof i)+mm
draw angle arg(mm)+phi, -mod(mm), 15 ' 11
end if
if aa>=5 and aa<=15 and i=0 then
else.if aa>=27 and aa<=37 and i=1 then
skip=1
else.if aa>=48 and aa<=58 and i=2 then
skip2=1
else
if aa>=27 and aa<=37 and i=0 then
skip2=1
else.if aa>=48 and aa<=58 and i=0 then
skip=1
else.if aa>=5 and aa<=15 then
if i=2 then skip=1 else skip2=1
end if
mm=(0, -ScZof i)
move oX, oY
step angle arg(mm)+phi, mod(mm)
mm=(x(5-i)-oX, ScZofM i)+mm
draw angle -arg(mm)+phi, mod(mm), 15' 14
end if
if skip2 else
mm=(x(i)-oX, -ScZofM i)
move oX, oY
step angle -arg(mm)+phi, mod(mm)
mm=(x((i+1) mod 3 + 3)-oX, ScZofM i)-mm
draw angle -arg(mm)+phi, mod(mm), 15' 13
end if
      if skip else
      'move x(i),oY-ScZofM
mm=(x(i)-oX, -ScZofM i)
move oX, oY
step angle -arg(mm)+phi, mod(mm)
mm=(x(i mod 3 + 3)-oX, ScZofM i)-mm
draw angle -arg(mm)+phi, mod(mm), 15
end if
next
end sub
}
Cube3D


 

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.