Skip to content

Turtle Positioning: goto, setpos, setx, sety, setheading

Turtle Positioning Tutorial

Introduction

  • In this tutorial we will look at the PIL methods that can be used to resize an image.
  • We will also check out another method that can be used to get the size (resolution) of an image.

Let’s first open an image so we can resize it.

.goto method

After

  • We’re going to need Image and ImageFilter modules from PIL library.
    • from PIL import Image, ImageFilter

.setpos method

setpos (also can be used as .setposition()) can be used to set a position for turtle.

If you combine it with penup() and pendown() methods you can avoid drawing while positioning the turtle.

  • Technically all of these methods will do the same thing and move turtle to coordinates (100,100):
    • turtle.setpos(100,100)
    • turtle.setposition(100,100)
    • turtle.goto(100,100)

.setx() method

setx can be used to change the turtle’s positing along the x-axis (horizontally) while vertical position is untouched. 

It can be used as: turtle.sety(-200) to go to 200 below zero horizontally.

.sety() method

sety can be used to change the turtle’s positing along the y-axis (vertically) while horizontal position is untouched. 

It can be used as: turtle.sety(-200) to go to 200 below zero vertically.

.setheading method

This method can be used to set the heading of turtle. While other methods such as left or right can be used to turn and rotate relatively, setheading offers an absolute way to set heading.

  • 0: Turtle will turn East
  • 90: Turtle will turn North
  • 180: Turtle will turn West
  • 270: Turtle will turn South

For instance, you can use turtle.heading(180) to turn your turtle to the West.

Example 1: Primitive Coat Hanger

We can use our newly acquired turtle positioning knowledge to create a simple coat hanger without the hook. I think it actually exists in hotel wardrobes for practicality.

  • We will assign a new color at each step to highlight the usage of each method
  • We’ll go to bottom left first using: .setpos(x, y)
  • Then we’ll move laterally to the bottom right corner: .setx(x, y)
  • We’ll then go back to origin (0,0) using: .goto(x, y)
  • Finally we’ll draw vertically for the hanging stick part using: .sety(x, y)
import turtle

turtle.color("green")
turtle.setpos(-200,-100)

turtle.color("blue")
turtle.setx(200)

turtle.color("orange")
turtle.goto(0,0)

turtle.color("red")
turtle.sety(50)

turtle.exitonclick()
Coat hanger with Python turtle

Example 2: Left Single Angle Quotation Mark

Let’s draw a left single angle quotation mark only using heading and forward with turtle in Python.

Writing the name of this character is almost harder than drawing it with turtle.

  • We’ll first set heading to Northwest: .setheading(135)
  • Then we’ll move forward: .fd(100)
  • We’ll then set heading to Northeast: .setheading(45)
  • We’ll move forward once again: .forward(100)
import turtle

turtle.color("red")

turtle.setheading(135)
turtle.forward(100)

turtle.setheading(45)
turtle.forward(100)

turtle.exitonclick()
Left angle single quotation mark drawing with turtle

You can hide turtle icon for a better result.

Summary

In this Python Tutorial, we’ve seen how to resize an image and different scenarios regarding the new size.

We have also seen hot to get the size of an image and we saw an example regarding how to make use of this information.

You can really see the power of PIL library in image processing as well as basic image editing tasks through these operations.