def mk_caracoles(): A = np.array([[[ 0.5 ,-0.2 ],[ 0.2 , 0.6 ]], [[ 0.75 , 0.5 ],[-0.6 , 0.4 ]]]) b = np.array([[-1.0 ,-2.0 ], [-1.0 , 2.0 ]]) next_transform = rv(name='Caracoles', values=((0,1),(0.42,0.58))) return ChaosGame(A,b,next_transform)
def mk_sierpinski(): A = np.array([[[ 0.50, 0.00],[ 0.00, 0.50]], [[ 0.50, 0.00],[ 0.00, 0.50]], [[ 0.50, 0.00],[ 0.00, 0.50]]]) b = np.array([[ 0.00, 0.00], [ 0.50, 0.00], [ 0.25, 0.25]]) next_transform = rv(name='Sierpinski',values=((0,1,2),(0.33,0.33,0.33))) return ChaosGame(A,b,next_transform)
def mk_cristales(): A = np.array([[[ 0.5 , 0.0 ],[ 0.0 ,-0.5 ]], [[ 0.5 , 0.0 ],[ 0.0 ,-0.5 ]], [[ 0.5 , 0.0 ],[ 0.0 ,-0.5 ]]]) b = np.array([[ 0.0 , 0.0 ], [ 0.5 , 0.0 ], [ 0.25 , 0.5 ]]) next_transform = rv(name='Cristales', values=((0,1,2),(0.333,0.333,0.334))) return ChaosGame(A,b,next_transform)
def mk_koch(): A = np.array([[[ 0.33, 0.00],[ 0.00, 0.33]], [[ 0.33, 0.00],[ 0.00, 0.33]], [[ 0.16,-0.28],[ 0.28, 0.16]], [[-0.16, 0.28],[ 0.28, 0.16]]]) b = np.array([[ 0.00, 0.00], [ 0.66, 0.00], [ 0.33, 0.00], [ 0.66, 0.00]]) next_transform = rv(name='Fern',values=((0,1,2,3),(0.25,0.25,0.25,0.25))) return ChaosGame(A,b,next_transform)
def mk_fern(): A = np.array([[[ 0.00, 0.00],[ 0.00, 0.16]], [[ 0.85, 0.04],[-0.04, 0.85]], [[ 0.20,-0.26],[ 0.23, 0.22]], [[-0.15, 0.28],[ 0.26, 0.24]]]) b = np.array([[ 0.00, 0.00], [ 0.00, 1.60], [ 0.00, 1.60], [ 0.00, 0.44]]) next_transform = rv(name='Fern',values=((0,1,2,3),(0.01,0.85,0.07,0.07))) return ChaosGame(A,b,next_transform)
def mk_bronchi(): A = np.array([[[ 0.05, 0.00],[ 0.00, 0.50]], [[ 0.05, 0.00],[ 0.00,-0.50]], [[ 0.00,-0.80],[ 0.60, 0.00]], [[ 0.00, 0.80],[-0.65, 0.00]]]) b = np.array([[ 0.00, 0.00], [ 0.00, 0.80], [ 0.00, 0.80], [ 0.00, 0.80]]) next_transform = rv(name='Bronchi',values=((0,1,2,3),(0.15,0.15,0.35,0.35))) return ChaosGame(A,b,next_transform)
def mk_molinos(): A = np.array([[[ 0.02 , 0.0 ],[ 0.0 , 0.5 ]], [[ 0.02 , 0.0 ],[ 0.0 ,-0.5 ]], [[ 0.5303307,-0.5303307],[ 0.5303307, 0.5303307 ]], [[ 0.0 , 0.5 ],[-0.5 , 0.0 ]]]) b = np.array([[ 0.0 , 0.0 ], [ 0.0 , 0.5 ], [ 0.0 , 1.0 ], [ 0.0 , 1.2 ]]) next_transform = rv(name='Molinos', values=((0,1,2,3),(0.145,0.145,0.426,0.284))) return ChaosGame(A,b,next_transform)
def mk_hoja(): A = np.array([[[ 0.64987,-0.013 ],[ 0.013 , 0.64987]], [[ 0.64948,-0.026 ],[ 0.026 , 0.64948]], [[ 0.3182 ,-0.3182],[ 0.3182 , 0.3182 ]], [[-0.3182 , 0.3182],[ 0.3182 , 0.3182 ]]]) b = np.array([[ 0.175, 0.00 ], [ 0.165, 0.325], [ 0.2 , 0.00 ], [ 0.8 , 0.00 ]]) next_transform = rv(name='Helecho', values=((0,1,2,3),(0.3333,0.3333,0.1667,0.1667))) return ChaosGame(A,b,next_transform)
def mk_helecho(): A = np.array([[[ 0.00000, 0.0000],[ 0.00 , 0.17000]], [[ 0.84962, 0.0255],[-0.0255 , 0.84962]], [[-0.15540, 0.2350],[ 0.19583, 0.18648]], [[ 0.1554 ,-0.235 ],[ 0.19583, 0.18648]]]) b = np.array([[ 0.00, 0.00], [ 0.00, 3.00], [ 0.00, 1.20], [ 0.00, 3.00]]) next_transform = rv(name='Helecho', values=((0,1,2,3),(0.0572,0.5724,0.1852,0.1852))) return ChaosGame(A,b,next_transform)
def mk_vicsek(): A = np.array([[[ 0.333333, 0.0 ],[ 0.0 , 0.333333 ]], [[ 0.333333, 0.0 ],[ 0.0 , 0.333333 ]], [[ 0.333333, 0.0 ],[ 0.0 , 0.333333 ]], [[ 0.333333, 0.0 ],[ 0.0 , 0.333333 ]], [[ 0.333333, 0.0 ],[ 0.0 , 0.333333 ]]]) b = np.array([[ 0.0 , 0.0 ], [ 2.0 , 0.0 ], [ 1.0 , 1.0 ], [ 0.0 , 2.0 ], [ 2.0 , 2.0 ]]) next_transform = rv(name='Vicsek', values=((0,1,2,3,4),(0.2,0.2,0.2,0.2,0.2))) return ChaosGame(A,b,next_transform)
class Fern: A = np.array([[[0.0, 0.0], [0.00, 0.16]], [[0.85, 0.04], [-0.04, 0.85]], [[0.2, -0.26], [0.23, 0.22]], [[-0.15, 0.28], [0.26, 0.24]]]) b = np.array([[0.00, 0.00], [0.00, 1.60], [0.00, 1.60], [0.00, 0.44]]) #next_transform = rv(name='Fern',values=((0,1,2,3),(0.01,0.85,0.0,0.14))) next_transform = rv(name='Fern', values=((0, 1, 2, 3), (0.01, 0.85, 0.07, 0.07))) def simplify_axes(self, ax): ax.set_yticklabels([]) ax.set_xticklabels([]) ax.set_yticks([]) ax.set_xticks([]) ax.set_xlim(-2.3, 2.8) ax.set_ylim(-0.1, 10.1) def next_point(self, last_point): transform = self.next_transform.rvs() return np.dot(self.A[transform], last_point) + self.b[transform] def plot(self): current = np.array([0.0, 0.0]) fig = plt.figure() ax = fig.add_subplot(111) self.simplify_axes(ax) # Don't plot the first few iterations. for x in range(15): current = self.next_point(current) ax.plot(current[0], current[1], 'go', markersize=1) xs, ys = [], [] line_fern, = ax.plot(xs, ys, 'go', markersize=1) for x in range(60000): current = self.next_point(current) xs.append(current[0]) ys.append(current[1]) line_fern.set_data(xs, ys) plt.show()
def mk_arbol(): A = np.array([[[ 0.05 , 0.00 ],[ 0.00 , 0.6 ]], [[ 0.05 , 0.00 ],[ 0.00 ,-0.5 ]], [[ 0.46 ,-0.32 ],[ 0.39 , 0.38 ]], [[ 0.47 ,-0.15 ],[ 0.17 , 0.42 ]], [[ 0.43 , 0.28 ],[-0.25 , 0.45 ]], [[ 0.42 , 0.26 ],[-0.35 , 0.31 ]]]) b = np.array([[ 0.00 , 0.00 ], [ 0.00 , 1.00 ], [ 0.00 , 0.60 ], [ 0.00 , 1.10 ], [ 0.00 , 1.00 ], [ 0.00 , 0.70 ]]) next_transform = rv(name='Arbol', values=((0,1,2,3,4,5),(0.1,0.1,0.2,0.2,0.2,0.2))) return ChaosGame(A,b,next_transform)
import numpy as np import matplotlib.pyplot as plt from scipy.stats import rv_discrete as rv A = np.array([[[0.0,0.0],[0.00,0.16]], [[0.85,0.04],[-0.04,0.85]], [[0.2,-0.26],[0.23,0.22]], [[-0.15,0.28],[0.26,0.24]]]) b = np.array([[0.00,0.00], [0.00,1.60], [0.00,1.60], [0.00,0.44]]) #next_transform = rv(name='Fern',values=((0,1,2,3),(0.01,0.85,0.0,0.14))) next_transform = rv(name='Fern',values=((0,1,2,3),(0.01,0.85,0.07,0.07))) def simplify_axes(ax): ax.set_yticklabels([]) ax.set_xticklabels([]) ax.set_yticks([]) ax.set_xticks([]) ax.set_xlim(-2.3,2.8) ax.set_ylim(-0.1,10.1) def next_point(last_point): transform = next_transform.rvs() return np.dot(A[transform],last_point) + b[transform] def main():
""" Animated construction of the Sierpinski gasket using the chaos game. """ import random import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from scipy.stats import rv_discrete as rv A = np.array([[[0.0, 0.0], [0.00, 0.16]], [[0.85, 0.04], [-0.04, 0.85]], [[0.2, -0.26], [0.23, 0.22]], [[-0.15, 0.28], [0.26, 0.24]]]) b = np.array([[0.00, 0.00], [0.00, 1.60], [0.00, 1.60], [0.00, 0.44]]) next_transform = rv(name='Fern', values=((0, 1, 2, 3), (0.01, 0.85, 0.07, 0.07))) xs, ys = [], [] def simplify_axes(ax): ax.set_yticklabels([]) ax.set_xticklabels([]) ax.set_yticks([]) ax.set_xticks([]) ax.set_xlim(-2.3, 2.8) ax.set_ylim(-0.1, 10.1) # int(last_point): # transform = next_transform.rvs()