【问题标题】:Write a function called circle that takes a turtle, t, and radius, r, as param编写一个名为 circle 的函数,以乌龟 t 和半径 r 作为参数
【发布时间】:2017-07-07 10:50:57
【问题描述】:
import turtle
bob = turtle.Turtle()
def polygon(t,length,n):
   for i in range(n):
      t.fd(length)
      t.lt(360/n)
   print(t)

polygon(bob,30,15)

turtle.mainloop()

如何通过调用多边形函数来做一个圆?

【问题讨论】:

  • 我投票决定将此问题作为题外话结束,因为它看起来像是家庭作业。
  • @HannounYassir,鉴于It is okay to ask about homework 社区政策,这是关闭问题的正当理由吗?该帖子显示了迄今为止为解决该问题所做的工作,并且只是略微缺乏对尝试解决该问题的困难的描述。您使用的问题指南是什么?
  • @cdlane 给我一个家庭作业的答案可能对学生弊大于利。话虽如此,我认为如果做出足够合理的尝试来解决问题,我认为提供帮助是可以的。

标签: python turtle-graphics


【解决方案1】:

您已经编写了正确的代码来生成一个圆圈。在turtle自己的circle()方法看来,一个圆只是一个有60条边的多边形(如果圆小则更少。)即这是关于感知以及在您无法区分之前需要多少方面。

import turtle

def polygon(t, length, n):
    for _ in range(n):
        t.fd(length)
        t.lt(360 / n)

bob = turtle.Turtle()

bob.penup()
bob.sety(-270)
bob.pendown()

polygon(bob, 30, 60)

turtle.mainloop()

您现在的问题是控制多边形/圆的绘制以生成具有特定半径的多边形。您的 length 参数没有映射到半径,因为圆出来的太大了。这里length 代表圆周的 1/60 (1/n),我们知道:

circumference = 2 * math.pi * radius

我们可以在我们的circle(t, radius)函数中计算length需要给radius(即circumference/n),并使用这些参数调用polygon(t, length, n)。这是一个视觉比较,用海龟的circle() 方法(红色)绘制一个半径为 100 的圆,并用我刚刚描述的解决方案(蓝色)绘制它:

【讨论】:

    【解决方案2】:
    import turtle
    bob=turtle.Turtle()
    bob.color('green', 'cyan')
    bob.begin_fill()
    def polygon(t,length, n):
        for i in range(n):
            bob.forward(length)
            bob.left(360/n)
    
    import math
    def circle(t, r):
            circum= 2*math.pi*r
            n= int(circum/10)+1
            length= circum/n
            polygon(t,length, n)
    
    circle(bob, 100)
    bob.end_fill()
    turtle.done()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      相关资源
      最近更新 更多