"""Geometry excerpt from Solar System Explorer's tesseract_wobble.py. The vertex and edge definitions, rotation and projection functions below are reproduced verbatim. Requires Python 3 and NumPy. To obtain the XYZ coordinates of one projected frame, use project_4to3(VERTS4 @ rot4(math.pi / 4, 0.0).T) """ import math import numpy as np VERTS4 = np.array([[(i >> k) & 1 for k in range(4)] for i in range(16)], dtype=float) * 2 - 1 EDGES = [(i, j) for i in range(16) for j in range(i + 1, 16) if bin(i ^ j).count("1") == 1] def rot4(theta_xw: float, theta_xy: float) -> np.ndarray: """Rotation in the x–w plane (fold in/out) then about the pole (x–y plane).""" c, s = math.cos(theta_xw), math.sin(theta_xw) Rxw = np.eye(4) Rxw[0, 0], Rxw[0, 3], Rxw[3, 0], Rxw[3, 3] = c, -s, s, c c2, s2 = math.cos(theta_xy), math.sin(theta_xy) Rxy = np.eye(4) Rxy[0, 0], Rxy[0, 1], Rxy[1, 0], Rxy[1, 1] = c2, -s2, s2, c2 return Rxy @ Rxw def project_4to3(v4: np.ndarray, d: float = 3.0) -> np.ndarray: """Perspective from w: inner cube = far in w, outer cube = near in w.""" w = v4[:, 3] f = d / (d - w) return v4[:, :3] * f[:, None]