#---------------------------------------------------------------------- # Sierpinski Gasket fractal VERSION 2 # # Copyright (C) October 6, 2017 -- Dr. William T. Verts # # IMPROVED STRUCTURE: using lists of [X,Y] values to represent points # on the plane. Also using lists for table-lookup (Colors and Points). #---------------------------------------------------------------------- import random def AveragePoints (P,Q): # P and Q are both [X,Y] lists, representing points in the plane X = (P[0] + Q[0]) / 2 Y = (P[1] + Q[1]) / 2 return [X,Y] def Main(): Canvas = makeEmptyPicture(1000,700) show(Canvas) P0 = [getWidth(Canvas) / 2,0] # Point P0 fixed on top row, center P1 = [0, getHeight(Canvas) - 1] # Point P1 fixed at lower left corner P2 = [getWidth(Canvas) - 1,getHeight(Canvas) - 1] # Point P2 fixed at lower right corner P = [P0[0],P0[1]] # Point P allowed to move around Count = 0 Colors = [red,green,blue] # Random number N is used to pick... Points = [P0,P1,P2] # ...items from these lists. while (True): N = random.randrange(3) # Pick random number 0, 1, or 2 P = AveragePoints(P, Points[N]) # Average P with indicated point setColor(getPixel(Canvas,P[0],P[1]),Colors[N]) # Set Pixel P to the indicated color Count = Count + 1 if Count == 10000: repaint(Canvas) Count = 0 return # Never reached! Loop is infinite!