使用Python turtle在圆函数之外创建圆

2024-06-06 22:20:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个学校作业:

建造一个没有海龟功能的雪人

雪人应该在蓝色的背景上,应该画满白色。

雪人的轮廓应该是黑色的。

雪人的身体应该是由三个充满的圆圈组成的。

每个圆的轮廓应该是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()

Tags: tofor半径math像素leftallowsforward
3条回答

很抱歉没有解释。 第一部分是Ramanujan对π的近似,但不是很好,因为它只在循环的300000次迭代之后达到π的近似,并且它只精确到小数点后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()

“没有海龟圈函数”的大多数解决方案都涉及到编写与海龟圈函数等价的函数。但现在已经有另外两种方法可以画出轮廓分明、充满乌龟的圆圈。

一种是可以使用同心点:

turtle.color('black')
turtle.dot(100)
turtle.color('white')
turtle.dot(100 - 6)

记住,dot()表示直径,而circle()表示半径:

enter image description here

但是,我更喜欢使用冲压来解决这些问题:

''' 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()

这个想法是你扭曲海龟光标本身到你需要的,在屏幕上做一个快照,然后扭曲它到下一个你需要画的东西。

enter image description here

下面是一个绘制蓝色填充圆的示例函数:

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

然后,您可以修改上述函数,添加圆中心位置(x,y)的参数:

def draw_circle(radius, x, y):    
    turtle.up()
    turtle.goto(x,y+radius) # go to (x, y + 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

你可以很容易地添加点,例如:

turtle.goto(-20,10)
turtle.color('red')
turtle.dot(20)
turtle.goto(40,10)
turtle.dot(20)

汇总:

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()

你应该试着自己完成任务的剩余部分。。;)

相关问题 更多 >