使用Python turtle通过circle函数生成圆
我有一个学校作业: 不带海龟圆圈功能的雪人 雪人应该在蓝色背景上,并且应该用白色填充 雪人的轮廓应该是黑色的 雪人的身体应该由3个填充的圆圈组成 每个圆的轮廓应为3像素宽 底圆的半径应为100像素 中间圆的半径应为70像素 顶圆的半径应为40像素 每个圆应位于其下方圆的上方(底部圆除外,底部圆可位于任何位置) 圆圈之间应该没有空隙 给雪人一张嘴、一只眼睛和一个鼻子(帽子是可选的) 确保每只手上有两个杆臂和至少两个手指 到目前为止,我创建了这个,但在我继续之前,我似乎无法正确地获得圆圈。 另外,不知道如何在圆圈中上色或在眼睛上做圆点。请帮帮我,第一次编码使用Python turtle通过circle函数生成圆,python,geometry,turtle-graphics,Python,Geometry,Turtle Graphics,我有一个学校作业: 不带海龟圆圈功能的雪人 雪人应该在蓝色背景上,并且应该用白色填充 雪人的轮廓应该是黑色的 雪人的身体应该由3个填充的圆圈组成 每个圆的轮廓应为3像素宽 底圆的半径应为100像素 中间圆的半径应为70像素 顶圆的半径应为40像素 每个圆应位于其下方圆的上方(底部圆除外,底部圆可位于任何位置) 圆圈之间应该没有空隙 给雪人一张嘴、一只眼睛和一个鼻子(帽子是可选的) 确保每只手上有两个杆臂和至少两个手指 到目前为止,我创建了这个,但在我继续之前,我似乎无法正确地获得圆圈。 另外,不
import turtle # allows us to use turtle library
wn = turtle.Screen() # allows us to create a graphics window
wn.bgcolor("blue") # sets gtaphics windows background color to blue
import math # allows us to use math functions
quinn = turtle.Turtle() # sets up turtle quinn
quinn.setpos(0,0)
quinn.pensize(3)
quinn.up()
# drawing first circle middle
quinn.forward(70)
quinn.down()
quinn.left(90)
# calculation of cicumference of a circle
a = (math.pi*140.00/360)
#itineration for first circle
for i in range (1,361,1):
quinn.left(a)
quinn.forward (1)
# drawing second circle bottom
quinn.up()
quinn.home()
quinn.right(90)
quinn.forward(70)
quinn.left(90)
quinn.down()
b = (math.pi*200.00/360)
for i in range (1,361,1):
quinn.right(b)
quinn.forward(1)
# drawing third circle head top
quinn.up ()
quinn.goto(0,70)
quinn.right(90)
quinn.down()
c =(math.pi*80/360)
for i in range (1,361,1):
quinn.left(c)
quinn.forward(1)
wn.exitonclick()
以下是绘制蓝色圆圈的示例函数:
def draw_circle(radius):
turtle.up()
turtle.goto(0,radius) # go to (0, radius)
turtle.begin_fill() # start fill
turtle.down() # pen down
turtle.color('blue')
times_y_crossed = 0
x_sign = 1.0
while times_y_crossed <= 1:
turtle.forward(2*math.pi*radius/360.0) # move by 1/360
turtle.right(1.0)
x_sign_new = math.copysign(1, turtle.xcor())
if(x_sign_new != x_sign):
times_y_crossed += 1
x_sign = x_sign_new
turtle.up() # pen up
turtle.end_fill() # end fill.
return
综合起来:
import turtle
import math
def draw_circle(radius, x, y):
turtle.up()
turtle.goto(x,y+radius) # go to (0, radius)
turtle.begin_fill() # start fill
turtle.down() # pen down
turtle.color('blue')
times_y_crossed = 0
x_sign = 1.0
while times_y_crossed <= 1:
turtle.forward(2*math.pi*radius/360.0) # move by 1/360
turtle.right(1.0)
x_sign_new = math.copysign(1, turtle.xcor())
if(x_sign_new != x_sign):
times_y_crossed += 1
x_sign = x_sign_new
turtle.up() # pen up
turtle.end_fill() # end fill.
return
draw_circle(100, 10, 10)
turtle.goto(-20,10)
turtle.color('red')
turtle.dot(20)
turtle.goto(40,10)
turtle.dot(20)
turtle.pen(shown=False)
turtle.done()
导入海龟
输入数学
def绘制圆(半径,x,y):
乌龟
海龟。转到(x,y+半径)#转到(0,半径)
乌龟,开始加油
乌龟,趴下
海龟。颜色(‘蓝色’)
交叉次数=0
x_符号=1.0
虽然时间过得很快但很抱歉没有给出解释。
第一部分是Ramanujan对pi的近似,但不是很好,因为它只在大约300000次循环迭代之后才达到pi的近似值,并且只精确到小数点后5位。这就是这一部分:
r += (1 / k) * (-1)**i
pi = (4 * (1 - r))
然后我用一个圆的周长:
t.forward(2*5*pi)
最后,我只是让乌龟按时钟方向走20步
import turtle
t = turtle.Turtle()
t.right(90)
t.penup()
t.goto(100, 0)
t.pendown()
i = 0
r = 0
k = 3
while i <= 360:
r += (1 / k) * (-1)**i
pi = (4 * (1 - r))
t.write(pi)
t.forward(2*5*pi)
t.right(20)
i += 1
k += 2
turtle.done()
导入海龟
t=海龟。海龟()
t、 右(90)
t、 彭普()
t、 后藤(100,0)
t、 彭敦()
i=0
r=0
k=3
而对于“没有海龟圈函数”的大多数解决方案都涉及到编写自己的等价于海龟圈函数的函数。但现在已经有两种其他的方法可以用海龟画出轮廓和填充的圆圈
一是可以使用同心点:
请记住,dot()
取直径,而circle()
取半径:
然而,我更喜欢使用冲压来解决这类问题:
''' Build a Snowman without turtle circle function '''
from turtle import Turtle, Screen
# The snowman’s body should be made of 3 filled circles.
# The bottom circle should have a radius of 100 pixels.
# The middle circle should have a radius of 70 pixels.
# The top circle should have a radius of 40 pixels.
RADII = (100, 70, 40)
STAMP_SIZE = 20
# The snowman should be on a blue background
screen = Screen()
screen.bgcolor('blue')
quinn = Turtle('circle')
quinn.setheading(90)
quinn.up()
# The outline of the snowman should be in black, and should be drawn filled with white.
quinn.color('black', 'white')
for not_first, radius in enumerate(RADII):
if not_first:
quinn.forward(radius)
# The outline of each circle should be 3 pixels wide.
quinn.shapesize((radius * 2) / STAMP_SIZE, outline=3)
quinn.stamp()
# Each circle should be centered above the one below it
# There should be no gap between the circles.
quinn.forward(radius)
# Give the snowman eyes
quinn.shapesize(15 / STAMP_SIZE)
quinn.color('black')
quinn.backward(3 * RADII[-1] / 4)
for x in (-RADII[-1] / 3, RADII[-1] / 3):
quinn.setx(x)
quinn.stamp()
# Give the snowman a mouth, and a nose (a hat is optional).
pass
# Make sure to include two stick-arms and at least two fingers on each hand.
pass
quinn.hideturtle()
screen.exitonclick()
这样做的目的是将海龟光标自身扭曲到您需要的位置,在屏幕上对其进行快照,然后将其扭曲到您需要绘制的下一个对象
您可以创建一个函数,该函数使用参数fd和left
这是我创造的
from turtle import *
speed(100000)
for i in range(360):
fd(2)
left(1)
计算如下:范围内的迭代次数除以fd+左。
这就是我得到的近似值。所以您应该能够创建这样的函数。您可以尝试一下,希望这有帮助
def polygon(length, sides):
for i in range(sides):
turtle.forward(length)
turtle.left(360.0/sides)
polygon(1, 360)
从数学角度来看,可以使用中的函数sin
和cos
绘制圆
绘制圆后,使用turtle.begin\u fill()
和turtle.end\u fill()
方法填充圆(尽管我同意在编程中,该方法更实用):
正如回答中所指出的,您可以使用turtle.dot()
方法在屏幕上绘制一个点,
笔的宽度就是圆点的直径
还有另一种方法,但是与dot
方法相比,它是不切实际的。
不管怎样,我还是要把它扔出去,只是为了说明变通方法中有很多可能性:
from turtle import Turtle
def draw_circle(radius, x, y, color="light bue", line_width=3):
c = Turtle(visible=False)
c.penup()
c.goto(x, y)
c.pendown()
# Circle drawing starts here
c.width(radius * 2 + line_width)
c.forward(0)
c.color(color)
c.width(radius * 2 - line_width)
c.forward(0)
# Circle drawing ends here
draw_circle(100, 0, -100)
因此turtle.dot()
相当于turtle.forward(0)
(和turtle.backward(0)
、turtle.goto(turtle.pos())、turtle.setpos(turtle.pos())
,等等)
输出:
除非你的教授特别要求,否则评论每一行被认为是糟糕的编码风格。特别是当这条线的作用显而易见的时候。而且,以你自己的名字命名你的乌龟似乎。。。困惑的小说中一些著名的海龟是米开朗基罗和甘托克。肯定是一只乌龟和一只海豚。虽然这个代码片段是受欢迎的,可能会提供一些帮助,但它将是如何解决这个问题的。如果没有这些,你的答案就没有什么教育价值——记住,你是在将来为读者回答这个问题,而不仅仅是现在提问的人!请在回答中添加解释,并说明适用的限制和假设。
from turtle import *
speed(100000)
for i in range(360):
fd(2)
left(1)
def polygon(length, sides):
for i in range(sides):
turtle.forward(length)
turtle.left(360.0/sides)
polygon(1, 360)
from turtle import Turtle
from math import sin, cos, radians
def draw_circle(radius, x, y, color="light blue", line_width=3):
c = Turtle(visible=False)
c.width(3)
c.penup()
c.goto(x + radius, y)
c.pendown()
c.color("black", color)
c.begin_fill()
# Circle drawing starts here
for i in range(1, 361):
c.goto(radius * cos(radians(i)) + x,
radius * sin(radians(i)) + y)
# Circle drawing ends here
c.end_fill()
draw_circle(100, 0, -100)
from turtle import Turtle
def draw_circle(radius, x, y, color="light bue", line_width=3):
c = Turtle(visible=False)
c.penup()
c.goto(x, y)
c.pendown()
# Circle drawing starts here
c.width(radius * 2 + line_width)
c.forward(0)
c.color(color)
c.width(radius * 2 - line_width)
c.forward(0)
# Circle drawing ends here
draw_circle(100, 0, -100)