4D Cube Simulation: A Wireframe Cube That Cycles Through Every Color
Every 3D graphics tutorial starts with a rotating cube. This is mine. Pygame for rendering, NumPy for matrix math, and a color system that cycles the cube through the entire RGB spectrum while the background shows the inverse.
Eight vertices at the corners of a unit cube centered at the origin. Twelve edges connecting them. Two rotation matrices for X and Y axes. Orthographic projection to drop the z-coordinate. That is the entire graphics pipeline in 120 lines.
The Rotation
The cube rotates around the X and Y axes independently. Each frame increments the angles by a fixed step. The rotation matrices multiply against the vertex array using NumPy. The result is a set of rotated 3D coordinates that get projected to 2D by discarding the depth component.
The matrix approach means rotation costs the same regardless of object complexity. Eight vertices or eight thousand. The math scales. That was the insight I wanted to understand by building it from scratch instead of using a library.
The Color
The color cycles through RGB in three segments. Red to green over 120 degrees. Green to blue. Blue to red. Each segment linearly interpolates between two primaries while holding the third at zero. The background receives the inverse of the cube color, which ensures contrast through the entire cycle.
The cycle completes in 600 frames at 60 frames per second. That is a ten second loop. The color changes smoothly enough that the transitions are visible without being distracting.
What I Would Change
The projection is orthographic. There is no perspective. The cube looks flat because parallel lines stay parallel regardless of depth. Adding perspective projection would make it look like a real 3D object instead of a wireframe schematic.
The vertices are regenerated every frame even though they never change. The code calls generate_cube inside the loop. That is eight vertices of unnecessary work per frame. I would generate them once before the loop starts.
There is no user interaction. No mouse control. No keyboard input. No way to change the rotation speed or color cycle. It plays itself until you close the window.
It is a cube built from math, drawn with lines, and colored by time. That is all it needs to be. Every 3D project I built later started with the same eight vertices and two rotation matrices.