-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle3D.py
103 lines (88 loc) · 3.79 KB
/
rectangle3D.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
from OpenGL.GL import *
import math
class Rectangle3D:
def __init__(self, x, y, z, width, height, depth, cow=False):
self.x = x
self.y = y
self.z = z
self.width = width
self.height = height
self.depth = depth
self.cow = cow
def draw(self):
glBegin(GL_QUADS)
glColor3f(0.5, 0.5, 0.5)
if self.cow:
glColor3f(0, 0, 1)
# Front face
glVertex3f(self.x, self.y, self.z)
glVertex3f(self.x + self.width, self.y, self.z)
glVertex3f(self.x + self.width, self.y + self.height, self.z)
glVertex3f(self.x, self.y + self.height, self.z)
# Back face
glVertex3f(self.x, self.y, self.z + self.depth)
glVertex3f(self.x + self.width, self.y, self.z + self.depth)
glVertex3f(self.x + self.width, self.y + self.height, self.z + self.depth)
glVertex3f(self.x, self.y + self.height, self.z + self.depth)
# Top face
glVertex3f(self.x, self.y, self.z)
glVertex3f(self.x + self.width, self.y, self.z)
glVertex3f(self.x + self.width, self.y, self.z + self.depth)
glVertex3f(self.x, self.y, self.z + self.depth)
# Bottom face
glVertex3f(self.x, self.y + self.height, self.z)
glVertex3f(self.x + self.width, self.y + self.height, self.z)
glVertex3f(self.x + self.width, self.y + self.height, self.z + self.depth)
glVertex3f(self.x, self.y + self.height, self.z + self.depth)
# Left face
glVertex3f(self.x, self.y, self.z)
glVertex3f(self.x, self.y, self.z + self.depth)
glVertex3f(self.x, self.y + self.height, self.z + self.depth)
glVertex3f(self.x, self.y + self.height, self.z)
# Right face
glVertex3f(self.x + self.width, self.y, self.z)
glVertex3f(self.x + self.width, self.y, self.z + self.depth)
glVertex3f(self.x + self.width, self.y + self.height, self.z + self.depth)
glVertex3f(self.x + self.width, self.y + self.height, self.z)
glEnd()
glColor3f(1, 1, 1)
def set_color(self, r, g, b):
glColor3f(r, g, b)
def rotate_y(self, angle_degrees):
angle_radians = math.radians(angle_degrees)
cos_angle = math.cos(angle_radians)
sin_angle = math.sin(angle_radians)
# Center of the rectangle
center_x = self.x + self.width / 2
center_z = self.z + self.depth / 2
# Rotate each vertex around the Y-axis
vertices = [
(self.x, self.y, self.z),
(self.x + self.width, self.y, self.z),
(self.x + self.width, self.y + self.height, self.z),
(self.x, self.y + self.height, self.z),
(self.x, self.y, self.z + self.depth),
(self.x + self.width, self.y, self.z + self.depth),
(self.x + self.width, self.y + self.height, self.z + self.depth),
(self.x, self.y + self.height, self.z + self.depth)
]
rotated_vertices = []
for vertex in vertices:
x, y, z = vertex
# Translate vertex to origin
x -= center_x
z -= center_z
# Rotate around Y axis
x_rotated = x * cos_angle - z * sin_angle
z_rotated = x * sin_angle + z * cos_angle
# Translate vertex back
x_rotated += center_x
z_rotated += center_z
rotated_vertices.append((x_rotated, y, z_rotated))
# Update the rectangle with rotated vertices
self.x = rotated_vertices[0][0]
self.y = rotated_vertices[0][1]
self.z = rotated_vertices[0][2]
self.width = rotated_vertices[1][0] - rotated_vertices[0][0]
self.height = rotated_vertices[2][1] - rotated_vertices[0][1]
self.depth = rotated_vertices[4][2] - rotated_vertices[0][2]