#---------------------------------------------------------------------- # Sierpinski Gasket # # Copyright (C) March 2, 2018 -- Dr. William T. Verts # # Here the code has been rewritten to use points (lists of [X,Y] # coordinates). This requires a function to compute the average # of two points. All the rest of the code is now based off of # lists as well: Points is a list of the three triangle vertices, # and Colors is a list of the three corner colors. Rather than # explicitly test if N==0, if N==1, etc., we now use N as the # index into the lists. The code packs down considerably as a # result. #---------------------------------------------------------------------- import random def Average (P,Q): # P and Q are both [X,Y] points X = (P[0] + Q[0]) / 2 Y = (P[1] + Q[1]) / 2 return [X,Y] Canvas = makeEmptyPicture(640,480) P0 = [getWidth(Canvas) / 2,0] P1 = [0,getHeight(Canvas) - 1] P2 = [getWidth (Canvas) - 1,getHeight(Canvas) - 1] PP = [P0[0],P0[1]] Points = [P0,P1,P2] Colors = [magenta,cyan,yellow] Counter = 0 while True: N = random.randint(0,2) PP = Average(PP, Points[N]) setColor(getPixel(Canvas,PP[0],PP[1]),Colors[N]) Counter = Counter + 1 if (Counter >= 1000): repaint(Canvas) Counter = 0