迭代法動態生成謝爾賓斯基三角形

定義初始點和隨機點

import matplotlib.pyplot as plt
import numpy as np
colors = ['r','g','b']
x0 = (0,0)
x1 = (10, 0)
x2 = (5, np.sqrt(75))
z = (3,4)

開始迭代

%matplotlib inline
# 繪製初始地圖
fig = plt.figure()
# 1*1網格,第一子圖
ax1 = fig.add_subplot(111)
ax1.set_title('triangle')
plt.xlabel('X')
plt.ylabel('Y')
# 打開交互模式
plt.ion()
plt.scatter(x0[0], x0[1], c=colors[0], marker='o')
plt.scatter(x1[0], x1[1], c=colors[1], marker='o')
plt.scatter(x2[0], x2[1], c=colors[2], marker='o')
points = [x0, x1, x2]
import numpy as np
from IPython import display
for i in range(600):
    index = np.random.choice(3)
    z  = ((z[0]+points[index][0])/2, (z[1]+points[index][1])/2)
    display.clear_output(wait=True)
    points.append(z)
    colors.append(colors[index])
    for point, color in zip(points, colors):
        plt.scatter(point[0], point[1], c=color, marker='o')
    plt.pause(0.00000001)
plt.draw()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章