[英]Turtle graphics in Python 2.7: Drawing an arc
在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()
)。
我试图了解为什么以前的版本更好,尤其是按哪个指标。 我们如何定义逼近的真实圆? 有任何想法吗?
在尝试绘制草图之前,我也无法向自己解释。 如果可以想象,半步角转弯有助于大致将弧长平分。 通过介于两者之间,可以对创建的其他区域进行加法和减法,这是一种粗略的校正。
让我们进行比较。 我们将使用turtle.circle()
作为任意标准,然后使用两个arc()
例程绘制360度弧(aka圆),半径比我们的标准小3个像素,半径比我们大3个像素:
import math
from turtle import Turtle, Screen
def polyline(t, n, length, angle):
"""Draws n line segments.
t: Turtle object
n: number of line segments
length: length of each segment
angle: degrees between segments
"""
for _ in range(n):
t.fd(length)
t.lt(angle)
def arc2(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
t.lt(step_angle/2)
polyline(t, n, step_length, step_angle)
t.rt(step_angle/2)
def arc1(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)
screen = Screen()
screen.setup(500, 500)
screen. setworldcoordinates(-250, -50, 250, 450)
thing0 = Turtle()
thing0.circle(200, steps=60)
thing1 = Turtle()
thing1.color("red")
thing1.penup()
thing1.goto(0, 3)
thing1.pendown()
arc1(thing1, 197, 360)
thing2 = Turtle()
thing2.color("green")
thing2.penup()
thing2.goto(0, -3)
thing2.pendown()
arc2(thing2, 203, 360)
screen.exitonclick()
完整的圈子
细节#1
细节#2
我要说的是,第4.12弧(绿色)看起来比第4.7弧(红色)更好,因为绿色弧线的锯齿少,并且与我们的标准圆保持一致的3个像素,而红色弧线则越来越近。 您认为什么标准重要?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.