Skip to content

Instantly share code, notes, and snippets.

@Mukundan314
Created June 30, 2018 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mukundan314/08128dbc2cbb5ab04e3c659b6b206cf7 to your computer and use it in GitHub Desktop.
Save Mukundan314/08128dbc2cbb5ab04e3c659b6b206cf7 to your computer and use it in GitHub Desktop.
Python code to generate sierpinski carpet.
import numpy as np
import matplotlib.pyplot as plt
def fill_square(x1, y1, size, img):
for x in range(x1, x1+size):
for y in range(y1, y1+size):
img[x, y] = 0
return img
levels = int(input("Level: "))
size = 3**levels
img = np.ones((size, size), dtype=np.uint8)
for level in range(1, levels+1):
square_size = int(size/(3**level))
for x in range(0, 3**level, 3):
x = int((x+1)*square_size)
for y in range(0, 3**level, 3):
y = int((y+1)*square_size)
img = fill_square(x, y, square_size, img)
plt.axis('off')
plt.imshow(img, cmap='binary')
plt.imsave('sierpinski.png', img, cmap='binary')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment