-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumbrella_locus_sweep.py
134 lines (103 loc) · 2.91 KB
/
umbrella_locus_sweep.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import pygame, sys, random, math
W = 800
H = 600
scale = 120
dot_sz = 3
n = 2
scale = 240/n
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
colors = [red, green, blue]
ncolors = len(colors)
speeds = [10, 25, 50, 100, 200]
ispeed = 2
pygame.init()
screen = pygame.display.set_mode((W,H))
pygame.display.set_caption('Umbrella Locus')
def blank():
screen.fill(black)
pygame.draw.line(screen,white,(int(W/2),H),(int(W/2),0))
pygame.draw.line(screen,white,(0,int(H/2)),(W,int(H/2)))
add_pt = 1
pts = []
indices = [0 for x in range(n)]
def reset():
pts.clear()
indices.clear()
indices.extend([0 for x in range(n)])
print('RESET')
n_step = 20
th_step = math.pi/n_step
sweep_home = 0
def get_xy(ths):
x = sum([math.cos(a) for a in ths])
y = sum([math.sin(a) for a in ths])
x = int(W/2 + x*scale)
y = int(H/2 - y*scale)
return (x,y)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
reset()
add_pt = 1
if event.key == pygame.K_p:
add_pt = 1-add_pt
if event.key == pygame.K_1:
ispeed = 4
if event.key == pygame.K_2:
ispeed = 3
if event.key == pygame.K_3:
ispeed = 2
if event.key == pygame.K_4:
ispeed = 1
if event.key == pygame.K_5:
ispeed = 0
if event.key == pygame.K_6:
ispeed = -1
if event.key == pygame.K_PERIOD:
if n < 6:
n += 1
scale = 240/n
reset()
print(n)
if event.key == pygame.K_COMMA:
if n > 1:
n -= 1
scale = 240/n
reset()
print(n)
blank()
if add_pt:
ths = [th_step*a for a in indices]
(x,y) = get_xy(ths)
pts.append((x,y))
if sum(indices) == n_step*n and sweep_home == 0:
sweep_home = 1
elif sum(indices) == 0 and sweep_home == 1:
#reset()
add_pt = 0
sweep_home = 0
elif sweep_home:
a = indices[0]
indices.clear()
indices.extend([a-1 for x in range(n)])
else:
ii = 0
while(indices[ii]==n_step):
ii += 1
indices[ii] += 1
icolor = 0
for a in range(len(ths)):
pygame.draw.line(screen,colors[icolor],get_xy(ths[0:a]),get_xy(ths[0:(a+1)]))
icolor = (icolor + 1) % ncolors
for a in pts:
#screen.set_at((x,y),white)
pygame.draw.circle(screen,white,a,dot_sz)
pygame.display.flip()
if ispeed >= 0:
pygame.time.wait(speeds[ispeed])