双曲螺旋(第一次用 ThinkPython 学习)

Hyperbolic Spirals (first time learning with ThinkPython)

我正在通过 ThinkPython 这本书学习 Python(为了好玩)。到目前为止,我真的很享受这个新爱好。最近的一项练习是制作阿基米德命令 spiral.In 来测试我的技能我一直在努力制作双曲螺旋,但被难住了!

根据等式r=a+b*theta^(1/c)

我正在使用以下代码,如果能为正确的方向提供帮助,我将不胜感激。

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"""

非常感谢!

简单地调整作为参数传递给draw_spiral()的常量:

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

我能够沿着您描述的线条生成螺旋线:

但是,它是从外部绘制的,而不是内部。所以,可能不是您要找的。

正如@cdlane 的回答中提到的,这只是参数的问题。我会扩展答案。

对于 Archimedean spiral 添加参数 c 到函数 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 

并使用以下参数,例如:

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