#ShowTriPartition.py """ Illustrates the recursive procedure Partition. This procedure ``tiles'' a given triangle with smaller triangles. """ from simpleGraphicsE import * def Partition(x,y,L): """ Draws a level L partitioning of the triangle whose vertices are defined by x and y. PreCond: x and y are length-3 lists of numbers and L is a nonnegative int. """ map(float,x) map(float,y) if L==0: DrawPoly(x,y,color=YELLOW) else: # Compute the midpoint coordinates. a = [(x[0]+x[1])/2,(x[1]+x[2])/2,(x[2]+x[0])/2] b = [(y[0]+y[1])/2,(y[1]+y[2])/2,(y[2]+y[0])/2] # Paint the inner triangle DrawPoly(a,b,color=MAGENTA) # Partition each of the corner triangles Partition([x[0],a[0],a[2]],[y[0],b[0],b[2]],L-1) Partition([x[1],a[0],a[1]],[y[1],b[0],b[1]],L-1) Partition([x[2],a[1],a[2]],[y[2],b[1],b[2]],L-1) if __name__ == '__main__': x = [-5.,5.,3.] y = [-5.5,-5.,5.] # Display level-0, level-1, level-2, level-3, and level-4 partitionings # of the same triangle MakeWindow(6,bgcolor=WHITE,labels=False) Partition(x,y,0) MakeWindow(6,bgcolor=WHITE,labels=False) Partition(x,y,1) MakeWindow(6,bgcolor=BLACK,labels=False) Partition(x,y,2) MakeWindow(6,bgcolor=BLACK,labels=False) Partition(x,y,3) MakeWindow(6,bgcolor=BLACK,labels=False) Partition(x,y,4) ShowWindow()