Turtling in circles

Effid_by_8dfineartHi, I'm Effid.  You're the Turtle coder, right?  I've got another challenge you might like!

Now that you have polygon, why don't you make a spell-function to build a circle?  One way to do it would just be to build a polygon with a lot of small sides.  Since we want to compute the size using pi, we'll have to import the math library first:

import math
def circle( t, r ):
  circumference = 2 * math.pi * r
  n = 50
  length = circumference / n
  polygon( t, n, length )

If you try this, be sure to include your code for polygon before the function for circle.  Try it out with a few circles; be sure to try some with pretty small radii and some with really big radii.

You probably noticed the problem already.  With a fixed number of sides at 50, when we draw a small circle the system spends a lot of extra time drawing tons of small sides that don't really matter; when we draw really large circles the polygon-ness of our solution starts showing up.

What we need is a spell that varies depending on the size of the circle.  One thing we could do is put another variable in the interface.  What's that?  Oh, the interface is the part where the user of the spell interacts with it.  In this case, it's how the spell is called, with a turtle (t) and a radius (r).  Adding more to the interface would complicate it, though.

Another way to approach this would be to calculate a reasonable number of sides based on the circumference of the circle.  One estimate would be to have the number of sides equal to 1/3 of the circumference, so the length of the sides is (about) 3.  That will make the circles look good, but also allow for efficient spell-code.  Because it's calculated, it will be right for any circle we draw.

So, rewrite that circle function to calculate n based on the circumference, then draw the circle as before.

Speed Hint

All right.  Let's take a short break from coding to add an entry to your Codex.  Think a bit about how you are studying for this course.  How do you learn, here?  What sort of techniques are you using?

How might you use those in other classes?  Are there any that won't work in some of your other classes?  Why not?

Nice work.  Keep exploring!

Skip to toolbar