Press "Enter" to skip to content

Generalized Sierpinski chaotic polygon

The script below draws polygonal fractals similar to Sierpinski triangle. The number of vertices is arbitrary. The algorithm is based on the chaos game, which is described, for example, on this Wikipedia page.

import numpy  as np
import matplotlib.pyplot as plt
import random

vertices = np.array([[0, 0], [1, 0], [0,1]])

vert_no = np.shape(vertices)[0]

for i in range(0,vert_no):
    plt.plot(vertices[i][0], vertices[i][1], marker="o", color="red")

points = 5000
start = vertices[0]
ratio = 0.5

for i in range(0,points):
    m = random.randint(0,vert_no-1)
    target = vertices[m]
    point = start + ratio*(target-start)
    plt.plot(point[0],point[1],marker = ",", color = "black")
    start = point
The program defines only three vertices, thus generating the triangle above.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *