Hyperbolic Spirals (first time learning with ThinkPython)

396 Views Asked by At

I'm learning Python (for fun) through the book ThinkPython. So far I'm really enjoying this new hobby. One of the recent exercise was crafting a Archimedes spiral.In order to test my skills I've been working on making a hyperbolic spiral but have been stumped!

according to the equation r=a+b*theta^(1/c)

  • when c=1 we see a Archimedes spiral
  • when c=-1 we should see a hyperbolic spiral

I'm using the following code and would appreciate any help towards the right direction.

  • Extra points to whoever can help me in the right direction without directly giving away the answer. Unless it's simply a formatting issue.
  • No points if the answer suggested involves using (x,y) coordinates to draw.
import turtle
import math

def draw_spiral(t, n, length=3, a=0.1, b=0.0002):
    """Draws an Archimedian spiral starting at the origin.

    Args:
      n: how many line segments to draw
      length: how long each segment is
      a: how loose the initial spiral starts out (larger is looser)
      b: how loosly coiled the spiral is (larger is looser)

    http://en.wikipedia.org/wiki/Spiral
    """
    theta = 0.1

    for i in range(n):
        t.fd(length)
        dtheta = 1/((a + b * (theta**(1/-1))))

        t.lt(dtheta)
        theta += dtheta

# create the world and bob
bob = turtle.Turtle()
draw_spiral(bob, n=1000)

turtle.mainloop()

"""Source code from ThinkPython @ http://greenteapress.com/thinkpython2/code/spiral.py

edited to attempt a hyperbolic spiral"""

many thanks!

2

There are 2 best solutions below

1
cdlane On BEST ANSWER

Simply adjusting the constants passed to draw_spiral() as arguments:

def draw_spiral(t, n, length=1, a=0.01, b=60):

I was able to generate a spiral along the lines you describe:

enter image description here

However, it draws from the outside in, not the inside out. So, may not be what you're looking for.

1
Rabbid76 On

As mentioned in the answer of @cdlane, it is just a matter of the arguments. I'll extend the answer.

For the Archimedean spiral add the argument c to the function draw_spiral:

def draw_spiral(t, n, length=3, a=0.1, b=0.0002, c=-1):

    theta = 0.1
    for i in range(n):
        t.fd(length)
        dtheta = 1/((a + b * (theta**(1/c))))

        t.lt(dtheta)
        theta += dtheta 

And use the following arguments, for instance:

draw_spiral(bob, n=1000, length=1, a=0.01, b=0.001, c=1)