Search code examples
pythonpython-3.xmathturtle-graphicscartesian-coordinates

See all the coordinates used in a turtle circle() drawing


I want to draw 1 DNA strand with its nitrogen bases: a litle piece of strand, one base, a litle pice of strand, another base, so on. In this order. But when you interrupt a circle(), (in this case a semicircle, the strand) to draw something else, like a straight line (that would be the base), the circle() angle is changed. And I can't think a way to change it back.

So, the easier way, make a semicircle, draw a line, and continue the semicircle, seems to be just goto() a cordinate and draw what you want there.

But to calculate all the precise coordinates of a circle, for each diferent circle, in case I need more, would be long.

There is any way to make turtle, or any other stuff/software, to return as output all the cordinates of a circle that I drew/coded?

Like, if I drew this circle():

from turtle import *

t = Turtle()

t.seth(45)
t.circle(100,90)
t.circle(-100,90)

would it be possible for turtle to return the coordinates used to make it?

And here's an example of what I mean by create a circle using only its coordinates:

from turtle import *

t = Turtle()

def got(x,y,d) :        # to use goto more easily
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.seth(d)

x=0
y=0
d=0
megalist = [5,5,5,5,5,5,5,5,5,5,5,5,1,1,1,1,1,1,1,1,1,1,-1,0,0,0,0,0-1,-1,-1,-1,-1,-1,-1,-1,-1,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,]

for i in megalist :
    x = x + i
    y= y +4
    got(x,y,d)
    t.forward(-1)

Solution

  • would be possible for turtle to return the coordinates used to make it?

    Yes. There are some *_poly() methods, normally used to create custom cursors, that can be used to do what you describe:

    from turtle import Screen, Turtle
    
    screen = Screen()
    turtle = Turtle(visible=False)
    turtle.penup()
    
    turtle.seth(45)
    
    turtle.begin_poly()
    turtle.circle(100, 90)
    turtle.circle(-100, 90)
    turtle.end_poly()
    
    polygon = turtle.get_poly()
    
    print(polygon)
    
    for point in polygon:
        turtle.goto(point)
        turtle.pendown()
    
    screen.exitonclick()
    

    CONSOLE OUTPUT

    > python3 test.py
    ((0.00,0.00), (13.96,17.51), (23.68,37.68), (28.66,59.51), (28.66,81.91),
    (23.68,103.74), (13.96,123.91), (0.00,141.42), (-13.96,158.93),
    (-23.68,179.10), (-28.66,200.94), (-28.66,223.33), (-23.68,245.16),
    (-13.96,265.34), (0.00,282.84))
    >
    

    SCREEN OUTPUT

    enter image description here

    when you interrupt a circle(), (...) to draw something else,like a straight line (...), the circle() angle is changed. And i can't think a way to change it back.

    My belief is you can interrupt circle() by looping through small extents, save the position and heading, do your drawing, and restore the position and heading before the next circle extent iteration. Here's a simple example that just saves and restores the heading as the drawing itself puts back the position:

    from turtle import Screen, Turtle
    
    screen = Screen()
    
    turtle = Turtle(visible=False)
    turtle.speed('fastest')  # because I have no patience
    
    turtle.setheading(45)
    
    for extent in range(9):
        turtle.circle(100, 10)
    
        heading = turtle.heading()
    
        turtle.setheading(0)
        turtle.forward(10)
        turtle.backward(10)
    
        turtle.setheading(heading)
    
    for extent in range(9):
        turtle.circle(-100, 10)
    
        heading = turtle.heading()
    
        turtle.setheading(180)
        turtle.forward(10)
        turtle.backward(10)
    
        turtle.setheading(heading)
    
    screen.exitonclick()
    

    SCREEN OUTPUT

    enter image description here