Drawing arhimedean spiral

779 Views Asked by At

Here is a piece of python 3 code that draws archimedean spiral using Turtle. Equation of the spiral in polar coordinates

$$r = a + b\theta$$

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.0

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

        t.lt(dtheta)
        theta += dtheta

Why do we define dtheta and theta like this? I mean, I don't see why such definition should draw the spiral. So, I want to know how author got these dtheta and theta.

2

There are 2 best solutions below

7
On BEST ANSWER

If theta were incremented by a constant value, then the lines drawn would arbitrarily long as theta increases, which would not be aesthetically pleasing. Evidently, the writer wanted to keep the length of the line segments more or less constant. For large $t$, the arc length we get from this incrementation is $$\int_{t}^{t+(a+bt)^{-1}}\sqrt{(a+b\theta)^2+b^2}d\theta \approx \frac{\sqrt{(a+bt)^2+b^2}}{a+bt}\approx1$$ The value for dtheta doesn't really affect whether the program will draw a spiral (unless dtheta is too big, then the resolution will be too poor), it just determines what/how many values of $\theta$ will be plotted.

1
On

I am a beginner. Chances are I could likely be wrong.

In my opinion, the solution is elegant, unfortunately it does not work when a = 0 at the starting point when theta value is also 0.

The bot asks me to provide additional supporting information, here it is.

In this formula, r = a + bθ, zero is a perfectly valid value for a.

Look at the codes below. At the beginning, in case a = 0, we have theta = 0, hence a + bθ = 0 + b*0 = 0.

""" theta = 0.0

for i in range(n):
    t.fd(length) # this line of code will run fine
    dtheta = 1 / (a + b * theta) # this line of code will result in divide by zero.

    t.lt(dtheta)
    theta += dtheta

'''

Sorry for the poor English.