-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.py
104 lines (90 loc) · 2.3 KB
/
shapes.py
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
# Parit Vacharaskunee 6580209
# Project for ICCS 205 Numerical Methods, MUIC
import pygame as pg
import numpy as np
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
size = 80
transX = 250
transY = 250
class Cube:
def __init__(self):
# https://www.malinc.se/math/linalg/rotatecubeen.php
self.vertices = size * np.array([
[1, 1, 1],
[1, -1, 1],
[-1, -1, 1],
[-1, 1, 1],
[1, 1, -1],
[1, -1, -1],
[-1, -1, -1],
[-1, 1, -1]
])
self.lines = [
(0, 1),
(1, 2),
(2, 3),
(3, 0),
(4, 5),
(5, 6),
(6, 7),
(7, 4),
(0, 4),
(1, 5),
(2, 6),
(3, 7)
]
class Prism:
def __init__(self):
self.vertices = size * np.array([
[1, 1, 0],
[1, -1, 0],
[-1, -1, 0],
[-1, 1, 0],
[1, 0, 1],
[-1, 0, 1]
])
self.lines = [
(0, 1),
(1, 2),
(2, 3),
(3, 0),
(4, 5),
(4, 1),
(4, 0),
(5, 2),
(5, 3)
]
def update(shape, r, locations, screen):
for point in shape.vertices:
rotation = np.matmul(point, r)
px, py = rotation[0], rotation[2]
locations.append((px + transX, py + transY))
pg.draw.circle(screen, RED, ((px + transX), py + transY), 6)
for pos in shape.lines:
pg.draw.line(screen, WHITE, locations[pos[0]], locations[pos[1]], 4)
def show_axis(r, screen):
locations = []
points = size * np.array([
[2, 0, 0],
[0, 2, 0],
[0, 0, 2],
[0, 0, 0]
])
lines = [
(3, 0),
(3, 1),
(3, 2)
]
colors = [RED, GREEN, BLUE]
i = 0
for point in points:
rotation = np.matmul(point, r)
px, py = rotation[0], rotation[2]
locations.append((px + transX, py + transY))
for pos in lines:
pg.draw.line(screen, colors[i], locations[pos[0]], locations[pos[1]], 4)
i += 1