Spectacular Spirals¶

This projection uses turtle graphics to draw spirals: choose your own design. On the way, we meet a spiral that is 200 meg years old.

First accept your polygon¶

Showtime the project by making an empty file spiral.py . Right-click and open information technology with IDLE. Blazon this (or re-create most of information technology from final.py in the introduction) and salve it:

                                    from                  turtle                  import                  *                  speed                  (                  "fastest"                  )                  def                  polygon                  (                  a                  ,                  northward                  ):                                      "n-sided polygon, side a."                                    for                  i                  in                  range                  (                  north                  ):                  frontward                  (                  a                  )                  left                  (                  360                  /                  n                  )                

The new line is chosen a documentation string, a lilliputian reminder of what the role does. It makes no difference to what the code does, but in IDLE, when y'all become equally far as typing polygon( , IDLE volition pop up this string as a reminder.

Of import

Never throw code away.

In these projects we explain the program a piece at a time. By and large you should add each new code fragment to the program yous take already, normally at the stop. Sometimes the fragments show a alter to what y'all take.

Occasionally, in that location is a brusque fragment of lawmaking that you only need for a few tests, then you'll replace it.

If e'er you want to delete a lot of code, first use "File" >> "Save Copy Equally" and requite a proper noun similar spiral_2.py , so you can become back to your program every bit it was.

First and fill the polygon¶

Your program is going to describe lots of different spirals. Here's a simple instance where you can run into what's going on:

../_images/example.png

Drawing starts with the smallest square tile, nearest the middle, and continues anti-clockwise, putting down tiles.

Each tile is slightly bigger than the concluding. Each circuit has the aforementioned number of tiles, that you choose, 9 in this example. By the time it gets circular once, the tile is twice the original size, and twice every bit far from the heart. It will go round as many times every bit you cull.

You will draw each tile with your polygon method. To put it in the correct place, suppose the turtle is in the centre, and pointing the correct style. We need to go forrard some distance, then draw the polygon. Then we go back to the centre, fix for adjacent fourth dimension.

In lawmaking, it looks similar this:

                                    def                  tile                  (                  r                  ,                  a                  ,                  north                  ):                  "n-sided polygon, side a, at range r (assumes pen is upwards)."                  forward                  (                  r                  )                  pendown                  ()                  begin_fill                  ()                  polygon                  (                  a                  ,                  n                  )                  end_fill                  ()                  penup                  ()                  back                  (                  r                  )                

Add that at the end of your plan, and then add together this to examination it:

                                    # Test                  fillcolor                  (                  "red"                  )                  tile                  (                  100                  ,                  50                  ,                  half-dozen                  )                

Lines that begin with a # are comments. Python ignores them: they're a hint to the humans. Yous should put comments in code y'all invent, simply you lot don't have to copy them in these examples.

Save and run that. It should look like this:

../_images/tile.png

The initial line is not a error: it is there considering nosotros started with the pen downwardly.

Turn and echo¶

Putting the tiles down in a spiral is withal too hard for us in one leap. Remember instead how would we put tiles downwardly in a circle.

That would be rather like cartoon a polygon, except we use the tile function each time instead of forward . Add together after your last function:

                                    def                  circ                  (                  r                  ,                  scale                  ,                  n                  ,                  tiles                  ):                  "Circle of due north-sided polygons at radius r and size r/scale."                  # Angle between each tile                  step                  =                  360                  /                  tiles                  # Now draw it                  penup                  ()                  for                  i                  in                  range                  (                  tiles                  ):                  tile                  (                  r                  ,                  r                  /                  scale                  ,                  northward                  )                  left                  (                  pace                  )                

Change your test code at the finish to read:

                                    # Test                  fillcolor                  (                  "red"                  )                                      circ                    (                    100                    ,                    ten                    ,                    iv                    ,                    9                    )                                  

Save and run: you should go a circumvolve of 9 picayune squares.

Abound a piffling each time¶

How tin can nosotros plow the circle into a spiral? This ways making the altitude from home (the radius) and the tile size grow each time a tile is placed.

Here "grow" means that nosotros should multiply the size and the radius by some amount each time we put down a tile. The growth factor should be just a little more than one, or the size will get huge in just a few tiles. Call back what happens to powers of numbers from affiliate 1?

We have to work out the starting radius and the growth per tile. Choosing that number, a trivial bigger than one, to get the final size you lot want, is the the hardest part. Y'all may not have learned the maths for this yet, merely the comments explain what's happening. Add together after your last office:

                                    def                  spiral                  (                  r                  ,                  northward                  ,                  calibration                  ,                  cycles                  ,                  thou                  ,                  growth                  ):                  """Screw of n-sided polygons out to radius r and size r/scale,                                      in given number of cycles of m steps, growing each bicycle.                                      """                  # Angle between each tile                  pace                  =                  360                  /                  m                  # Full number of tiles (made a whole number)                  p                  =                  int                  (                  cycles                  *                  m                  )                  +                  1                  # Growth between each tile                  chiliad                  =                  growth                  **                  (                  ane                  /                  m                  )                  # Starting radius (this volition grow with each tile placed)                  r                  =                  r                  /                  (                  growth                  **                  cycles                  )                  # Now draw it                  penup                  ()                  for                  i                  in                  range                  (                  p                  ):                  # As the distance from the centre grows, and so does the polygon                  tile                  (                  r                  ,                  r                  /                  calibration                  ,                  north                  )                  left                  (                  step                  )                  r                  =                  r                  *                  chiliad                

You tin see this is like the circ function, just with extra code to make the size change. Finally, we're ready to effort this out. At the end of your program, add:

                                    def                  case                  ():                  fillcolor                  (                  "red"                  )                  spiral                  (                  100                  ,                  4                  ,                  4                  ,                  2                  ,                  9                  ,                  ii                  )                

The telephone call to screw has a lot or arguments. In order it says that you lot want:

  • a concluding size of 100.
  • squares (iv sides)
  • … that are four times smaller than the altitude from the centre
  • to go round twice
  • to have nine tiles per revolution
  • to abound by a factor 2 each revolution

Relieve and run this. And then at the beat prompt type:

You should get the example we showed before.

Cull wild numbers¶

Here is a proffer to add:

                                    def                  ammonite                  ():                  pensize                  (                  i                  )                  color                  (                  "peru"                  ,                  "wheat"                  )                  spiral                  (                  100                  ,                  6                  ,                  iii                  /                  2                  ,                  4.seven                  ,                  22.five                  ,                  ii                  )                  hideturtle                  ()                

And here is some other a suggestion to add:

                                    def                  vortex                  ():                  pensize                  (                  5                  )                  color                  (                  "navy blue"                  ,                  "majestic blue"                  )                  spiral                  (                  500                  ,                  6                  ,                  3                  ,                  15                  ,                  4.2                  ,                  one.4                  )                  hideturtle                  ()                

Call them at the crush prompt, like you did with example() . The function call reset() will clear the drawing window between tests.

In the vortex you get a sort of two-fashion screw considering the number of tiles per cycle is not a whole number, but is close to a whole number (4.2). And so what might have been 4 straight lines becomes 4 curved lines. What number would brand them bend the other style?

Try calling spiral in your own function with a variety of numbers. If yous notice a combination you like, give information technology a new name.