Skip to content

adddou/vegetative

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

vegetative

-- coding: utf-8 --

''' Draw a Barnsley Fern,fractal的经典模型 ''' from matplotlib import pyplot as plt import random

#四个规则 def transformation_1(p): x = p[0] y = p[1] x1 = 0.85x + 0.04y y1 = -0.04x + 0.85y + 1.6 return x1, y1

def transformation_2(p): x = p[0] y = p[1] x1 = 0.2x - 0.26y y1 = 0.23x + 0.22y + 1.6 return x1, y1

def transformation_3(p): x = p[0] y = p[1] x1 = -0.15x + 0.28y y1 = 0.26x + 0.24y + 0.44 return x1, y1

def transformation_4(p): x = p[0] y = p[1] x1 = 0 y1 = 0.16*y return x1, y1

#产生非匀称随机数,其原理和下注系统类似
def get_index(probability): r = random.random() c_probability = 0 sum_probability = [] for p in probability: c_probability += p sum_probability.append(c_probability) for item, sp in enumerate(sum_probability): if r <= sp: return item return len(probability)-1

def transform(p): # List of transformation functions transformations = [transformation_1, transformation_2, transformation_3, transformation_4] probability = [0.85, 0.07, 0.07, 0.01] # Pick a random transformation function and call it tindex = get_index(probability) #测试 list_t.append(tindex) t = transformations[tindex] x, y = t(p) return x, y

def draw_fern(n): # We start with (0, 0) x = [0] y = [0] x1, y1 = 0, 0 for i in range(n): x1, y1 = transform((x1, y1)) x.append(x1) y.append(y1) return x, y

list_t=[]
n=10000 x,y=draw_fern(n) plt.plot(x, y, 'o') plt.title('Fern with {0} points'.format(n)) plt.show()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published