codehs-sandbox

All programs / funni cube

funni cube

A tesseract spinning through the fourth dimension

Run funni cube

Runs on its own, no input.

What it does

It draws a tesseract (a 4D hypercube) as 32 white lines on a black canvas and keeps it turning. What looks like a small cube inside a big one is really two cubes sitting at w = −1 and w = +1, joined along the fourth axis. When the rotation reaches into w, they swap places and turn inside out.

The interesting idea

Rotation happens in planes, not around axes. In 2D you rotate in the one plane there is. In 3D, “around the Y axis” really means “in the X–Z plane”. In 4D there are six planes. The program rotates a point in one pair of coordinates (i, j) and leaves the others alone, and chains three of these each frame:

  • rotate(point, 0, 2, angleY): the X–Z plane, an ordinary 3D spin
  • rotate(…, 1, 2, angleX): the Y–Z plane
  • rotate(…, 1, 3, angleYW): the Y–W plane, the one that goes into the fourth dimension

Then it projects twice. toPoint() divides x, y and z by w + wDistance, which gives a perspective view of 4D squashed into 3D. Then it divides by z + camDistance for ordinary 3D-to-screen perspective. That is why the cube at w = −1 looks bigger: it is “closer” in w.

Each frame it calls removeAll(), adds a fresh black background rectangle and rebuilds all 32 Lines. It runs on a 10 ms setTimer, which CodeHS caps at one tick per display frame.

Original source

The file exactly as saved on CodeHS, read-only. Open the raw file.

Checking SHA-256…

Evidence: CodeHS history

Screenshots of CodeHS’s “My Code History” page, which logs every save with a timestamp and the reason for it (autosave, run, paste).