-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzoom.py
More file actions
122 lines (85 loc) · 2.9 KB
/
zoom.py
File metadata and controls
122 lines (85 loc) · 2.9 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
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
117
118
119
120
121
122
# ----------------------------------------------------------------------------------------------------------
#
# Program Written By: Ruchi Saha (CED15I023)
# Description: The program illustrates zooming
#
# ----------------------------------------------------------------------------------------------------------
import pygame
import math
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
GREEN = [0, 255, 0]
RED = [255, 0, 0]
BLUE = [0, 0, 255]
GREY = [50,50,50]
origin = (400,400)
circle_center = (350,350)
screen_size = (830, 700)
circle_points = set() # stores the coordinates belonging to a circle
pygame.init()
def plot_circle(point_set,color,title,text_color):
for i in point_set:
screen.set_at(i, color)
text = font.render(title, True, text_color)
screen.blit(text, (50, 20))
pygame.display.flip()
def add_circle_points(cx,cy,x,y):
circle_points.add((cx+x, cy+y))
circle_points.add((cx+x, cy-y))
circle_points.add((cx-x, cy+y))
circle_points.add((cx-x, cy-y))
circle_points.add((cx+y, cy+x))
circle_points.add((cx+y, cy-x))
circle_points.add((cx-y, cy+x))
circle_points.add((cx-y, cy-x))
def draw_circle(cx,cy,r):
x=0
y=r
d=3-2*r
while y>=x:
add_circle_points(cx,cy,x,y)
x+=1
if d>0:
y=y-1
d=d+4*(x-y)+10
else:
d=d+ 4*x+6
def zoom(factor):
zoom_points = set()
for i in circle_points:
x,y=i
x1 = int(x*factor - factor*circle_center[0] + circle_center[0])
y1 = int (y*factor - factor * circle_center[1] + circle_center[1])
zoom_points.add((x1,y1))
text = "After Zooming " + str(factor) + "X"
plot_circle(zoom_points, GREEN, text, BLUE)
pygame.time.delay(700)
plot_circle(zoom_points, BLACK, text, BLACK)
if __name__ == '__main__':
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Affine transformations")
# Loop until the user clicks the close button.
done = False
font = pygame.font.SysFont("comicsansms", 40)
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#------ Code ------------------------------------------
draw_circle(circle_center[0],circle_center[1],50)
plot_circle(circle_points,GREEN,"",BLACK)
pygame.time.delay(1000)
zoom(1.5)
zoom(2)
zoom(3)
zoom(4)
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()