-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3DKnots.py
More file actions
86 lines (69 loc) · 2.45 KB
/
Copy path3DKnots.py
File metadata and controls
86 lines (69 loc) · 2.45 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
# http://paulbourke.net/geometry/knots/
from typing import List, Tuple
from math import pi, sin, cos
from OpenGL.GL import *
import pygame
import time
import sys
class Game:
def __init__(self):
self.W: int = 500
self.H: int = 500
self.WIN: pygame.surface.Surface = pygame.display.set_mode(
(self.W, self.H), pygame.DOUBLEBUF | pygame.OPENGL
)
self.running: bool = True
self.clock: pygame.time.Clock = pygame.time.Clock()
self.FPS: int = 60
self.vectors: List[Tuple[float, float, float]] = []
self.color_data: List[Tuple[int, int, int]] = []
self.beta: float = 0
self.timestep: float = 500
pygame.display.set_caption("3D Konts with pygame/OpenGL")
glOrtho(-250, 250, 250, -250, 250, -250)
self.last_time: float = time.time()
self.dt = (time.time() - self.last_time) * self.FPS
self.last_time = time.time()
def event_handler(self) -> None:
for event in pygame.event.get():
if event.type == pygame.QUIT or (
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
):
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
self.vectors.clear()
self.color_data.clear()
self.beta = 0
def draw(self) -> None:
r = 100 * (0.8 + 1.6 * sin(6 * self.beta))
theta = 2 * self.beta
phi = 0.6 * pi * sin(12 * self.beta)
x = r * cos(phi) * cos(theta)
y = r * cos(phi) * sin(theta)
z = r * sin(phi)
self.beta += pi / self.timestep
if len(self.vectors) - 1 < self.timestep:
self.vectors.append((x, y, z))
self.color_data.append((255, int(r), 255))
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glRotatef(self.dt, 0, 1, 0)
glLineWidth(2)
glBegin(GL_LINE_STRIP)
for pt, clr in zip(self.vectors, self.color_data):
glColor3fv(clr)
glVertex3dv(pt)
glEnd()
pygame.display.flip()
def run(self) -> None:
while self.running:
self.dt = (time.time() - self.last_time) * self.FPS
self.last_time = time.time()
self.clock.tick(self.FPS)
self.event_handler()
self.draw()
def run():
game = Game()
game.run()
if __name__ == "__main__":
run()