如何让我的线条不与乌龟交叉?

时间:2017-12-27 23:04:15

标签: python turtle-graphics

这是我的代码的一部分(尚未准备好)。我需要让圆圈的线不交叉,假装是重叠物体。我不想填空白圈。提前谢谢!

# G7
for i in range (140,0,-20):
    turtle.up()
    turtle.sety(160)
    turtle.setx(i)
    turtle.down()
    turtle.setheading(90)
    turtle.circle(i)

# G8
for i in range (160,0,-20):
    turtle.up()
    turtle.sety(0)
    turtle.setx(i)
    turtle.down()
    turtle.setheading(90)
    turtle.circle(i,180)

Python Drawing.jpg

1 个答案:

答案 0 :(得分:0)

显而易见的方法似乎是使用 fill

import turtle

turtle.color('black', 'white')
turtle.speed('fastest')

for i in range(140, 0, -20):
    turtle.up()
    turtle.setposition(i, 160)
    turtle.down()
    turtle.setheading(90)
    turtle.circle(i)

for i in range(160, 0, -20):
    turtle.up()
    turtle.setposition(i, 0)
    turtle.down()
    turtle.setheading(90)
    turtle.begin_fill()
    turtle.circle(i, 180)
    turtle.end_fill()

turtle.hideturtle()
turtle.mainloop()

enter image description here

但是既然你表示你不想使用 fill ,另一种方法是使用标记

from turtle import Turtle, Screen

CURSOR_SIZE = 20

screen = Screen()

turtle = Turtle('circle', visible=False)
turtle.color('black', 'white')
turtle.penup()

turtle.sety(140)

for i in range(240, 0, -40):
    turtle.shapesize(i / CURSOR_SIZE)
    turtle.stamp()

turtle.sety(0)

for i in range(280, 0, -40):
    turtle.shapesize(i / CURSOR_SIZE)
    turtle.stamp()

turtle.sety(-10)

turtle.begin_poly()
turtle.circle(10, 180, 12)
turtle.end_poly()

screen.register_shape("semicircle", turtle.get_poly())
turtle.shape("semicircle")

turtle.sety(-160)

for i in range(320, 0, -40):
    turtle.shapesize(i / CURSOR_SIZE)
    turtle.stamp()

screen.mainloop()

enter image description here

但如果这些都不吸引你,你可以通过计算每个弧的起点位于其下方的圆圈的位置来简单地绘制图像,以避免重叠的线条。即做数学。