#---------------------------------------------------------------------- # Sierpinski Gasket Fractal # # Made more efficient. # # Copyright (C) October 24, 2018 -- Dr. William T. Verts #---------------------------------------------------------------------- import random def Average (Q0,Q1): # Q0 and Q1 are both [X,Y] point lists X = (Q0[0] + Q1[0]) / 2 Y = (Q0[1] + Q1[1]) / 2 return [X,Y] def Main(): W = 800 H = 600 Canvas = makeEmptyPicture(W,H) P0 = [W / 2, 0] P1 = [0,H-1] P2 = [W-1,H-1] P = [P0[0],P0[1]] Counter = 10000 Colors = [red,green,blue] Points = [P0,P1,P2] while True: N = random.randrange(3) P = Average(P, Points[N]) setColor(getPixel(Canvas,P[0],P[1]),Colors[N]) Counter = Counter - 1 if Counter == 0: repaint(Canvas) Counter = 10000 return