Reputation: 464
This is the code for generating the fractals.
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)
At the end I don't know how to visualize the fractals.
I think it has to do with matplot.lib but I'm not sure how
Upvotes: 0
Views: 1347
Reputation: 764
Although matplotplib
is primarily suited for plotting graphs, but you can draw points and polygons using it if you wish as well; see also: How to draw a triangle using matplotlib.pyplot based on 3 dots (x,y) in 2D?
For instance, to compose a Sierpinski triangle from polygons, and plot those polygons onto a figure:
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()
The above code generates the following image output for me:
Upvotes: 1