4D Tesseract Visualizer

Crafted by Bob Bu · Spring 2025

Four dimensional geometry sounds abstract, but applying visualization and projection methods generalized from lower dimensions can be intuitive. This project draws a tesseract in Python using NumPy and Matplotlib. Points live in 4D with (x,y,z,w)(x, y, z, w) coordinates. Each frame the code rotates the shape in 4D and then projects it into a 3D view.

Orthographic and perspective projections of a cube

This figure shows a normal cube under orthographic and perspective projection. Orthographic uses a simple matrix PorthoP_{\text{ortho}} that keeps xx and yy and drops zz, so size does not change with depth and parallel edges stay parallel. Perspective uses PperspP_{\text{persp}} and a divide by ww, so distant points look smaller and edges tilt toward a vanishing point.

The tesseract code reuses that idea one dimension higher. For each rotated vertex the 4D to 3D perspective is

X=xfw,Y=yfw,Z=zfw, X = \frac{x}{f - w},\quad Y = \frac{y}{f - w},\quad Z = \frac{z}{f - w},

where ff is the focal length. A small clamp on fwf - w keeps the picture stable when a point gets close to the camera.

The script first builds the 24=162^4 = 16 vertices of the tesseract and the thirty two edges by pairing indices that differ in exactly one bit. Vertices are stored as columns in a NumPy array of shape (4,16)(4, 16).

Each animation frame updates two angles. One rotation happens in the xwxw plane, the other in the yzyz plane. The 4D rotation matrix is

R4D=RxwRyzR_{4D} = R_{xw} R_{yz}
Rxw=[cosθxw00sinθxw01000010sinθxw00cosθxw]Ryz=[10000cosθyzsinθyz00sinθyzcosθyz00001] R_{xw} = \begin{bmatrix} \cos\theta_{xw} & 0 & 0 & -\sin\theta_{xw} \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ \sin\theta_{xw} & 0 & 0 & \cos\theta_{xw} \end{bmatrix} \\[6pt] R_{yz} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta_{yz} & -\sin\theta_{yz} & 0 \\ 0 & \sin\theta_{yz} & \cos\theta_{yz} & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}

Every edge is a one-line object. Line width and alpha follow slow sine curves in time, so the projected tesseract can vary its colors.

def rotation_4d(angle_xw, angle_yz):
cxw = np.cos(angle_xw)
sxw = np.sin(angle_xw)
R_xw = np.eye(4, dtype=np.float32)
R_xw[0, 0] = cxw
R_xw[0, 3] = -sxw
R_xw[3, 0] = sxw
R_xw[3, 3] = cxw
cyz = np.cos(angle_yz)
syz = np.sin(angle_yz)
R_yz = np.eye(4, dtype=np.float32)
R_yz[1, 1] = cyz
R_yz[1, 2] = -syz
R_yz[2, 1] = syz
R_yz[2, 2] = cyz
return R_xw @ R_yz
def project_4d_to_3d(points_4d, focal=2.5):
x, y, z, w = points_4d
denom = np.clip(focal - w, 0.01, None)
X = x / denom
Y = y / denom
Z = z / denom
return np.vstack((X, Y, Z))

This block is the core of the visual. Rotation mixes the xwxw and yzyz planes. Projection turns the result into three coordinates for display. NumPy carries the linear algebra, and Matplotlib handles the camera and the dark background.

Initialization sets up the vertices and edges, builds the figure, and creates one line per edge. FuncAnimation calls the update function each frame, so the cube keeps spinning without a manual loop. To try it yourself you only need a recent Python interpreter and a normal desktop backend.

University of Alberta · Edmonton, AB, Canada