-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
55 lines (52 loc) · 2.06 KB
/
test.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
import pygame, sys
import numpy as np
import math
import time
def rotate_vector(v, angles):
""" Rotate an n-dimensional vector v by a list of angles for each dimension. """
# Create rotation matrix
if type(angles) is float:
angles = np.asarray(angles)
angles = np.append(angles, 0)
else:
angles = np.asarray(angles)
rotation_matrix = np.identity(len(v))
for i in range(len(v)):
for j in range(i+1, len(v)):
rotation_matrix[i][i] = math.cos(angles[i])
rotation_matrix[i][j] = -math.sin(angles[i])
rotation_matrix[j][i] = math.sin(angles[i])
rotation_matrix[j][j] = math.cos(angles[i])
# Rotate vector
rotated_vector = np.matmul(rotation_matrix, v)
return rotated_vector
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
def draw_rect_angle(surface, color, rect, angle, width=0):
target_rect = pygame.Rect(rect)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, (0, 0, *target_rect.size), width)
rotated_surf = pygame.transform.rotate(shape_surf, angle)
surface.blit(rotated_surf, rotated_surf.get_rect(center = target_rect.center))
def draw_ellipse_angle(surface, color, rect, angle, width=0):
target_rect = pygame.Rect(rect)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.ellipse(shape_surf, color, (0, 0, *target_rect.size), width)
rotated_surf = pygame.transform.rotate(shape_surf, angle)
surface.blit(rotated_surf, rotated_surf.get_rect(center = target_rect.center))
angle = 00
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window_center = window.get_rect().center
window.fill((0, 0, 0))
draw_rect_angle(window, (255, 255, 255), (75, 150, 250, 100), angle, 2)
draw_ellipse_angle(window, (255, 255, 255), (75, 150, 250, 100), angle, 2)
angle += 1
pygame.display.update()
time.sleep(0.1)
pygame.quit()