我一个任务正在真的窃听: 用户输入半径r,然后龟平圆然后继续绘制另一个圆圈具有相同中心,但小10像素,直至半径为0Python的龟同心圆
0
A
回答
0
首先让我们将一个圆近似为36边/正方形的正多边形。 要绘制这种给定半径的形状r我们需要知道;
- 每段
- 每段
之间 把要计算的长度的角度的长度,我们首先需要的圆周上,这是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
目前尚不清楚为什么@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()
但是,由此得出原油界称为它炸毁一个小:
为了解决这个问题,我可能会在两种方案之间的妥协上面做:
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()
这通过缩小一个大的,而不是扩大小一提高圈质量:
在哪里可以使用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()
方法从中心而不是边缘绘制,使用直径而非半径,只绘制实心圆,而且似乎这个特殊的锻炼我们的最佳解决方案:
-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()
相关问题
- 1. Gnuplot同心圆图
- 2. 将一只乌龟移动到一个圆圈的中心
- 3. Python的龟 - 龟位置
- 4. Android:绘制同心圆
- 5. 阵营本地同心圆
- 6. 的Python:移动海龟同时
- 7. 同心圆在java中的颜色
- 8. C3.js中的同心圆弧
- 9. Hold键 - Python的龟
- 10. 如何在Python中使用龟绘制圆3
- 11. 如何仅在Python龟中绘制半圆
- 12. Python龟图
- 13. 将原点从中心移到Python龟的左上角?
- 14. 移动同心圆并向右移动
- 15. 如何在iphone中绘制同心圆?
- 16. 在Python中结束Onscreenclick()命令龟龟
- 17. 用海龟绘制超椭圆形
- 18. 分割后的Python龟图
- 19. Python的海龟不工作
- 20. Python的装载机龟
- 21. 用户输入的Python龟
- 22. 如何找到所有以其圆心为中心的圆圈
- 23. 如何在python中使用乌龟图形找到圆的直径
- 24. Matplotlib:散点图的圆心
- 25. 中心圆(圆)按钮,在网格
- 26. hough圆圈opencv圆心坐标
- 27. 如何让Python龟停止试图赶上它跟踪的龟?
- 28. Python龟:递归函数
- 29. 解决龟概念在Python
- 30. Python龟图形简化
你为什么重塑龟” s'circle()'方法?我在OP的问题中没有看到任何需要额外努力的问题。 – cdlane
@cdlane我的坏,没有意识到这个功能。我用过的练习让我创造了自己的功能。 –