我有这个学校作业:
建立雪人没有海龟circle
功能
雪人应该在蓝色背景上,并且应该用白色填充。
雪人的轮廓应该是黑色的。
雪人的身体应该由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()
答案 0 :(得分:2)
以下是绘制填充蓝色圆圈的示例函数:
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()
您应该尝试自己完成作业的剩余部分..;)
答案 1 :(得分:1)
很抱歉没有给出解释。 第一部分是Ramanujan对pi的近似,但不是一个非常好的,因为它只能在循环的300,000次迭代之后达到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()
答案 2 :(得分:1)
“没有龟圈功能”的大多数解决方案都涉及编写自己的等同于龟圈功能。但是还有另外两种方法可以用乌龟绘制概述的圆圈。
一个是你可以使用同心点:
turtle.color('black')
turtle.dot(100)
turtle.color('white')
turtle.dot(100 - 6)
请记住,dot()
取直径而circle()
取半径:
但是,我更喜欢使用 stamping 来解决这些问题:
''' 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()
你的想法是将乌龟光标本身扭曲成你需要的东西,在屏幕上制作它的快照,然后将它扭曲到你需要绘制的下一个东西。
答案 3 :(得分:0)
您可以创建一个接受参数fd并左移的函数。
这是我创建的。
private void runCommand(String commandName,
String[] commandArgs) {
try (CommandContext commandContext = createCommandContext()) {
// find and run the command
SparkCommand command = commandContext.findCommand(commandName);
checkSparkResource(command.context.sc());
command.main(commandArgs);
} catch (Exception e) {
logger.error(e.getMessage(), e);
String message = "Something wrong~";
String title = "Run Job on Dataproc:" + commandName + " Fail";
String text = e.getMessage();
SlackNotifier.instance()
.error(message, title, text);
}
}
这是计算:范围中的迭代除以fd + left。 那是我的近似值。因此,您应该能够创建类似的功能。
答案 4 :(得分:0)
您可以尝试一下,希望对您有所帮助!
def polygon(length, sides):
for i in range(sides):
turtle.forward(length)
turtle.left(360.0/sides)
polygon(1, 360)
答案 5 :(得分:0)
从数学的角度来看,您可以使用math
中的函数sin
和cos
绘制圆。
绘制圆后,请使用turtle.begin_fill()
和turtle.end_fill()
方法来填充圆(尽管我确实同意在编程中,this方法更实用) :
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)
正如this答案中指出的那样,您可以使用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())
等,等等)。
输出: