Está en la página 1de 1

Alfombra de Sierpinski

[0]: import matplotlib.pyplot as plt

def sierpinski(x, y, size, depth):


if depth == 0:
# Dibujar un solo cuadrado
square = plt.Rectangle((x, y), size, size, linewidth=0,
,→facecolor='black')

plt.gca().add_patch(square)
else:
# Dibujar recursivamente 8 cuadrados más pequeños
sierpinski(x, y, size/3, depth-1)
sierpinski(x+size/3, y, size/3, depth-1)
sierpinski(x+2*size/3, y, size/3, depth-1)
sierpinski(x, y+size/3, size/3, depth-1)
sierpinski(x+2*size/3, y+size/3, size/3, depth-1)
sierpinski(x, y+2*size/3, size/3, depth-1)
sierpinski(x+size/3, y+2*size/3, size/3, depth-1)
sierpinski(x+2*size/3, y+2*size/3, size/3, depth-1)

# Diagrama
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')

# Sierpinski con x=0, y=0, size=1, y depth=3


# Cambiar el último número para diferentes niveles
sierpinski(0, 0, 1, 4)

# Mostrar diagrama
# plt.show()
# Guardar imagen
plt.savefig('sierpinski-cuadrado.png')

También podría gustarte