鱼C论坛

 找回密码
 立即注册
查看: 3118|回复: 3

一个画圆圈的例子我看不懂,求大神帮忙解答一下

[复制链接]
发表于 2017-12-25 15:54:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请看下面的代码

  1. """This module contains a code example related to

  2. Think Python, 2nd Edition
  3. by Allen Downey
  4. http://thinkpython2.com

  5. Copyright 2015 Allen Downey

  6. License: http://creativecommons.org/licenses/by/4.0/
  7. """

  8. from __future__ import print_function, division

  9. import math
  10. import turtle


  11. def square(t, length):
  12.     """Draws a square with sides of the given length.
  13.         用给的长度画一个正方形。
  14.     Returns the Turtle to the starting position and location.
  15.         返回小乌龟开始的方向和位置。
  16.     """
  17.     for i in range(4):
  18.         t.fd(length)
  19.         t.lt(90)


  20. def polyline(t, n, length, angle):
  21.     """Draws n line segments. 画一个多边形

  22.     t: Turtle object    乌龟项目,t,这个形参好像没有什么特殊的意思,我还没悟透
  23.     n: number of line segments  n,代表多边形的边数
  24.     length: length of each segment  length,每条边的长度
  25.     angle: degrees between segments angle,两条边的角度
  26.     """
  27.     for i in range(n):
  28.         t.fd(length)    #fd,向前移动的距离
  29.         t.lt(angle)     #lt,left turn 左转,括号内填的是度数
  30.         


  31. def polygon(t, n, length):
  32.     """Draws a polygon with n sides.

  33.     t: Turtle
  34.     n: number of sides
  35.     length: length of each side.
  36.     """
  37.     angle = 360.0/n
  38.     polyline(t, n, length, angle)


  39. def arc(t, r, angle):
  40.     """Draws an arc with the given radius and angle. 用给定的半径 和 角度,画一个弧

  41.     t: Turtle
  42.     r: radius
  43.     angle: angle subtended by the arc, in degrees   #角度:圆弧所对的角度,以度为单位
  44.     """
  45.     arc_length = 2 * math.pi * r * abs(angle) / 360 #这个 abs() 应该是一个内建函数,将角度转化成数字(我猜的),周长 = 2πr
  46.     n = int(arc_length / 4) + 3 # 内建函数 int() 的作用是 将括号内的值,转换为 整型,至于这里为什么要将弧长除以4 再 加上3 我就不知道了
  47.     step_length = arc_length / n
  48.     step_angle = float(angle) / n

  49.     # making a slight left turn before starting reduces
  50.     # the error caused by the linear approximation of the arc
  51.     t.lt(step_angle/2)
  52.     polyline(t, n, step_length, step_angle)
  53.     t.rt(step_angle/2)


  54. def circle(t, r):
  55.     """Draws a circle with the given radius.

  56.     t: Turtle
  57.     r: radius
  58.     """
  59.     arc(t, r, 360)


  60. # the following condition checks whether we are
  61. # running as a script, in which case run the test code,
  62. # or being imported, in which case don't.

  63. if __name__ == '__main__':
  64.     bob = turtle.Turtle()

  65.     # draw a circle centered on the origin
  66.     radius = 100
  67.     bob.pu()
  68.     bob.fd(radius)
  69.     bob.lt(90)
  70.     bob.pd()
  71.     circle(bob, radius)

  72.     # wait for the user to close the window
  73.     turtle.mainloop()
复制代码

问题:
1. def arc 中 abs(angle) 这个函数是什么意思,我以前没有见过abs函数;
2. def arc 中  n = int(arc_length / 4) + 3  为什么要这么写,有什么作用
3.    t.lt(step_angle/2)    t.rt(step_angle/2) 为什么要有这两个,为什么要转向
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-12-25 16:05:17 | 显示全部楼层
1. def arc 中 abs(angle) 这个函数是什么意思,我以前没有见过abs函数;
abs是求绝对值,比如 abs(-3) == 3

2. def arc 中  n = int(arc_length / 4) + 3  为什么要这么写,有什么作用
这里应该是用来求解步长的中间过程

3.    t.lt(step_angle/2)    t.rt(step_angle/2) 为什么要有这两个,为什么要转向
就是下一个点的坐标,这样才能用polyline函数画出下一段线段
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-25 17:04:12 | 显示全部楼层
BngThea 发表于 2017-12-25 16:05
1. def arc 中 abs(angle) 这个函数是什么意思,我以前没有见过abs函数;
abs是求绝对值,比如 abs(-3) == ...

2.圆 是无限多个相同长度的线段 收尾相连 而的出来的,弧形 是圆的一部分,n 是这个弧形的 线段的数量,只是我不知道为什么 要用弧长除以4 再加上3

3.根据注释来猜测,我猜测  t.lt(step_angle/2) t.rt(step_angle/2) 只是用来调整 小箭头的起始方向,和结束方向,就删掉这两句,应该也没什么太大的关系
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-25 17:05:30 | 显示全部楼层
Dexter1203 发表于 2017-12-25 17:04
2.圆 是无限多个相同长度的线段 收尾相连 而的出来的,弧形 是圆的一部分,n 是这个弧形的 线段的数量, ...

你可以按照你自己的思路写一个,或者修改后试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-26 17:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表