turtle.setworldcoordinates() function in Python
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
import turtle
sc = turtle.Screen()
sc.mode( 'world' )
turtle.setworldcoordinates( - 20 , - 20 , 20 , 20 )
for i in range ( 20 ):
turtle.forward( 1 + 1 * i)
turtle.right( 90 )
|
Output:
Part A
Example 2:
Python3
import turtle
sc = turtle.Screen()
sc.mode( 'world' )
turtle.setworldcoordinates( - 40 , - 40 , 40 , 40 )
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
import turtle
sc = turtle.Screen()
sc.mode( 'world' )
turtle.setworldcoordinates( - 50 , - 50 , 50 , 50 )
for i in range ( 16 ):
turtle.forward( 1 + 1 * i)
turtle.right( 90 )
turtle.setworldcoordinates( - 40 , - 40 , 40 , 40 )
for i in range ( 16 ):
turtle.forward( 1 + 1 * (i + 16 ))
turtle.right( 90 )
turtle.setworldcoordinates( - 30 , - 30 , 30 , 30 )
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
Share your thoughts in the comments
Please Login to comment...