/
Draw.py
277 lines (246 loc) · 9.74 KB
/
Draw.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://programarcadegames.com/
# http://simpson.edu/computer-science/
# Import a library of functions called 'pygame'
import pygame
from collections import defaultdict
import sys
from pygame.locals import *
import pygbutton
from psWrapper import PyWrapperPointSet
from time import gmtime
def main():
# Initialize the game engine
pygame.init()
# Define the colors we will use in RGB format
black = [ 0, 0, 0]
white = [255,255,255]
blue = [ 0, 0,255]
green = [ 0,255, 0]
red = [255, 0, 0]
DARKGRAY = ( 64, 64, 64)
GRAY = (128, 128, 128)
LIGHTGRAY = (212, 208, 200)
N = 30000
whichNonLin1 = 0
whichNonLin2 = 0
whichNonLin3 = 0
whichNonLin4 = 0
whichNonLin5 = 0
whichNonLin6 = 0
whichNonLin7 = 0
whichNonLin8 = 0
whichNonLin9 = 0
whichNonLin10 = 0
whichNonLin11 = 0
whichNonLin12 = 0
numLin = 10
colorInc = 3
width = 800
height = 600
# Create a button
buttonGo = pygbutton.PygButton((width-155, 60, 50, 25), 'Go')
# Create a button to save image
buttonSave = pygbutton.PygButton((width-95, 60, 50, 25), 'Save')
# Create 12 buttons to control the type of non linear transformations to use
button1 = pygbutton.PygButton((50, 60, 25, 25), '1')
button2 = pygbutton.PygButton((100, 60, 25, 25), '2')
button3 = pygbutton.PygButton((150, 60, 25, 25), '3')
button4 = pygbutton.PygButton((200, 60, 25, 25), '4')
button5 = pygbutton.PygButton((250, 60, 25, 25), '5')
button6 = pygbutton.PygButton((300, 60, 25, 25), '6')
button7 = pygbutton.PygButton((350, 60, 25, 25), '7')
button8 = pygbutton.PygButton((400, 60, 25, 25), '8')
button9 = pygbutton.PygButton((450, 60, 25, 25), '9')
button10 = pygbutton.PygButton((500, 60, 25, 25), '10')
button11 = pygbutton.PygButton((550, 60, 25, 25), '11')
button12 = pygbutton.PygButton((600, 60, 25, 25), '12')
# Define the colorCon : the function which controls the coloring behavior
def linearStep(i):
if i>1:
y = 0
elif i<0:
y = 0
elif i>=0.6667:
y = 0
elif i<=0.3333:
y = i*3
else:
y = (0.6667-i)*3
return y
def colorCon(i, colorCode):
if colorCode==1:
return (int)(linearStep(i)*255)
elif colorCode==2:
return (int)(linearStep(i-0.1667)*255)
else:
return (int)(linearStep(i-0.3333)*255)
# Set the height and width of the screen
size=[width,height]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("M & T's flame fractal !")
#Loop until the user clicks the close button.
done=False
clock = pygame.time.Clock()
calculated = False
wrapper = PyWrapperPointSet()
while done==False:
# This limits the while loop to a max of 10 times per second.
# Leave this out and we will use all CPU we can.
clock.tick()
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# handle user input
if event.type == KEYDOWN:
# if the user presses escape, quit the event loop.
if event.key == K_ESCAPE:
done=True
if 'click' in buttonGo.handleEvent(event):
prePointsXY = []
wrapper.go(N, whichNonLin1, whichNonLin2,
whichNonLin3, whichNonLin4, whichNonLin5,
whichNonLin6, whichNonLin7, whichNonLin8,
whichNonLin9, whichNonLin10, whichNonLin11,
whichNonLin12, numLin)
pointsX = wrapper.getPointsX()
pointsY = wrapper.getPointsY()
calculated = True
length = len(pointsX)
# print len(pointsX)
for i in range(len(pointsX)):
prePointsXY.append( (pointsX[i]/4+0.5,pointsY[i]/4+0.5) )
# Count the number of points
hitNumberXY = defaultdict(int)
pointsXY = defaultdict(int)
for (a,b) in prePointsXY:
for j in range(colorInc):
for k in range(colorInc):
hitNumberXY[((int)(a*width)+j,(int)(b*(height-100)+100+k))] +=1
pointsXY[((int)(a*width),(int)(b*(height-100)+100))] = 1
print len(pointsXY.keys())
maxHit = max(hitNumberXY.values())
numberHits = range(maxHit)
color = []
for i in numberHits:
j = (float)((i+1)/(maxHit+1)/2+0.25)^0.5
color.append((colorCon(j,1),
colorCon(j,2),colorCon(j,3),j))
if 'click' in buttonSave.handleEvent(event):
clicktime = (list)(gmtime())
clicktime = [str(i) for i in clicktime]
pygame.image.save(screen, "%s.png" % ''.join(clicktime))
# Controls the behavior of the 12 buttons on non linear trans
if 'click' in button1.handleEvent(event):
whichNonLin1 = 1 - whichNonLin1
if whichNonLin1==1:
button1.bgcolor = GRAY
else:
button1.bgcolor = LIGHTGRAY
if 'click' in button2.handleEvent(event):
whichNonLin2 = 1 - whichNonLin2
if whichNonLin2==1:
button2.bgcolor = GRAY
else:
button2.bgcolor = LIGHTGRAY
if 'click' in button3.handleEvent(event):
whichNonLin3 = 1 - whichNonLin3
if whichNonLin3==1:
button3.bgcolor = GRAY
else:
button3.bgcolor = LIGHTGRAY
if 'click' in button4.handleEvent(event):
whichNonLin4 = 1 - whichNonLin4
if whichNonLin4==1:
button4.bgcolor = GRAY
else:
button4.bgcolor = LIGHTGRAY
if 'click' in button5.handleEvent(event):
whichNonLin5 = 1 - whichNonLin5
if whichNonLin5==1:
button5.bgcolor = GRAY
else:
button5.bgcolor = LIGHTGRAY
if 'click' in button6.handleEvent(event):
whichNonLin6 = 1 - whichNonLin6
if whichNonLin6==1:
button6.bgcolor = GRAY
else:
button6.bgcolor = LIGHTGRAY
if 'click' in button7.handleEvent(event):
whichNonLin7 = 1 - whichNonLin7
if whichNonLin7==1:
button7.bgcolor = GRAY
else:
button7.bgcolor = LIGHTGRAY
if 'click' in button8.handleEvent(event):
whichNonLin8 = 1 - whichNonLin8
if whichNonLin8==1:
button8.bgcolor = GRAY
else:
button8.bgcolor = LIGHTGRAY
if 'click' in button9.handleEvent(event):
whichNonLin9 = 1 - whichNonLin9
if whichNonLin9==1:
button9.bgcolor = GRAY
else:
button9.bgcolor = LIGHTGRAY
if 'click' in button10.handleEvent(event):
whichNonLin10 = 1 - whichNonLin10
if whichNonLin10==1:
button10.bgcolor = GRAY
else:
button10.bgcolor = LIGHTGRAY
if 'click' in button11.handleEvent(event):
whichNonLin11 = 1 - whichNonLin11
if whichNonLin11==1:
button11.bgcolor = GRAY
else:
button11.bgcolor = LIGHTGRAY
if 'click' in button12.handleEvent(event):
whichNonLin12 = 1 - whichNonLin12
if whichNonLin12==1:
button12.bgcolor = GRAY
else:
button12.bgcolor = LIGHTGRAY
# All drawing code happens after the for loop and but
# inside the main while done==False loop.
# Clear the screen and set the screen background
screen.fill(black)
buttonGo.draw(screen)
buttonSave.draw(screen)
button1.draw(screen)
button2.draw(screen)
button3.draw(screen)
button4.draw(screen)
button5.draw(screen)
button6.draw(screen)
button7.draw(screen)
button8.draw(screen)
button9.draw(screen)
button10.draw(screen)
button11.draw(screen)
button12.draw(screen)
if calculated:
# # Draw points
drawnPoints = 0
for (a,b) in pointsXY.keys():
drawnPoints +=1
pygame.draw.rect(screen,color[hitNumberXY[(a,b)]-1],[a,b,1,1],1)
if drawnPoints >50000:
break
# for i in range(length):
# j = (float)(i)/length
# color = (colorCon(j,1),
# colorCon(j,2),colorCon(j,3))
# # print(color)
# pygame.draw.rect(screen,color,[(int)((pointsX[i]/4+0.5)*width),(int)((pointsY[i]/4+0.5)*height),2,2])
# Select the font to use. Default font, 25 pt size.
font = pygame.font.Font(None, 25)
# Go ahead and update the screen with what we've drawn.
# This MUST happen after all the other drawing commands.
pygame.display.flip()
# Be IDLE friendly
pygame.quit ()
main()