简体   繁体   中英

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.

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.

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.



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:

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. And look up the valid arguments to the speed() method.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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