Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the distance between two points using return methods in python?

I'm still new to python and have been trying to get the hang of it. I've been trying to learn simple return methods but I can't seem to get the hang of it. I have been trying to find the distance between two points and this is what I have so far. If anyone could help me figure this out it would be very helpful! Thank you!

import math

def calculateDistance(x1,y1,x2,y2):
     dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
     return dist

calculateDistance(2,4,6,8)

print calculateDistance
like image 501
User1222 Avatar asked Jan 26 '14 21:01

User1222


1 Answers

Why don't you use math.hypot() to calculate the distance?

>>> import math
>>> p1 = (3, 5)  # point 1 coordinate
>>> p2 = (5, 7)  # point 2 coordinate
>>> math.hypot(p2[0] - p1[0], p2[1] - p1[1]) # Linear distance 
2.8284271247461903
like image 166
Fernando Avatar answered Sep 29 '22 01:09

Fernando