繁体   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