我正在通过ThinkPython这本书学习Python(很有趣)。到目前为止,我真的很喜欢这个新爱好。最近的一项练习是制作阿基米德螺旋线。为了测试我的技能,我一直在努力制作双曲线螺旋线,但被挫败了!
根据等式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"""
非常感谢!