# Tapis de Sierpinski

from tkinter import *

def dessiner_rectangle(x, y, coul):
    global niveau
    taille=729//(3**niveau)
    can.create_rectangle(taille*x, taille*y, taille*(x+taille), taille*(y+taille), fill=coul, outline=coul)
 
def dans_tapis(x, y):
    while True:
        if x == 0 or y == 0:
            return True
        elif x % 3 == 1 and y % 3 == 1:
            return False
        x //= 3
        y //= 3
 
def tapis(n):
    for i in range(3**n):
        for j in range(3**n):
            if dans_tapis(i, j):
                dessiner_rectangle(i, j, "black")
            else:
                dessiner_rectangle(i, j, "white")


fen = Tk()
can = Canvas(fen,bg='white',height=729,width=729)
can.pack(side=LEFT)
niveau = 4  # maximum 6
tapis(niveau)
