python circle函数如何画圆_自学《Think Python》之第二个程序——用“海龟”画一个圆弧...

本文通过Swampy库详细介绍了如何使用TurtleWorld模块在Python中绘制正方形、正多边形和圆弧。关键在于理解polyline函数的作用及其在其他图形构造中的应用,展示了代码重用性和问题分解的重要性。
摘要由CSDN通过智能技术生成

源代码+代码解释+小结

要用这个程序,首先需要下载一个叫swampy的包(package),也就是一个文件夹或者说是一套模块,其中包含了很多的函数。关于package的安装只需提一点,由于模块不大,最好直接安装默认的路径(也就是直接打开shell或者dos提示符的工作路径)里,确保只有一个叫swampy文件夹,里面都是相应的py文件。

好,开始我们的问题,在书中的第四章,整章作为一个接口学习的案例。

问题:用下载的swampy中的TurtleWorld模块画出一个正方形,正多边形,圆形,一段弧。

分析:在这里的过程中,我们发现问题不断被推广,从一个简单的由4条边的正方形,被推广到n条边的正多边形,进而推广到n=∞的时候(实际上做不到无穷大,只是取一个比较大的n近似),最后再从圆形中取一小段,即一段圆弧。

这里涉及函数,前进fd(t,length)和左转lt(t,angle),其中t表示turtle,就是我们是拿“海龟”来画出一条路径。

这里直接把源代码附上。

1、源代码

"""This module contains code from

Think Python by Allen B. Downey

http://thinkpython.com

Copyright 2012 Allen B. Downey

License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html

"""

import math

try:

# see if Swampy is installed as a package

from swampy.TurtleWorld import *

except ImportError:

# otherwise see if the modules are on the PYTHONPATH

from TurtleWorld import *

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):

fd(t, length)

lt(t)

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):

fd(t, length)

lt(t, angle)

def polygon(t, n, length):

"""Draws a polygon with n sides.

t: Turtle

n: number of sides

length: length of each side.

"""

angle = 360.0/n

polyline(t, n, length, 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) + 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)

def circle(t, r):

"""Draws a circle with the given radius.

t: Turtle

r: radius

"""

arc(t, r, 360)

# the following condition checks whether we are

# running as a script, in which case run the test code,

# or being imported, in which case don't.

if __name__ == '__main__':

world = TurtleWorld()

bob = Turtle()

bob.delay = 0.001

# draw a circle centered on the origin

radius = 100

pu(bob)

fd(bob, radius)

lt(bob)

pd(bob)

circle(bob, radius)

wait_for_user()复制代码

2、源代码解释

首先,square这个函数是画正方形,比较简单,就是重复“前进+左拐”四次就得到了。

其次,polyline这个函数,这个函数是之后几个函数的基础,也就是画若干条直线,更准确的说是画出某个正m边形中的连续的n条直线(m>=n)。毫无疑问,这个是一切图形的基础,任何图形都可以拿线段去逼近(稍微卖弄一下数学)~

之后的polygon这个函数只是刚好是n=m的情形,就是画出所有的正m边形的边。(注:程序中没有出现过m,但是实际上任何一个angle都可以唯一对应一个m,满足angle=360°/m)

接着,也就是我们的目标要画出circle。但是,上述程序是先给了关于arc即圆弧的函数,因为弧长对应的圆周角取360°就是一个完整的圆了。(注:当然,我们也可以按照数学中从特殊到一般的角度,先确定定如何画圆,然后再按照一个百分比,即angle/360°来画一个弧。)画圆,就是用一个足够大的正n边形去逼近。

3、小结

最后,这里给个点评。程序是以polyline函数作为基本函数,来得到其他的函数,进而用其他的函数来表示目标函数。这种方法对于接口的设计起到了很好的练习和提升作用,即把大问题化成小问题,再把小问题分成几个基本的问题去解决,其中polyline这个函数反复使用的频率很高,这也就是这一章所强调的代码的重用性。

但是,作为代价,当你发现了一个很好的代码作为一个基函数,那么之前的代码可能就要重写。但就像书里提到的 “Once you start coding, you understand the problem better. Sometimes refactoring (重构)is a sign that you have learned something.”

(refactoring 就是重新整理一个程序以改进函数接口和促进代码的重用性。)这说的很清楚,代码可以不断被改“正”,当一个program被你用封装好的一系列函数所搞定之后,我们也就学到了东西。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值