【发布时间】:2014-06-16 00:01:48
【问题描述】:
在 Python for Software Design 的第 4.12 节(第 4 章)的练习 4.1(c) 中,声称以下版本的函数 arc(),
def arc(t, r, angle):
"""Draws an arc with the given radius and angle.
t: Turtle
r: radius
angle: angle subtended by the arc, in degrees
"""
arc_length = 2 * math.pi * r * abs(angle) / 360
n = int(arc_length / 4) + 1
step_length = arc_length / n
step_angle = float(angle) / n
# making a slight left turn before starting reduces
# the error caused by the linear approximation of the arc
lt(t, step_angle/2)
polyline(t, n, step_length, step_angle)
rt(t, step_angle/2)
比第 4.7 节中的原始版本“更好”:
def arc(t, r, angle):
arc_length = 2 * math.pi * r * angle / 360
n = int(arc_length / 3) + 1
step_length = arc_length / n
step_angle = float(angle) / n
polyline(t, n, step_length, step_angle)
(可以查看子程序的代码,如polyline()、here)。
我试图了解为什么以前的版本更好,特别是哪个指标。我们如何定义我们正在逼近的真实圆?有什么想法吗?
【问题讨论】:
-
您是否正在寻找评论中的原因之外的原因?此外,据说前者更好,而不是后者。
-
没错,我看不出这两个版本在近似误差方面有何不同(在考虑了几个简单的例子之后)。感谢您发现语义错误,现已修复。
-
并不是说它回答了你关于为什么一个比另一个更好的问题,但我想指出这是already implemented anyway。
-
this implementation 的源代码是否在某处发布? - 它可能会为我最初的问题中提出的近似值的优点提供一些线索。
标签: python python-2.7 turtle-graphics approximation