-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2d-transformation.py
More file actions
59 lines (50 loc) · 1.47 KB
/
2d-transformation.py
File metadata and controls
59 lines (50 loc) · 1.47 KB
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
import pygame
import math
# Initialize Pygame
pygame.init()
# Set up the display
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Draw Line Example")
# Set up colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLACK = (0,0,0)
global x1,y1,x2,y2
x1,y1,x2,y2 = 100,100,400,300
def translation(x1,y1,x2,y2,tx,ty):
x1 += tx
y1 += ty
x2 += tx
y2 += ty
pygame.draw.line(screen, BLACK, (x1, y1), (x2, y2), 5)
def scaling(x1,y1,x2,y2,sx,sy):
x1 *= sx
y1 *= sy
x2 *= sx
y2 *= sy
pygame.draw.line(screen, BLACK, (x1, y1), (x2, y2), 5)
def rotation(x1,y1,x2,y2,angle):
angle=30
angle = math.radians(angle)
x1_new = x1*math.cos(angle) - y1*math.sin(angle)
y1_new = x1*math.sin(angle) + y1*math.cos(angle)
x2_new = x2*math.cos(angle) - y2*math.sin(angle)
y2_new = x2*math.sin(angle) + y2*math.cos(angle)
pygame.draw.line(screen, BLACK, (x1_new, y1_new), (x2_new, y2_new), 5)
# Main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill the screen with white
screen.fill(WHITE)
# Draw a red line (x1, y1, x2, y2, color)
pygame.draw.line(screen, RED, (x1, y1), (x2, y2), 5)
translation(x1,y1,x2,y2,50,50)
scaling(x1,y1,x2,y2,5,5)
rotation(x1,y2,x2,y2,45)
# Update the display
pygame.display.flip()
# Quit Pygame
pygame.quit()