forked from markmbadham/pythoncourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaoes_game.py
53 lines (49 loc) · 1.58 KB
/
chaoes_game.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
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 20 14:52:43 2014
@author: mark
"""
import random
import numpy as np
import time
import matplotlib.pyplot as pl #!
import numpy as np
def Sierpinski_Chaos(gp=None,n=None):
""" This Chaos game shows how randomness can lead to structure:
1. 3 points have to be drawn in the plane
2. Mark a triangle with these points with pl.plot
3. draw a random point outside this triangle- this is the first 'Game-Point(gp)'
Repeat til n:
4. Choose a randomly a base-point out of the three corner points
5. Build the vector between the 'Game-Point' and the randomly chosen base-point
and mark a point (scatter) half way to the base-point
Created 08-29-2014 by Pierre Noire """
#1.,2.
pl.plot([0,4,2,0],[0,0,4,0])
pl.xlim(0,4)
pl.ylim(0,4)
base_points=[[0,0],[4,0],[2,4]]
if gp==None:
gp=np.array([5,5])#starting game_point
if n==None:
n=500 #number of iterations
for n in range(500):
gp_log=gp.copy()
pl.scatter(gp[0],gp[1],lw='0',s=20)#3.
pl.xlim(0,4)#!
pl.ylim(0,4)#!
pl.draw()#!
pl.show()
#display.clear_output(wait=True)#!
#display.display(pl.gcf())#!
time.sleep(0.0000005)#!
#4
fort_wheel=random.choice(base_points)
rand_base=np.array(fort_wheel)
#5
gp=gp-1.0/2*(gp-rand_base)
gp_log=np.concatenate((gp_log,gp))
#(gp-rand_base) is
#"direction-vector" starting from the gp and just walking half way leads to new gp
return gp_log
Sierpinski_Chaos()