HereIStand
HereIStand

Reputation: 23

turtle drawing automatic centering

I'm looking for best way to automatically find starting position for new turtle drawing so that it would be centered in graphics window regardless of its size and shape.

So far I've developed a function that checks with each drawn element turtle position to find extreme values for left, right, top and bottom and that way I find picture size and can use it to adjust starting position before releasing my code. This is example of simple shape drawing with my picture size detection added:

from turtle import *

Lt=0
Rt=0
Top=0
Bottom=0

def chkPosition():
    global Lt
    global Rt
    global Top
    global Bottom

    pos = position()
    if(Lt>pos[0]):
        Lt = pos[0]
    if(Rt<pos[0]):
        Rt= pos[0]
    if(Top<pos[1]):
        Top = pos[1]
    if(Bottom>pos[1]):
        Bottom = pos[1]

def drawShape(len,angles):
    for i in range(angles):
        chkPosition()
        forward(len)
        left(360/angles)


drawShape(80,12)
print(Lt,Rt,Top,Bottom)
print(Rt-Lt,Top-Bottom)

This method does work however it seems very clumsy to me so I would like to ask more experiences turtle programmers is there a better way to find starting position for turtle drawings to make them centered?

Regards

Upvotes: 0

Views: 7659

Answers (2)

cdlane
cdlane

Reputation: 41905

I admire @furas' explanation and code, but I avoid math. To illustrate that there's always another way to go about a problem here's a math-free solution that produces the same concentric polygons:

from turtle import Turtle, Screen

def draw_shape(turtle, radius, sides):

    # move start point

    turtle.penup()

    turtle.sety(-radius)

    turtle.pendown()

    # draw "almost" circle

    turtle.circle(radius, steps=sides)

turtle = Turtle()

shapes = [(155, 12), (275, 36), (50, 5)]

for shape in shapes:
    draw_shape(turtle, *shape)
    turtle.penup()
    turtle.home()
    turtle.pendown()

screen = Screen()
screen.exitonclick()

enter image description here

Upvotes: 0

furas
furas

Reputation: 142960

There is no universal method to center every shape (before you draw it and find all your max, min points).

For your shape ("almost" circle) you can calculate start point using geometry.

enter image description here

alpha + alpha + 360/repeat = 180 

so

alpha = (180 - 360/repeat)/2

but I need 180-alpha to move right (and later to move left)

beta = 180 - aplha = 180 - (180 - 360/repeat)/2

Now width

cos(alpha) = (lengt/2) / width

so

width = (lengt/2) / cos(alpha)

Because Python use radians in cos() so I need

width = (length/2) / math.cos(math.radians(alpha))

Now I have beta and width so I can move start point and shape will be centered.

from turtle import *
import math

# --- functions ---

def draw_shape(length, repeat):

    angle = 360/repeat

    # move start point

    alpha = (180-angle)/2
    beta = 180 - alpha

    width = (length/2) / math.cos(math.radians(alpha))

    #color('red')
    penup()

    right(beta)
    forward(width)
    left(beta)

    pendown()
    #color('black')

    # draw "almost" circle

    for i in range(repeat):
        forward(length)
        left(angle)

# --- main ---

draw_shape(80, 12)

penup()
goto(0,0)
pendown()

draw_shape(50, 36)

penup()
goto(0,0)
pendown()

draw_shape(70, 5)

penup()
goto(0,0)
pendown()

exitonclick()

I left red width on image.

enter image description here

Upvotes: 1

Related Questions