[英]How to draw a circle using turtle in python?
I wanted ask how can I draw a circle using turtle module in python just using turtle.forward and turtle.left?我想问一下如何在python中使用turtle模块绘制一个圆圈,只使用turtle.forward和turtle.left? I use the code below:
我使用下面的代码:
for i in range(30):
turtle.forward(i)
turtle.left(i)
turtle.done()
What I get is that the line does not stop once I get the full cirle.我得到的是,一旦我得到完整的圆圈,这条线就不会停止。 How can I code so that I have a circle of specific radius and that I have a full stop once the circle is drawn (without using turtle.circle).
我如何编码,以便我有一个特定半径的圆,并且一旦绘制了圆我就有一个句号(不使用turtle.circle)。
If you want to draw a circle the best thing to do is to simplyfy the problem, if we consider moving 1 space for each degree of the circle then we can simply write this as如果你想画一个圆,最好的办法就是把问题简单化,如果我们考虑为圆的每一度移动 1 个空间,那么我们可以简单地把它写成
def draw_circle1():
for _ in range(360):
turtle.forward(1)
turtle.left(1)
Now what do we know about this basic circle that we drew?现在我们对我们绘制的这个基本圆了解多少? well we know it took 360 steps and each step was 1. so the circle has a circumference of 360. we can use a bit of math to calculate the radius.
我们知道它需要 360 步,每一步都是 1。所以圆的周长是 360。我们可以使用一些数学来计算半径。
circumference = 2 * 3.14... * radius
360 = 2 * 3.14... * radius
360 / 2 / 3.14... = radius
radius = 57.29...
So now we can reverse this, if we want to specify a circle of a given radius, we can calculate what circumference that circle should have.所以现在我们可以颠倒这个,如果我们想指定一个给定半径的圆,我们可以计算这个圆应该有多少周长。 divide that by the 360 degrees and we know what size step to take before each turn of 1 degree.
将其除以 360 度,我们就知道在每转 1 度之前要采取多大的步长。
def draw_circle(radis):
circumfrence = 2 * math.pi * radis
step_size = circumfrence / 360
for _ in range(360):
turtle.forward(step_size)
turtle.left(1)
if we run this for 3 separate circles each increasing in size you see it gives us a consistent result如果我们为 3 个单独的圆圈运行这个,每个圆圈的大小都在增加,你会看到它给了我们一致的结果
draw_circle(20)
draw_circle(40)
draw_circle(60)
turtle.hideturtle()
turtle.done()
So now we have a function which can accept a radius and draw a circle based on that radius所以现在我们有一个可以接受半径并根据该半径绘制圆的函数
I made this image as a reference,我制作了这张图片作为参考,
Essentially you need to draw the inscribed polygon with n sides.本质上,您需要绘制具有 n 边的内接多边形。
The initial left turn will be ϴ/2.初始左转弯将Θ/ 2。
Then forward by a = 2rsin(ϴ/2).然后向前推进a = 2rsin(ϴ/2)。
Each forward is followed by a left turn of the full ϴ, except that after the last forward we want only a left turn of ϴ/2 so that the heading will be correctly updated to be tangential to the circle (or arc).每向前后跟完整Θ的左转,只是在最后的未来,我们只希望Θ/ 2的左转,这样的标题会被正确地更新为相切圆(或圆弧)。
Something like this,像这样的东西,
import turtle
import math
def circle2(radius,extent=360,steps=360):
if extent<360 and steps==360:
steps=extent
theta=extent/steps
step_size=2*radius*math.sin(math.radians(theta/2))
turtle.left(theta/2)
turtle.forward(step_size)
for i in range(1,steps):
turtle.left(theta)
turtle.forward(step_size)
turtle.left(theta/2)
turtle.hideturtle()
turtle.speed(0)
turtle.getscreen().tracer(False)
circle2(50)
circle2(100,180)
turtle.up()
turtle.home()
turtle.down()
circle2(130)
circle2(130,360,10)
turtle.update()
turtle.mainloop()
Create a SPIROGRAPH using turtle使用乌龟创建螺旋图
[Final output][1] [1]: https://i.stack.imgur.com/unlAl.png [最终输出][1] [1]:https://i.stack.imgur.com/unlAl.png
import random随机导入
import turtle进口龟
from turtle import Turtle, Screen从海龟进口海龟,屏幕
tim = Turtle()蒂姆 = 乌龟()
tim.shape('arrow') tim.shape('箭头')
turtle.colormode(255)龟.colormode(255)
def random_colour( ):定义随机颜色():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return (r, g, b)
tim.speed('fastest') tim.speed('最快')
def draw_spirograph(size_of_gap): def draw_spirograph(size_of_gap):
for _ in range(int(360/size_of_gap)):
tim.color(random_colour())
tim.circle(100)
tim.setheading(tim.heading()+size_of_gap)
draw_spirograph(5) draw_spirograph(5)
screen = Screen() screen.exitonclick() screen = Screen() screen.exitonclick()
A spirograph code一个螺旋图代码
from turtle import Turtle
import random
josh = Turtle()
josh.color('DarkRed')
def random_color()->tuple:
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
return (r,g,b)
josh.speed('fastest')
josh.pensize(2)
for i in range(72):
josh.circle(100)
josh.right(5)
colormode(255)
josh.pencolor(random_color())
screen = Screen()
screen.setup(800,800)
screen.exitonclick()
Example here,这里的例子,
import turtle
def circle(distance, sections):
angle = 360/sections
for i in range(1, sections+1):
turtle.forward(distance)
turtle.left(angle)
circle(20, 30)
turtle.done()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.