[英]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.