下面给出的代码是作者为显示如何绘制一个广义弧线而提供的,但我不理解的是,在最后的弧形函数中,如何在开始之前稍微左转一下,就可以减少错误。
import math
import turtle
def square(t, length):
"""Draws a square with sides of the given length.
Returns the Turtle to the starting position and location.
"""
for i in range(4):
t.fd(length)
t.lt(90)
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 i in range(n):
t.fd(length)
t.lt(angle)
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) + 3
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)发布于 2021-04-30 12:29:22
在开始前稍微左转可以减小弧形t.lt(step_ the /2)折线(t,n,step_length,step_angle) t.rt(step_ the /2)
的线性近似所造成的误差。
我认为这就是你所指的那部分。这似乎是一个多余的步骤,因为它所做的就是在调用polyline函数之前向左转&然后在调用polyline函数之后向右转等量。
这个看起来很小的变化是,它把“折线”从一个切线变成了一个长度相同的割线。如果你在一张纸上画出这两种场景,你就会意识到割线是圆的近似值,因为它与圆有2个共同点,不像只有一个公共点的切线。
https://stackoverflow.com/questions/67333609
复制相似问题