turtle.setworldcoordinates() function in Python

The turtle.setworldcoordinates() function sets up a user-defined coordinate system. This changes the mapping of pixels to coordinates, enabling non-standard graphs.

Syntax: turtle.setworldcoordinates(llx, lly, urx, ury)
Parameters: llx, lly – a number (x and y coordinates of lower-left corner); urx, ury – a number (x and y coordinates of upper-right corner).

Let us see an example to implement the turtle.setworldcoordinates() function in Python:

Demo54.py

# turtle.setworldcoordinates() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
# Set custom coordinate system (llx, lly, urx, ury)
window.setworldcoordinates(-1, -1, 1, 1)

t = turtle.Turtle()
t.circle(0.5)  # Draw circle with radius 0.5 in new coordinates

window.exitonclick()

The following is the output:

turtle.setworldcoordinates() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Set world coordinates: window.setworldcoordinates(-1, -1, 1, 1) defines a custom logical coordinate system where the lower-left corner is (-1, -1) and the upper-right corner is (1, 1). All turtle movements now use these units.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Draw circle: t.circle(0.5) draws a circle of radius 0.5 in the new coordinate system.
  • Close on click: window.exitonclick() waits for a mouse click, then closes the window.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.Screen().turtles() function in Python
turtle.textinput() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment