第 4.3 节练习 #5 - Allen Downey 的 "Think Python"
Section 4.3 Exercise #5 - "Think Python" by Allen Downey
Link 练习可在此处访问 - Case Study: Interface Design, Exercise Section 4.3
引用问题,看来我必须实现一个arc()
功能:
Make a more general version of circle
called arc
that takes an additional parameter angle
, which determines what fraction of a circle to draw. angle
is in units of degrees, so when angle=360, arc
should draw a complete circle.
到目前为止我写的代码:
import turtle
import math
bob = turtle.Turtle()
def polygon(t, n, length):
for i in range(n):
t.fd(length)
t.lt(360/n)
def circle(t, r):
circumference = 2 * math.pi * r
n = int(circumference/3) + 1
length = circumference/n
polygon(t, n, length)
def arc(t, r, angle):
arc_length = 2 * math.pi * r * (angle/360)
n = (arc_length/4) + 1
arc(bob, 1000, 45)
turtle.mainloop()
我打算在 arc()
中调用 circle()
函数,就像在 circle()
中调用 polygon()
一样,但我不知道该怎么做那。除此之外,arc()
函数 不会 绘制任何东西,而只是显示静止不动的海龟。
我认为 Turtle 对象 bob
没有收到任何在 polygon()
中分配的移动指令。因此它所做的只是显示 Turtle 对象!
我可能是错的,这是我需要澄清的地方。我应该在 arc()
中调用 circle()
并让 Turtle 对象移动吗?有更简单的选择吗?在函数内调用函数对我来说仍然很困惑,所以更多的资源来了解它们也是很好的!
I'm trying to ... call the circle() function
within arc() just as polygon() was called within circle()
你搞反了。问题状态:
Make a more general version of circle called arc
正如您可以使用更通用的函数 polygon()
绘制圆一样,您应该能够使用更通用的函数 arc()
绘制圆。这是一个用于思考这个问题的框架程序:
from turtle import Screen, Turtle
from math import pi
def polygon(turtle, sides, length):
outside_angle = 360 / sides
for _ in range(sides):
turtle.forward(length)
turtle.left(outside_angle)
def circle_using_polygon(turtle, radius):
circumference = 2 * pi * radius
sides = min(60, int(circumference / 3))
length = circumference / sides
polygon(turtle, sides, length)
def arc(turtle, radius, angle):
# implement arc not by calling *circle() nor by
# calling polygon() but rather by borrowing code
# from both and adding one more step to reduce
# the number of sides based on the arc angle
def circle_using_arc(turtle, radius):
arc(turtle, radius, 360)
bob = Turtle(visible=False)
# Draw overlapping circles three different ways:
bob.color("green")
circle_using_polygon(bob, 100)
for color in ['cyan', 'magenta', 'yellow', 'black']:
bob.color(color)
arc(bob, 100, 90)
bob.color("blue")
circle_using_arc(bob, 100)
screen = Screen()
screen.mainloop()
import turtle
bob=turtle.Turtle()
import math
def arc(t,radius,angle):
circumference = 2.0*math.pi*radius
frac = angle/360.0
arclength = circumference*frac
n = 50 # pick a number
len = arclength/n;
turnang = angle/n
for i in range(n):
t.fd(len)
t.lt(turnang)
arc(bob, 130,360)
turtle.done()
import tkinter
import swampy
from swampy.TurtleWorld import *
def polygon(n, t, length, angle):
print(t)
k= angle/360
for i in range(0,int(n*k)):
fd(t, length)
p= 360
lt(t,p/n)
t.delay
world = TurtleWorld()
bob = Turtle()
#def circle(r):
#l1= 2*3.14*r
#l= l1/60
#polygon(30, bob, l)
polygon(60, bob, 10, 180)
Link 练习可在此处访问 - Case Study: Interface Design, Exercise Section 4.3
引用问题,看来我必须实现一个arc()
功能:
Make a more general version of
circle
calledarc
that takes an additional parameterangle
, which determines what fraction of a circle to draw.angle
is in units of degrees, so when angle=360,arc
should draw a complete circle.
到目前为止我写的代码:
import turtle
import math
bob = turtle.Turtle()
def polygon(t, n, length):
for i in range(n):
t.fd(length)
t.lt(360/n)
def circle(t, r):
circumference = 2 * math.pi * r
n = int(circumference/3) + 1
length = circumference/n
polygon(t, n, length)
def arc(t, r, angle):
arc_length = 2 * math.pi * r * (angle/360)
n = (arc_length/4) + 1
arc(bob, 1000, 45)
turtle.mainloop()
我打算在 arc()
中调用 circle()
函数,就像在 circle()
中调用 polygon()
一样,但我不知道该怎么做那。除此之外,arc()
函数 不会 绘制任何东西,而只是显示静止不动的海龟。
我认为 Turtle 对象 bob
没有收到任何在 polygon()
中分配的移动指令。因此它所做的只是显示 Turtle 对象!
我可能是错的,这是我需要澄清的地方。我应该在 arc()
中调用 circle()
并让 Turtle 对象移动吗?有更简单的选择吗?在函数内调用函数对我来说仍然很困惑,所以更多的资源来了解它们也是很好的!
I'm trying to ... call the circle() function within arc() just as polygon() was called within circle()
你搞反了。问题状态:
Make a more general version of circle called arc
正如您可以使用更通用的函数 polygon()
绘制圆一样,您应该能够使用更通用的函数 arc()
绘制圆。这是一个用于思考这个问题的框架程序:
from turtle import Screen, Turtle
from math import pi
def polygon(turtle, sides, length):
outside_angle = 360 / sides
for _ in range(sides):
turtle.forward(length)
turtle.left(outside_angle)
def circle_using_polygon(turtle, radius):
circumference = 2 * pi * radius
sides = min(60, int(circumference / 3))
length = circumference / sides
polygon(turtle, sides, length)
def arc(turtle, radius, angle):
# implement arc not by calling *circle() nor by
# calling polygon() but rather by borrowing code
# from both and adding one more step to reduce
# the number of sides based on the arc angle
def circle_using_arc(turtle, radius):
arc(turtle, radius, 360)
bob = Turtle(visible=False)
# Draw overlapping circles three different ways:
bob.color("green")
circle_using_polygon(bob, 100)
for color in ['cyan', 'magenta', 'yellow', 'black']:
bob.color(color)
arc(bob, 100, 90)
bob.color("blue")
circle_using_arc(bob, 100)
screen = Screen()
screen.mainloop()
import turtle
bob=turtle.Turtle()
import math
def arc(t,radius,angle):
circumference = 2.0*math.pi*radius
frac = angle/360.0
arclength = circumference*frac
n = 50 # pick a number
len = arclength/n;
turnang = angle/n
for i in range(n):
t.fd(len)
t.lt(turnang)
arc(bob, 130,360)
turtle.done()
import tkinter
import swampy
from swampy.TurtleWorld import *
def polygon(n, t, length, angle):
print(t)
k= angle/360
for i in range(0,int(n*k)):
fd(t, length)
p= 360
lt(t,p/n)
t.delay
world = TurtleWorld()
bob = Turtle()
#def circle(r):
#l1= 2*3.14*r
#l= l1/60
#polygon(30, bob, l)
polygon(60, bob, 10, 180)