首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python海龟制作圆圈函数

用Python海龟制作圆圈函数
EN

Stack Overflow用户
提问于 2014-10-27 03:58:17
回答 6查看 16.4K关注 0票数 5

我有一项学校的作业:

构建一个不含海龟circle函数的雪人circle

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

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

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

每个圆的轮廓应该是3个像素宽。

底部圆圈的半径应为100像素。

中间圆圈的半径应为70像素。

顶部圆圈的半径应为40像素。

每个圆圈都应该在它下面的那个上面(除了底部的圆圈,它可以在任何地方)。

两个圈子之间不应该有任何差距。

给雪人一个嘴巴,眼睛和鼻子(帽子是可选的)。

确保每只手都有两根棍子和至少两根手指.

到目前为止,我创建了这个,但是在我继续前进之前,我似乎还不能正确地得到这个圆圈。而且,不知道如何将涂成圆圈,或者为眼睛做点。请帮帮我,第一次编码。

代码语言:javascript
复制
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()
EN

回答 6

Stack Overflow用户

发布于 2014-10-27 04:03:31

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

代码语言:javascript
复制
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)添加参数:

代码语言:javascript
复制
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

您可以轻松地添加点,例如:

代码语言:javascript
复制
turtle.goto(-20,10)
turtle.color('red')
turtle.dot(20)
turtle.goto(40,10)
turtle.dot(20)

汇集在一起:

代码语言:javascript
复制
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()

你应该尝试自己完成作业的其余部分。;)

票数 2
EN

Stack Overflow用户

发布于 2017-05-04 12:37:34

很抱歉没有给出解释。第一部分是Ramanujan对pi的近似,但不是很好,因为它只在循环的大约300,000次迭代之后才到达pi的近似,并且它只精确到小数点5位。这部分将是:

代码语言:javascript
复制
    r += (1 / k) * (-1)**i
    pi = (4 * (1 - r))

然后我用圆周:

代码语言:javascript
复制
t.forward(2*5*pi)

最后,我只让乌龟走到了20点钟。

代码语言:javascript
复制
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()
票数 1
EN

Stack Overflow用户

发布于 2017-05-04 18:03:01

“无海龟圆函数”的大多数解决方案都涉及到编写与海龟的圆函数相当的内容。但是已经有另外两种方法可以画出轮廓,满是海龟的圆圈。

一个是你可以使用同心圆点。

代码语言:javascript
复制
turtle.color('black')
turtle.dot(200 + 3)
turtle.color('white')
turtle.dot(200 - 3)

请记住,dot()需要一个直径,而circle()需要一个半径:

然而,我更喜欢使用加盖印花来解决这类问题:

代码语言:javascript
复制
''' 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()

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26580987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档