1
from math import cos, pi, sin
2
from random import randint
3
from time import sleep
4

5
import pygame
6
from numpy import matrix
7

8
WHITE = (255, 255, 255)
9
WIDTH = 500
10
HEIGHT = 500
11

12
pygame.init()
13
display = pygame.display
14
surface = display.set_mode((WIDTH, HEIGHT))
15
display.set_caption("3D")
16

17
done = False
18
points = (
19
(50, 50, 50),
20
(-50, 50, 50),
21
(50, -50, 50),
22
(50, 50, -50),
23
(-50, 50, -50),
24
(50, -50, -50),
25
(-50, -50, -50),
26
(-50, -50, 50),
27
)
28

29
rotation = [0, 0, 0]
30

31

32
def generate_x(theta):
33
return matrix(
34
[[1, 0, 0], [0, cos(theta), -sin(theta)], [0, sin(theta), cos(theta)]]
35
)
36

37

38
def generate_y(theta):
39
return matrix(
40
[[cos(theta), 0, -sin(theta)], [0, 1, 0], [sin(theta), 0, cos(theta)]]
41
)
42

43

44
def generate_z(theta):
45
return matrix(
46
[[cos(theta), -sin(theta), 0], [sin(theta), cos(theta), 0], [0, 0, 1]]
47
)
48

49

50
while not done:
51
for e in pygame.event.get():
52
if e.type == pygame.QUIT:
53
done = True
54
break
55

56
pygame.draw.rect(surface, (0, 0, 0), pygame.Rect(0, 0, WIDTH, HEIGHT))
57

58
render_points = []
59

60
for p in points:
61

62
m = matrix([[p[0]], [p[1]], [p[2]]])
63

64
for method, angle in zip((generate_x, generate_y, generate_z), rotation):
65
m = method(angle) * m
66

67
x, y, z = map(lambda x: int(WIDTH / 2 - x), (m[0, 0], m[1, 0], m[2, 0]))
68

69
render_points.append((x, y))
70

71
for p1 in range(len(render_points) - 1):
72
for p2 in render_points[p1 + 1 :]:
73
pygame.draw.line(surface, WHITE, render_points[p1], p2)
74

75
rotation[0] += pi / randint(100, 200)
76
rotation[1] += pi / randint(100, 200)
77

78
display.flip()
79
sleep(1 / 45)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0