# # tsquare: recursive function for producing quickdraw commands that render the tsquare fractal # # Parameters: # cx, cy: coordinates specifying the center of the current square being drawn # w, h: width and height of the current square # # Returns: # no return value, requrired quickdraw commands are output to stdout # def tsquare(cx, cy, w, h): # # draw square centered at (cx,cy) of width w-1 and height h-1 # color to use is selected dependent on current width and height (so a different color is # used for each level of the recursion) # print "color",255,w-1,256-h print "fillrect",cx - w/2, cy - h/2, w, h # # recursively draw squares centered at each corner of the current square of half the current # width and height. If current width is < 4, then do not continue the recursion (base case) # if w >= 4: tsquare(cx - w/2, cy - h/2, w/2, h/2); tsquare(cx + w/2, cy - h/2, w/2, h/2); tsquare(cx - w/2, cy + h/2, w/2, h/2); tsquare(cx + w/2, cy + h/2, w/2, h/2); # # draw tsquare fractal, starting with a square 256x256 pixels, centered at (400,300) # def main(): tsquare(400, 300, 256, 256) main()