I am trying to draw a recursive structure in python using the turtle module. Essentially, the function to draw this structure is drawFrames(d), where d is the ‘depth’, i.e. a number specifying the complexity of the fractal. d=1 just splits the window into 4 rectangles (no recursion); d=2 splits each of those rectangular sections into 4 more rectangles, d=3 splits each of those , etc…
I’m finding trouble debugging the recursive case for my function. The bug is definitely in the helper function for drawFrames (drawFramesHelper), and I think it’s a mathematical or computational error rather than an error in reasoning with the recursion (I think my reasoning isn’t wrong, as I’m just calling the function recursively on each of the subrectangles I draw, which should work). Any help in debugging would be appreciated!
import turtle def drawFrames(d): """Draws the fractal with depth [d] """ t = turtle.Turtle() width = t.window_width() height = t.window_height() turtle.setworldcoordinates(0,0,width,height) #lowerleft corner is origin hwidth = width/2 hheight = height/2 uleft = (-hwidth, hheight) uright= (hwidth, hheight) lleft= (-hwidth, -hheight) lright = (hwidth, -hheight) drawFramesHelper(t, uleft, uright, lleft, lright, d); def drawFramesHelper(t, ul, ur, ll, lr, depth): """Helper for drawFrames. [t] is a turtle object [ul], [ur] ,[ll] and [lr] are tuples specifying the coordinates of the upperleft, upperright, lowerleft and lowerright positions of the grid. [depth] is the specified depth of the fractal to draw """ #dimensions of the window to draw on height= ul - ll width = ur - ul #origin is the lowerleft corner of the 'grid' + (width/2, height/2) origin_x = ll + width/2 origin_y = ll + height/2 if depth == 0: return if depth == 1: #upper right rectangle drawRectangle(t, width/2, height/2, origin_x, origin_y) #upper left rectangle drawRectangle(t, width/2, height/2, origin_x-width/2, origin_y) #lower left rectangle drawRectangle(t, width/2, height/2, origin_x-width/2, origin_y-height/2) #lower right rectangle drawRectangle(t, width/2, height/2, origin_x, origin_y-height/2) return; else: #get the midpoints of each boundary of grid top_midpt = (origin_x, 2*origin_y) left_midpt = (-origin_x, origin_y) right_midpt = (2*origin_x, origin_y) bottom_midpt = (origin_x, -origin_y) middle = (origin_x, origin_y) #call on upper right drawFramesHelper(t, top_midpt, ur, middle, right_midpt, depth-1) #call on upper left drawFramesHelper(t, ul, top_midpt, left_midpt, middle, depth-1) #call on lower left drawFramesHelper(t, left_midpt, middle, ll, bottom_midpt, depth-1) #call on lower right drawFramesHelper(t, middle, right_midpt, bottom_midpt, lr, depth-1) def drawRectangle(t, width, height, start_x, start_y): """Draws a rectangle with given width and height with lower left cornder at (start_x, start_y) [t] is a turtle object """ #move to position t.penup() t.goto(start_x, start_y); t.pendown() #draw rectangle t.forward(width) t.left(90) t.forward(height) t.left(90) t.forward(width) t.left(90) t.forward(height) t.left(90) if __name__ == "__main__": drawFrames(2) #testing recursive case, right now doesn't work
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!