-
Notifications
You must be signed in to change notification settings - Fork 0
/
anima3.py
76 lines (57 loc) · 1.85 KB
/
anima3.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
import pylab
from pylab import matplotlib
import time
import numpy as np
pylab.ion()
n=30
m=1
gn = 100
step = 1
xstep=2*step
ystep=step
spacing = 1
iterations = 50
centre = np.array([gn/2,gn/2],).astype(int)
#colour = zip(np.arange(0,1.,1./(n*m)),np.arange(1,0,-1./(m*n)),np.arange(0.5,0.9,0.4/(n*m)))
tstart = time.time() # for profiling
ogrid = np.lib.index_tricks.nd_grid(sparse=False)
grid = ogrid[0:n,0:n].astype(int)
#print '* %d %d %d %d' % (np.min(grid[0].flatten()), np.max(grid[0].flatten()), np.min(grid[1].flatten()), np.max(grid[1].flatten()))
x = np.tile(grid[0].flatten(),m)
y = np.tile(grid[1].flatten(),m)
#print '* %d %d %d %d' % (np.min(x), np.max(x), np.min(y), np.max(y))
x = centre[0]+x*spacing-spacing*n/2
y = centre[1]+y*spacing-spacing*n/2
#print '* %d %d %d %d' % (np.min(x), np.max(x), np.min(y), np.max(y))
blobs = []
#pylab.figure()
#pylab.ioff()
for i in range(len(x)):
if np.abs(x[i]-centre[0])+np.abs(y[i]-centre[1]) <= n/2:
#blob, = pylab.plot(x[i:i+1],y[i:i+1], marker='o', color=colour[i % len(colour) ], alpha=0.2)
blob, = pylab.plot(x[i:i+1],y[i:i+1], marker='o')
blob.set_color((1,0,0))
else:
blob, = pylab.plot(x[i:i+1],y[i:i+1], marker='o')
blob.set_color((0,0,1))
blobs.append(blob)
#print '%d %d %d %d %d' % (i, min(x), max(x), min(y), max(y))
pylab.axis([0,100,0,100],'equal')
#pylab.ion()
pylab.draw()
print "Press return to continue ..."
raw_input()
for j in np.arange(iterations):
pylab.ioff()
x = x+xstep*(2*np.random.binomial(1,0.5,n*n)-1)
y = y+ystep*(2*np.random.binomial(1,0.5,n*n)-1)
#print '%d %d %d %d %d' % (j, np.min(x), np.max(x), np.min(y), np.max(y))
#~ # pylab.clf()
for i in np.arange(len(x)):
blobs[i].set_xdata([x[i:i+1]])
blobs[i].set_ydata([y[i:i+1]])
pylab.title('Iteration '+str(j+1))
pylab.ion()
pylab.draw() # redraw the canvas
print "Press return to continue ..."
raw_input()