-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMat.py
116 lines (99 loc) · 2.57 KB
/
Mat.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
105
106
107
108
109
110
111
112
113
114
115
116
import pygame
from pygame.locals import *
import sys
import math
import serial
import re
# Define the COM port and baud rate
com_port = 'COM4'
baud_rate = 9600
# Initialize Pygame
ser = serial.Serial(com_port, baud_rate)
print(f"Reading from {com_port} at {baud_rate} baud...")
pygame.init()
xp,yp=0,0
# Constants
WIDTH, HEIGHT = 800, 600
BG_COLOR = (0, 0, 0)
CUBE_COLOR = (255, 255, 255)
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rotating Cube")
# Define the vertices of the cube
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]
]
# Define the edges that connect the vertices
edges = [
(0, 1),
(1, 2),
(2, 3),
(3, 0),
(4, 5),
(5, 6),
(6, 7),
(7, 4),
(0, 4),
(1, 5),
(2, 6),
(3, 7)
]
# Initialize rotation angles
angle_x = 0
angle_y = 0
# Main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
# Clear the screen
screen.fill(BG_COLOR)
# Rotate the cube around the X and Y axes
rotated_vertices = []
for vertex in vertices:
x, y, z = vertex
# Rotate around X-axis
new_y = y * math.cos(angle_x) - z * math.sin(angle_x)
new_z = y * math.sin(angle_x) + z * math.cos(angle_x)
# Rotate around Y-axis
new_x = x * math.cos(angle_y) + z * math.sin(angle_y)
rotated_vertices.append([new_x, new_y, new_z])
# Draw the edges
for edge in edges:
start = rotated_vertices[edge[0]]
end = rotated_vertices[edge[1]]
pygame.draw.line(screen, CUBE_COLOR, (start[0] * 100 + WIDTH // 2, start[1] * 100 + HEIGHT // 2),
(end[0] * 100 + WIDTH // 2, end[1] * 100 + HEIGHT // 2), 2)
pygame.display.flip()
data = ser.readline().decode().strip()
datax=list(map(int,data.split()))
xpos,ypos=datax[0],datax[1]
if abs(xp-xpos)>=5:
if xpos-xp>=0:
angle_x+=0.05
elif xpos-xp<=0:
angle_x-=0.05
xp=xpos
print("X is changing")
if abs(yp-ypos)>=5:
if ypos-yp>=0:
angle_y-=0.05
elif ypos-yp<=0:
angle_y+=0.05
yp=ypos
print("Y is changing")
print(xpos,ypos)
# Update rotation angles
# Limit the frame rate
pygame.time.delay(10)
# Quit Pygame
pygame.quit()
sys.exit()