2017-05-28 75 views
0

我一个任务正在真的窃听: 用户输入半径r,然后龟平圆然后继续绘制另一个圆圈具有相同中心,但小10像素,直至半径为0Python的龟同心圆

回答

0

首先让我们将一个圆近似为36边/正方形的正多边形。 要绘制这种给定半径的形状r我们需要知道;

  1. 每段
  2. 每段

之间 把要计算的长度的角度的长度,我们首先需要的圆周上,这是2πR(我们将接近pi作为3.1415),让我们

circumference = 2 * 3.1415 * radius 

接下来,我们通过我们逼近的段数除以本,给

circumference = 2 * 3.1415 * radius 
seg_lenght = circumferece/36 

现在我们需要分段之间的角度差或外角。这仅仅是为正n边形(多边形具有n侧)360/N,因此,我们做三十六分之三百六十= 10

现在,我们可以定义一个函数,以产生所述片段的长度并绘制圆:

def circle_around_point(radius): 
    circumference = 2 * 3.1415 * radius 
    seg_length = circumference/36 

    penup()   
    fd(radius) #Move from the centre to the circumference 
    right(90) #Face ready to start drawing the circle 
    pendown() 

    for i in range(36): #Draw each segment 
     fd(seg_length) 
     right(10) 

    penup() 
    right(90) #Face towards the centre of the circle 
    fd(radius) #Go back to the centre of the circle 
    right(180) #Restore original rotation 
    pendown() 

现在的同心圆:

def concentric_circles(radius): 
    while radius > 0: 
     circle_around_point(radius) 
     radius -= 10 
+0

你为什么重塑龟” s'circle()'方法?我在OP的问题中没有看到任何需要额外努力的问题。 – cdlane

+0

@cdlane我的坏,没有意识到这个功能。我用过的练习让我创造了自己的功能。 –

0

目前尚不清楚为什么@IbraheemRodrigues觉得需要根据您的问题说明重新编写乌龟的circle()功能,但我们可以通过不重新发明轮子,简化了解决方案:

def circle_around_point(turtle, radius): 
    is_down = turtle.isdown() 

    if is_down: 
     turtle.penup() 
    turtle.forward(radius) # move from the center to the circumference 
    turtle.left(90) # face ready to start drawing the circle 
    turtle.pendown() 

    turtle.circle(radius) 

    turtle.penup() 
    turtle.right(90) # face awary from the center of the circle 
    turtle.backward(radius) # go back to the center of the circle 

    if is_down: 
     turtle.pendown() # restore original pen state 

def concentric_circles(turtle, radius): 
    for r in range(radius, 0, -10): 
     circle_around_point(turtle, r) 

circle()的关键在于当前位置位于圆的边缘,因此您需要将位置移动半径以使特定点成为圆的中心。

但是,要解决这个问题,我可能会从绘制切换到冲压做这种方式来加快进行,并简化代码:

import turtle 

STAMP_SIZE = 20 

radius = int(input("Please input a radius: ")) 

turtle.shape('circle') 
turtle.fillcolor('white') 

for r in range(radius, 0, -10): 
    turtle.shapesize(r * 2/STAMP_SIZE) 
    turtle.stamp() 

turtle.mainloop() 

但是,由此得出原油界称为它炸毁一个小:

enter image description here

为了解决这个问题,我可能会在两种方案之间的妥协上面做:

import turtle 

radius = int(input("Please input a radius: ")) 

turtle.penup() 
turtle.forward(radius) 
turtle.left(90) 
turtle.pendown() 

turtle.begin_poly() 
turtle.circle(radius) 
turtle.penup() 
turtle.end_poly() 

turtle.addshape('round', turtle.get_poly()) # 'circle' is already taken 

turtle.right(90) 
turtle.backward(radius) 

turtle.shape('round') 
turtle.fillcolor('white') 

for r in range(radius - 10, 0, -10): 
    turtle.shapesize(r/radius) 
    turtle.stamp() 

turtle.mainloop() 

这通过缩小一个大的,而不是扩大小一提高圈质量:

enter image description here

在哪里可以使用steps=参数来调用控制circle()圆的质量。

但是,如果我真的想同时保持高品质和速度快,尽量减少代码,我可能会做:

import turtle 

radius = int(input("Please input a radius: ")) 

for diameter in range(radius * 2, 0, -20): 
    turtle.dot(diameter, 'black') 
    turtle.dot(diameter - 2, 'white') 

turtle.hideturtle() 

turtle.mainloop() 

dot()方法从中心而不是边缘绘制,使用直径而非半径,只绘制实心圆,而且似乎这个特殊的锻炼我们的最佳解决方案:

enter image description here

-1
import turtle 

####  ##### #### Below class draws concentric circles. 

class Circle: 

    def __init__(self, pen, cx, cy, radius): 
     self.pen = pen 
     self.cx = cx 
     self.cy = cy 
     self.radius = radius 

    def drawCircle(self): 
     self.pen.up() 
     self.pen.setposition(self.cx, self.cy - self.radius) 
     self.pen.down() 
     self.pen.circle(self.radius) 

    def drawConCircle(self, minRadius = 10, delta = 10): 
     if(self.radius > minRadius) : 
      self.drawCircle() 
      self.radius -= delta # reduce radius of next circle 
      self.drawConCircle() 
#### End class circle ####### 

win = turtle.Screen() 
win.bgcolor("white") 
s = Circle(turtle.Turtle(), 0, 0, 200) 
s.drawConCircle() 
win.exitonclick()