import random
import numpy as np
from matplotlib import pyplot as plt
# set point coordinates
x = [1, 1.5, 2]
y = [1, 1+np.sqrt(.75), 1]
ry = 1
rx = np.random.rand(1,) + 1
start = [ry, rx]
a, b, c = zip(x, y)
# set list of vertices for random choice
direction = [a, b, c]
def rand_dir(dirc):
return np.array(random.choice(dirc))
def next_point(array, array2):
return (array + array2) * .5
plt.figure(figsize=(10, 10))
# plot triangle
plt.scatter(x, y)
# plot initial random point
plt.scatter(rx, ry)
n = 100000
for i in range(n):
tri = rand_dir(direction)
start = next_point(tri, start)
point = plt.scatter(start[0], start[1], s=5)
plt.savefig('ChaosGameTriangle'+str(n)+'.png')
plt.show()
效果:
![](https://img.haomeiwen.com/i1114626/83e08c91881a74d6.png)
网友评论