The Wayback Machine - https://web.archive.org/web/20240213201502/https://www.geeksforgeeks.org/turtle-distance-function-in-python/
Open In App
Related Articles

turtle.distance() function in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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.distance()

This method is used to return the distance from the turtle to (x,y) in turtle step units. 

Syntax : turtle.distance(x, y=None) 
 

Parameters:

x: x coordinate of Vector 2DVec. 

y: y coordinate of Vector 2DVec.

This method can be called in different formats as given below :

distance(x, y) # two coordinates

distance((x, y)) # a pair (tuple) of coordinates

distance(vec) # e.g. as returned by pos()

distance(mypen) # where mypen is another turtle

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

Example 1 :

Python3




# importing package
import turtle
 
# print the distance
# before any motion
print(turtle.distance())
 
# forward turtle by 100
turtle.forward(100)
 
# print the distance
# after a motion
print(turtle.distance())


Output :

0.0
100.0

Example 2 :

Python3




# importing package
import turtle
 
# print distance (default)
print(turtle.distance())
 
for i in range(4):
     
    # draw one quadrant
    turtle.circle(50,90)
     
    # print distance
    print(turtle.distance())


Output :

0.0
70.7106781187
100.0
70.7106781187
1.41063873243e-14 

Example 3: 

Python3




# importing package
import turtle
 
# print distance with arguments
# in different formats
print(turtle.distance(3,4))
print(turtle.distance((3,4)))
print(turtle.distance((30.0,40.0)))


Output :

5.0
5.0
50.0

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 26 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials