如何在 python 中使用 turtle 画圆?
How to draw a circle using turtle in python?
我想问一下如何在 python 中使用 turtle 模块仅使用 turtle.forward 和 turtle.left 绘制一个圆圈?我使用下面的代码:
for i in range(30):
turtle.forward(i)
turtle.left(i)
turtle.done()
我得到的是,一旦我得到完整的圆圈,这条线就不会停止。我如何编写代码以便我有一个特定半径的圆并且在绘制圆后我有一个句号(不使用 turtle.circle)。
此处示例,
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()
如果你想画一个圆,最好的办法就是把问题简单化,如果我们考虑为圆的每一度移动 1 space 那么我们可以简单地写成
def draw_circle1():
for _ in range(360):
turtle.forward(1)
turtle.left(1)
现在我们对我们画的这个基本圆了解多少?好吧,我们知道它需要 360 步,每步是 1。所以圆的周长是 360。我们可以使用一些数学来计算半径。
circumference = 2 * 3.14... * radius
360 = 2 * 3.14... * radius
360 / 2 / 3.14... = radius
radius = 57.29...
所以现在我们可以反过来,如果我们想指定一个给定半径的圆,我们可以计算这个圆应该有多少周长。将其除以 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)
如果我们 运行 这对于 3 个单独的圆圈,每个圆圈的大小都在增加,你会看到它给了我们一个一致的结果
draw_circle(20)
draw_circle(40)
draw_circle(60)
turtle.hideturtle()
turtle.done()
现在我们有了一个函数,它可以接受一个半径并根据该半径画一个圆
我做了这个图片作为参考,
本质上你需要绘制n边的内接多边形。
最初的左转将是ϴ/2。
然后向前 by a = 2rsin(ϴ/2).
每个 forward 之后都是 left 完整 ϴ 转,除了在最后一个 forward 我们只需要 left 转 ϴ/2 以便航向正确更新为与圆(或圆弧)相切。
像这样,
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()
使用 turtle 创建 SPIROGRAPH。最终输出:
import random
import turtle
from turtle import Turtle, Screen
tim = Turtle()
tim.shape('arrow')
turtle.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')
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)
screen = Screen()
screen.exitonclick()
一个螺旋码
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()
我想问一下如何在 python 中使用 turtle 模块仅使用 turtle.forward 和 turtle.left 绘制一个圆圈?我使用下面的代码:
for i in range(30):
turtle.forward(i)
turtle.left(i)
turtle.done()
我得到的是,一旦我得到完整的圆圈,这条线就不会停止。我如何编写代码以便我有一个特定半径的圆并且在绘制圆后我有一个句号(不使用 turtle.circle)。
此处示例,
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()
如果你想画一个圆,最好的办法就是把问题简单化,如果我们考虑为圆的每一度移动 1 space 那么我们可以简单地写成
def draw_circle1():
for _ in range(360):
turtle.forward(1)
turtle.left(1)
现在我们对我们画的这个基本圆了解多少?好吧,我们知道它需要 360 步,每步是 1。所以圆的周长是 360。我们可以使用一些数学来计算半径。
circumference = 2 * 3.14... * radius
360 = 2 * 3.14... * radius
360 / 2 / 3.14... = radius
radius = 57.29...
所以现在我们可以反过来,如果我们想指定一个给定半径的圆,我们可以计算这个圆应该有多少周长。将其除以 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)
如果我们 运行 这对于 3 个单独的圆圈,每个圆圈的大小都在增加,你会看到它给了我们一个一致的结果
draw_circle(20)
draw_circle(40)
draw_circle(60)
turtle.hideturtle()
turtle.done()
现在我们有了一个函数,它可以接受一个半径并根据该半径画一个圆
我做了这个图片作为参考,
本质上你需要绘制n边的内接多边形。
最初的左转将是ϴ/2。
然后向前 by a = 2rsin(ϴ/2).
每个 forward 之后都是 left 完整 ϴ 转,除了在最后一个 forward 我们只需要 left 转 ϴ/2 以便航向正确更新为与圆(或圆弧)相切。
像这样,
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()
使用 turtle 创建 SPIROGRAPH。最终输出:
import random
import turtle
from turtle import Turtle, Screen
tim = Turtle()
tim.shape('arrow')
turtle.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')
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)
screen = Screen()
screen.exitonclick()
一个螺旋码
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()