Open In App

turtle.setworldcoordinates() function in Python

Improve
Improve
Like Article
Like
Save
Share
Report

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

turtle.setworldcoordinates()

This function is used to set up a user-defined coordinate system. This performs a reset. If mode ‘world’ is already active, all drawings are redrawn according to the new coordinates. 

Note: In user-defined coordinate systems angles may appear distorted.

Syntax : turtle.setworldcoordinates(llx, lly, urx, ury)
 

Parameters:

  • llx: a number, x-coordinate of lower left corner of canvas
  • lly: a number, y-coordinate of lower left corner of canvas
  • urx: a number, x-coordinate of upper right corner of canvas
  • ury: a number, y-coordinate of upper right corner of canvas

Below is the implementation of the above method with some examples :

Example 1 :

Python3




# importing package
import turtle
 
# make screen object and
# set screen mode to world
sc = turtle.Screen()
sc.mode('world')
 
# set world coordinates
turtle.setworldcoordinates(-20, -20, 20, 20)
 
# loop for some motion
for i in range(20):
    turtle.forward(1+1*i)
    turtle.right(90)


Output:

Part A

 Example 2:

Python3




# importing package
import turtle
 
# make screen object and
# set screen mode to world
sc = turtle.Screen()
sc.mode('world')
 
# set world coordinates
turtle.setworldcoordinates(-40, -40, 40, 40)
 
# loop for some motion
for i in range(20):
    turtle.forward(1+1*i)
    turtle.right(90)


Output :

In the  above two examples, the code are the same and only the difference of world coordinates differ the output as shown below :

Example  3 :

Python3




# importing package
import turtle
 
# make screen object and
# set mode to world
sc = turtle.Screen()
sc.mode('world')
 
# set world coordinates
turtle.setworldcoordinates(-50, -50, 50, 50)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*i)
    turtle.right(90)
 
# set world coordinates
turtle.setworldcoordinates(-40, -40, 40, 40)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*(i+16))
    turtle.right(90)
 
# set world coordinates
turtle.setworldcoordinates(-30, -30, 30, 30)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*(i+32))
    turtle.right(90)


Output :

Here, we can see that the all previous drawing is set to new world coordinates ( drawing enlarges ).



Last Updated : 25 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads