-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircles_and_points.py
More file actions
216 lines (152 loc) · 6.31 KB
/
circles_and_points.py
File metadata and controls
216 lines (152 loc) · 6.31 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import pygame
import math
from vector import Vector2
done = False
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 300
ORIGINX = SCREEN_WIDTH / 2
ORIGINY = SCREEN_HEIGHT / 2
pygame.init()
pygame.display.set_caption("Animated Points")
screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])
clock = pygame.time.Clock()
#==============================================================
#==============================================================
#==============================================================
#
#
# set RENDER_GIF to true if you want to render the frames
#
# leave set to false if you just want to view the animation
#
RENDER_GIF = False
NUM_FRAMES_TO_RENDER = 180
#
#
#
#==============================================================
#==============================================================
#==============================================================
class CircleWithRotator():
def __init__(self, x, y, radius, start_angle, angle_speed):
self.originx = x
self.originy = y
self.radius = radius
self.angle = start_angle
self.angle_speed = angle_speed
self.rotator_pos = Vector2(0, 0)
def update(self):
self.angle += self.angle_speed
if self.angle > (360 - self.angle_speed):
self.angle = 0
self.rotator_pos.x = self.originx + (self.radius * math.cos(math.radians(self.angle)))
self.rotator_pos.y = self.originy + (self.radius * math.sin(math.radians(self.angle)))
def draw(self):
self.update()
# draw the circle
pygame.draw.circle(screen, (100,0,75), (self.originx, self.originy), self.radius, 1)
# draw the rotator
pygame.draw.circle(screen, (0,255,255), (self.rotator_pos.x, self.rotator_pos.y), 4)
class Square():
def __init__(self):
# corners are pointers to the circle rotator vector objects
self.corners = []
def setCornerPoints(self, corner_points):
self.corners = corner_points
def getPointsBetween(self, v1, v2, num_points):
# returns a list of equally spaced points between Vector1 and Vector2
# ie, points along a line between 2 of our corners.
# each point is stored in the list as an xy tuple
points = []
# get a copy of V2 that is safe to mess with
dist = v2.getCopy()
# subtract the other top corner
dist.sub(v1)
# now I can work out the distance between the points
distance_between_points = dist.mag()
step = distance_between_points / (num_points+1)
dist.normalise()
# this calculates all the steps along the line
for i in range(num_points):
dist.mult(step * (i+1))
x = v1.x + dist.x
y = v1.y + dist.y
points.append((x,y))
dist.normalise()
return points
def draw(self):
num_points = 8
top_points = self.getPointsBetween(self.corners[0], self.corners[1], num_points)
bottom_points = self.getPointsBetween(self.corners[3], self.corners[2], num_points)
# draw points and lines joining the points
for tp, bp in zip(top_points, bottom_points):
pygame.draw.circle(screen, (255,255,255), tp, 3)
pygame.draw.circle(screen, (255,255,255), bp, 3)
pygame.draw.line(screen, (255,0,255), tp, bp)
# draw the lines that make up our square
for i in range(0, len(self.corners)-1):
p1 = self.corners[i]
p2 = self.corners[i+1]
pygame.draw.line(screen, (255,255,0), (p1.x, p1.y), (p2.x, p2.y))
# join last corner position to 1st
if i == len(self.corners)-2:
p1 = self.corners[-1]
p2 = self.corners[0]
pygame.draw.line(screen, (255,255,0), (p1.x, p1.y), (p2.x, p2.y))
class Animation():
def __init__(self):
# gif stuff
self.rendering = RENDER_GIF
self.render_done = False
self.frame_number = 0
self.frames_to_render = NUM_FRAMES_TO_RENDER
# these values set up where the circles will be placed
# their posx, posy, radius, start angle, and anglespeed
#
# px py r a s
self.circle_params = [(200, 60, 50, 0, 4),
(200, 150, 82, 90, 2),
(60, 60, 50, 180, 2),
(80, 200, 75, 270, 1)]
self.circles = []
self.square = Square()
# make dem circles!
for x, y, radius, startangle, angleinc in self.circle_params:
c = CircleWithRotator(x, y, radius, startangle, angleinc)
self.circles.append(c)
# gather all the circle rotor vectors
positions = []
for c in self.circles:
pos = (c.rotator_pos)
positions.append(pos)
# and pass them in to the spinny square
self.square.setCornerPoints(positions)
def renderFrame(self):
filename = '{}.png'.format(self.frame_number)
pygame.image.save(screen, filename)
print('Rendered file ' + filename)
def draw(self):
if self.rendering and self.frame_number == self.frames_to_render:
self.rendering = False
print('Render completed.')
else:
self.frame_number += 1
for c in self.circles:
c.draw()
self.square.draw()
if self.rendering:
self.renderFrame()
animation = Animation()
while not done:
screen.fill([0,0,0])
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_ESCAPE):
done = True
mousex, mousey = pygame.mouse.get_pos()
animation.draw()
clock.tick(50)
pygame.display.flip()
pygame.quit()