import math
from PIL import Image, ImageDraw
from random import randint

width = 1748
height = 1240
vonkoch = Image.new('RGB', (width,height), (255,255,255))
draw = ImageDraw.Draw(vonkoch)

def koch(A,B,n,couleur):
    if (n==0):
        draw.line((tuple(A), tuple(B)), fill=couleur, width=1)
    else:
        M2 = sim(A,B,math.pi/6,1/math.sqrt(3))
        koch(M2,A,n-1,couleur)
        koch(B,M2,n-1,couleur)
    
def sim(A,B,alpha,mu):
    C = [B[0]-A[0],B[1]-A[1]]
    D = [mu*(C[0]*math.cos(alpha)+C[1]*math.sin(alpha)),mu*(-C[0]*math.sin(alpha)+C[1]*math.cos(alpha))]
    return [A[0]+D[0],A[1]+D[1]]

def flocon(A,B):
    couleur = (randint(0,255),randint(0,255),randint(0,255))
    C = sim(B,A,math.pi/3,1)
    koch(A,B,14,couleur)
    koch(B,C,14,couleur)
    koch(C,A,14,couleur)

L = 550
for i in range(4):
    for j in range(4):
        A0 = [1748/2+L/2+L*(i-1),height/2+L*math.sqrt(3)/6+L*2/3*math.sqrt(3)*(j-1)+L/3*math.sqrt(3)*(i-1)]
        B0 = [1748/2-L/2+L*(i-1),height/2+L*math.sqrt(3)/6+L*2/3*math.sqrt(3)*(j-1)+L/3*math.sqrt(3)*(i-1)]
        flocon(A0,B0)

vonkoch.show()