简体   繁体   English

圆圈不对齐/对称,蟒蛇龟

[英]Circles not aligned/symmetrical , python turtle

I see that the position and angle of the turtle cursor is correct after each circle, but there seems to be an offset when drawing the last circle.我看到每个圆圈后海龟光标的位置和角度都是正确的,但是在绘制最后一个圆圈时似乎有偏移。 I can't seem to figure out how to get rid of the offset.我似乎无法弄清楚如何摆脱偏移。

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()

Problem is that you draw last circle in different direction - so first and second circle draws first line to the right from 0 to +8 but last circle draw first line to the left from 0 to -8.问题是你在不同的方向画了最后一个圆圈 - 所以第一个和第二个圆圈从 0 到 +8 向右绘制第一条线,但最后一个圆圈从 0 到 -8 向左绘制第一条线。

It needs the_turtle.backward(step) before last circe and it will draw first line from +8 to 0 - so it will be in the same place as in other circles.它需要the_turtle.backward(step)在最后一个圆之前,它将绘制从 +8 到 0 的第一条线 - 因此它将与其他圆位于同一位置。

BTW: probably if you try to draw rectangles then you should better see this problem.顺便说一句:可能如果您尝试绘制矩形,那么您应该更好地了解这个问题。

To draw it without backward() you would have to first plot half step, next 359 full steps, and finally half step.要在没有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()

It seems to me we can simplify things greatly if think of the two adjacent circles as a single figure eight:在我看来,如果将两个相邻的圆圈视为一个数字 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()

As a side effect, this draws the figure centered vertically on the window, unlike the original.作为副作用,这与原始图形不同,它会在窗口上垂直居中绘制图形。 Also, we can compute angle rather than use a constant.此外,我们可以计算angle而不是使用常数。 And look up the valid arguments to the speed() method.并查找speed()方法的有效参数。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM