-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4D hypercube.py
More file actions
182 lines (159 loc) · 5.19 KB
/
4D hypercube.py
File metadata and controls
182 lines (159 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import pygame
import numpy as np
from math import sin, cos, pi
from pygame.locals import *
# Initialize Pygame
pygame.init()
# Set up the display
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Interactive 4D Hypercube (Tesseract)")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
# Hypercube vertices (4D coordinates)
# A tesseract has 16 vertices
vertices_4d = np.array([
[x, y, z, w]
for x in [-1, 1]
for y in [-1, 1]
for z in [-1, 1]
for w in [-1, 1]
])
# Edges connecting the vertices
edges = []
for i in range(len(vertices_4d)):
for j in range(i + 1, len(vertices_4d)):
# Connect vertices that differ in exactly one coordinate
if sum(abs(vertices_4d[i][k] - vertices_4d[j][k]) for k in range(4)) == 2:
edges.append((i, j))
# Rotation angles for different planes
angles = {
'xy': 0,
'xz': 0,
'xw': 0,
'yz': 0,
'yw': 0,
'zw': 0
}
def rotation_matrix_4d(angle, plane):
"""Create a 4D rotation matrix for the given plane"""
c, s = cos(angle), sin(angle)
matrix = np.eye(4)
if plane == 'xy':
matrix[0:2, 0:2] = [[c, -s], [s, c]]
elif plane == 'xz':
matrix[0, 0], matrix[0, 2] = c, -s
matrix[2, 0], matrix[2, 2] = s, c
elif plane == 'xw':
matrix[0, 0], matrix[0, 3] = c, -s
matrix[3, 0], matrix[3, 3] = s, c
elif plane == 'yz':
matrix[1, 1], matrix[1, 2] = c, -s
matrix[2, 1], matrix[2, 2] = s, c
elif plane == 'yw':
matrix[1, 1], matrix[1, 3] = c, -s
matrix[3, 1], matrix[3, 3] = s, c
elif plane == 'zw':
matrix[2, 2], matrix[2, 3] = c, -s
matrix[3, 2], matrix[3, 3] = s, c
return matrix
def project_4d_to_3d(points_4d, distance=4):
"""Project 4D points to 3D using perspective projection"""
points_3d = []
for point in points_4d:
w = distance / (distance - point[3]) # Perspective projection
x = point[0] * w
y = point[1] * w
z = point[2] * w
points_3d.append([x, y, z])
return np.array(points_3d)
def project_3d_to_2d(points_3d, distance=5):
"""Project 3D points to 2D using perspective projection"""
points_2d = []
for point in points_3d:
f = distance / (distance - point[2]) # Perspective projection
x = point[0] * f * 100 + WIDTH/2
y = point[1] * f * 100 + HEIGHT/2
points_2d.append([x, y])
return np.array(points_2d)
# Main loop
clock = pygame.time.Clock()
running = True
# Rotation controls
rotation_speed = 0.02
active_rotations = set()
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN:
# Add rotation plane when key is pressed
if event.key == K_q: # XY rotation
active_rotations.add('xy')
elif event.key == K_w: # XZ rotation
active_rotations.add('xz')
elif event.key == K_e: # XW rotation
active_rotations.add('xw')
elif event.key == K_a: # YZ rotation
active_rotations.add('yz')
elif event.key == K_s: # YW rotation
active_rotations.add('yw')
elif event.key == K_d: # ZW rotation
active_rotations.add('zw')
elif event.type == KEYUP:
# Remove rotation plane when key is released
if event.key == K_q:
active_rotations.discard('xy')
elif event.key == K_w:
active_rotations.discard('xz')
elif event.key == K_e:
active_rotations.discard('xw')
elif event.key == K_a:
active_rotations.discard('yz')
elif event.key == K_s:
active_rotations.discard('yw')
elif event.key == K_d:
active_rotations.discard('zw')
# Update rotations
for plane in active_rotations:
angles[plane] += rotation_speed
# Apply all rotations
rotated_points = vertices_4d.copy()
for plane, angle in angles.items():
rotation = rotation_matrix_4d(angle, plane)
rotated_points = np.dot(rotated_points, rotation)
# Project 4D -> 3D -> 2D
points_3d = project_4d_to_3d(rotated_points)
points_2d = project_3d_to_2d(points_3d)
# Clear screen
screen.fill(BLACK)
# Draw edges
for edge in edges:
start = points_2d[edge[0]].astype(int)
end = points_2d[edge[1]].astype(int)
pygame.draw.line(screen, WHITE, start, end, 2)
# Draw vertices
for point in points_2d:
pygame.draw.circle(screen, RED, point.astype(int), 5)
# Draw instructions
font = pygame.font.Font(None, 24)
instructions = [
"Controls:",
"Q: Rotate XY plane",
"W: Rotate XZ plane",
"E: Rotate XW plane",
"A: Rotate YZ plane",
"S: Rotate YW plane",
"D: Rotate ZW plane"
]
for i, text in enumerate(instructions):
text_surface = font.render(text, True, GREEN)
screen.blit(text_surface, (10, 10 + i * 20))
pygame.display.flip()
clock.tick(60)
pygame.quit()