; === Initialize Vertices === to initializeCube ; Each vertex: [x y z] make "vertices [ [-50 -50 -50] [50 -50 -50] [50 50 -50] [-50 50 -50] [-50 -50 50] [50 -50 50] [50 50 50] [-50 50 50] ] end ; === Rotation Around Z-Axis === to rotateZ :point :angle make "x item 1 :point make "y item 2 :point make "z item 3 :point ; Apply rotation make "newX :x * cos :angle - :y * sin :angle make "newY :x * sin :angle + :y * cos :angle make "newZ :z output list :newX :newY :newZ end ; === Perspective Projection from 3D to 2D === to projectPoint :point make "x item 1 :point make "y item 2 :point make "z item 3 :point make "scale 300 / (300 + :z) make "projX :x * :scale make "projY :y * :scale output list :projX :projY end ; === Draw Edge Between Two 2D Points === to drawEdge :a :b penup goto item 1 :a item 2 :a pendown goto item 1 :b item 2 :b penup end ; === Connect Cube Vertices into Edges === to drawCube :points ; Front face (0 1 2 3) drawEdge item 1 :points item 2 :points drawEdge item 2 :points item 3 :points drawEdge item 3 :points item 4 :points drawEdge item 4 :points item 1 :points ; Back face (4 5 6 7) drawEdge item 5 :points item 6 :points drawEdge item 6 :points item 7 :points drawEdge item 7 :points item 8 :points drawEdge item 8 :points item 5 :points ; Connect front to back repeat 4 [ drawEdge item :repcount :points item :repcount + 4 :points ] end ; === Optional: Draw Axes === to drawAxes ; X axis - red setpencolor [255 0 0] penup setxy 0 0 pendown setheading 0 forward 100 back 200 forward 100 ; Y axis - green setpencolor [0 255 0] setheading 90 forward 100 back 200 forward 100 ; Z axis - blue (simulated as scaling) setpencolor [0 0 255] penup setxy 0 0 pendown repeat 20 [ forward 5 setpencolor ifelse repcount mod 2 = 0 [[0 0 255]] [[255 255 255]] ] penup setpencolor [0 0 0] end ; === Full Render Loop for Cube Animation === to render clearscreen ; Optionally draw axes drawAxes ; Rotate all vertices make "rotated [] foreach :vertices [ make "rotated lput (rotateZ ? :angle) :rotated ] ; Project 3D points to 2D make "projected [] foreach :rotated [ make "projected lput (projectPoint ?) :projected ] ; Move drawing origin to center of screen penup setxy 0 0 ; Draw cube using projected points drawCube :projected end ; === Animate the Rotating Cube === to animateCube initializeCube make "angle 0 forever [ render make "angle :angle + 5 wait 1 ] end ; === Start Everything === animateCube