-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesseract.html
More file actions
168 lines (143 loc) · 3.75 KB
/
tesseract.html
File metadata and controls
168 lines (143 loc) · 3.75 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Spinning Tesseract</title>
<style>
body {
margin: 0;
overflow: hidden;
background: white;
}
canvas {
display: block;
}
</style>
</head>
<body>
generate code in pure javascript
to make a 3d spinning tesseract
</p>
<canvas id="tesseractCanvas"></canvas>
<script>
const canvas = document.getElementById("tesseractCanvas");
const ctx = canvas.getContext("2d");
// Resize canvas to fit the screen
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const width = canvas.width;
const height = canvas.height;
// Define the vertices of the tesseract in 4D space
const vertices = [
[-1, -1, -1, -1],
[-1, -1, -1, 1],
[-1, -1, 1, -1],
[-1, -1, 1, 1],
[-1, 1, -1, -1],
[-1, 1, -1, 1],
[-1, 1, 1, -1],
[-1, 1, 1, 1],
[1, -1, -1, -1],
[1, -1, -1, 1],
[1, -1, 1, -1],
[1, -1, 1, 1],
[1, 1, -1, -1],
[1, 1, -1, 1],
[1, 1, 1, -1],
[1, 1, 1, 1]
];
// Define the edges connecting the vertices
const edges = [
[0, 1], [0, 2], [0, 4], [0, 8],
[1, 3], [1, 5], [1, 9],
[2, 3], [2, 6], [2, 10],
[3, 7], [3, 11],
[4, 5], [4, 6], [4, 12],
[5, 7], [5, 13],
[6, 7], [6, 14],
[7, 15],
[8, 9], [8, 10], [8, 12],
[9, 11], [9, 13],
[10, 11], [10, 14],
[11, 15],
[12, 13], [12, 14],
[13, 15],
[14, 15]
];
// Rotation angles
let angleXY = 0;
let angleXZ = 0;
let angleXW = 0;
let angleYW = 0;
// Project a point from 4D to a lower dimension
function project(point) {
// Rotate in XY plane
let x = point[0] * Math.cos(angleXY) - point[1] * Math.sin(angleXY);
let y = point[0] * Math.sin(angleXY) + point[1] * Math.cos(angleXY);
// Rotate in XZ plane
let z = point[2] * Math.cos(angleXZ) - x * Math.sin(angleXZ);
x = point[2] * Math.sin(angleXZ) + x * Math.cos(angleXZ);
// Rotate in XW plane
let w = point[3] * Math.cos(angleXW) - x * Math.sin(angleXW);
x = point[3] * Math.sin(angleXW) + x * Math.cos(angleXW);
// Rotate in YW plane
w = w * Math.cos(angleYW) - y * Math.sin(angleYW);
y = w * Math.sin(angleYW) + y * Math.cos(angleYW);
// Perspective projection (4D to 3D to screen)
const perspective = w !== -2 ? (1 / (w + 3)) : (1 / (w + Number.EPSILON));
return [
x * perspective * width / height,
y * perspective,
z * perspective
];
}
// Draw the tesseract
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "red";
ctx.strokeStyle = "red";
ctx.lineWidth = 2;
// Project all vertices to 2D screen space
const projectedVertices = vertices.map(project);
// Center the tesseract on the screen
const scale = Math.min(width, height) / 4; /// 8;
const offsetX = width / 2;
const offsetY = height / 2;
// Draw edges
for (const edge of edges) {
const [startIdx, endIdx] = edge;
const start = projectedVertices[startIdx];
const end = projectedVertices[endIdx];
ctx.beginPath();
ctx.moveTo(start[0] * scale + offsetX, start[1] * scale + offsetY);
ctx.lineTo(end[0] * scale + offsetX, end[1] * scale + offsetY);
ctx.stroke();
}
// Draw vertices as small circles
for (const vertex of projectedVertices) {
ctx.beginPath();
ctx.arc(
vertex[0] * scale + offsetX,
vertex[1] * scale + offsetY,
3,
0,
Math.PI * 2
);
ctx.fill();
}
}
// Animation loop
function animate() {
angleXY += 0.01; // Rotate in XY plane
angleXZ += 0.01; // Rotate in XZ plane
angleXW += 0.01; // Rotate in XW plane
angleYW += 0.01; // Rotate in YW plane
draw();
requestAnimationFrame(animate);
}
// Start the animation
animate();
</script>
</body>
</html>