簡體   English   中英

圓圈不對齊/對稱,蟒蛇龜

[英]Circles not aligned/symmetrical , python turtle

我看到每個圓圈后海龜光標的位置和角度都是正確的,但是在繪制最后一個圓圈時似乎有偏移。 我似乎無法弄清楚如何擺脫偏移。

import turtle
import math
window = turtle.Screen()
window.bgcolor('cyan')

loop = 90
angle = 4
step = 8
c = loop * step
r = ((c/2)/(math.pi))
d = c/(math.pi)
[enter image description here][1]
the_turtle = turtle.Turtle()
the_turtle.speed(500)
print(the_turtle.heading())
print(the_turtle.position())

for x in range(loop): 
    the_turtle.forward(step)
    the_turtle.left(angle)

print(the_turtle.heading())
the_turtle.penup()
the_turtle.setposition(0,r)
the_turtle.pendown()
print(the_turtle.position())

for x in range(loop):
    the_turtle.forward(step)
    the_turtle.left(angle)

the_turtle.left(180)
print(the_turtle.heading())
the_turtle.penup()
the_turtle.setposition(0,r)
the_turtle.pendown()
print(the_turtle.position())

for x in range(loop):
    the_turtle.forward(step)
    the_turtle.left(angle)

turtle.exitonclick()

問題是你在不同的方向畫了最后一個圓圈 - 所以第一個和第二個圓圈從 0 到 +8 向右繪制第一條線,但最后一個圓圈從 0 到 -8 向左繪制第一條線。

它需要the_turtle.backward(step)在最后一個圓之前,它將繪制從 +8 到 0 的第一條線 - 因此它將與其他圓位於同一位置。

順便說一句:可能如果您嘗試繪制矩形,那么您應該更好地了解這個問題。

要在沒有backward()情況下繪制它,您必須先繪制半步,接下來的 359 個完整步,最后是半步。



import turtle
import math

window = turtle.Screen()
window.bgcolor('cyan')

loop = 90
angle = 360/loop  
step = 8

c = loop * step
r = ((c/2)/(math.pi))
d = c/(math.pi)

the_turtle = turtle.Turtle()
the_turtle.speed(500)

print(the_turtle.heading())
print(the_turtle.position())

for x in range(loop): 
    the_turtle.forward(step)
    the_turtle.left(angle)

print(the_turtle.heading())
the_turtle.penup()

the_turtle.setposition(0, r)
the_turtle.pendown()
print(the_turtle.position())

for x in range(loop):
    the_turtle.forward(step)
    the_turtle.left(angle)

the_turtle.left(180)
print(the_turtle.heading())
the_turtle.penup()
the_turtle.setposition(0, r)
the_turtle.pendown()
print(the_turtle.position())

the_turtle.backward(step)  # <----

for x in range(loop):
    the_turtle.forward(step)
    the_turtle.left(angle)

turtle.exitonclick()

在我看來,如果將兩個相鄰的圓圈視為一個數字 8,我們可以大大簡化事情:

from turtle import Screen, Turtle
from math import pi

steps = 90
step_size = 8
angle = 360 / steps

circumference = steps * step_size
diameter = circumference / pi
radius = diameter / 2

screen = Screen()
screen.bgcolor('cyan')

turtle = Turtle()
turtle.speed('fastest')

for _ in range(2):
    for _ in range(steps):
        turtle.forward(step_size)
        turtle.right(angle)

    angle = -angle

turtle.penup()
turtle.sety(radius)
turtle.pendown()

for _ in range(steps):
    turtle.forward(step_size)
    turtle.right(angle)

turtle.hideturtle()
screen.exitonclick()

作為副作用,這與原始圖形不同,它會在窗口上垂直居中繪制圖形。 此外,我們可以計算angle而不是使用常數。 並查找speed()方法的有效參數。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM