画分形图的一个例子

2020-08-31  本文已影响0人  水之心
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()

效果:

上一篇下一篇

猜你喜欢

热点阅读