[英]How to visualize fractals in python
這是生成分形的代碼。
import matplotlib.pyplot as plt
def makes(self, fractal):
if (fractal == "SierpinskiTriangle"):
SierpinskiTriangle(self.dimensions)
for i in range(len(SierpinskiTriangle.verticies)):
plotPoint(i, self.vertexColor, self.vertexRadius)
for i in range(SierpinskiTriangle.numPoints):
listVertices = SierpinskiTriangle.verticies
randVert = randint(0, len(listVertices)-1)
newVertexPoint = listVertices[randVert]
m1 = Point.midpt(m1, newVertexPoint)
self.plot(m1)
elif (fractal == "SierpinskiCarpet"):
SierpinskiCarpet(self.dimensions)
for i in range(len(SierpinskiCarpet.verticies)):
plotPoint(i, self.vertexColor, self.vertexRadius)
for i in range(SierpinskiCarpet.numPoints):
listVertices = SierpinskiCarpet
randVert = randint(0, len(listVertices)-1)
newVertexPoint = listVertices[randVert]
m1 = Point.midpt(m1, newVertexPoint)
self.plot(m1)
else:
Pentagon(self.dimensions)
for i in range(len(Pentagon.verticies)):
plotPoint(i, self.vertexColor, self.vertexRadius)
for i in range(Pentagon.numPoints):
listVertices = SierpinskiCarpet
randVert = randint(0, len(listVertices)-1)
newVertexPoint = listVertices[randVert]
m1 = Point.midpt(m1, newVertexPoint)
self.plot(m1)
最后我不知道如何可視化分形。
我認為它與 matplot.lib 有關,但我不確定如何
盡管matplotplib
主要適用於繪制圖形,但如果您願意,也可以使用它繪制點和多邊形; 另請參閱: 如何使用基於 2D 中的 3 個點 (x,y) 的 matplotlib.pyplot 繪制三角形?
例如,要從多邊形組成謝爾賓斯基三角形,並將這些多邊形繪制到圖形上:
import numpy as np
import matplotlib.pyplot as plt
MAX_LEVEL = 6
def sierpinski(p1, p2, p3, level=0):
if level >= MAX_LEVEL:
yield plt.Polygon([p1, p2, p3], color='red')
return
yield from sierpinski(p1, (p1+p2) / 2, (p1+p3) / 2, level+1)
yield from sierpinski((p1+p2) / 2, p2, (p2+p3) / 2, level+1)
yield from sierpinski((p1+p3) / 2, (p2+p3) / 2, p3, level+1)
plt.figure()
plt.scatter([0, 0, 10, 10], [0, 10, 0, 10], color='blue')
for patch in sierpinski(
np.array([1.0, 1.0]), np.array([9.0, 1.0]), np.array([5.0, 9.0])):
plt.gca().add_patch(patch)
plt.show()
上面的代碼為我生成了以下圖像輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.