# Math Check: Distance Between 2 Obbjects

This topic is 2520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I would like to knoow if my math is correct in calculating the distance between 2 objects on a 2D plain:
def distance(x1,x2,y1,y2): # A squared + B squared = C squared return ( ( (x2 - x1)**2) + ( (y2-y1)**2) )**.5 # Object 1 Location coordinate1 = 2,8 # x,y # Object 2 Location coordinate2 = 5,10 # x,y print (distance(coordinate1[0],coordinate2[0],coordinate1[1],coordinate2[1]))

Thanks.

##### Share on other sites

I would like to knoow if my math is correct in calculating the distance between 2 objects on a 2D plain:
def distance(x1,x2,y1,y2): # A squared + B squared = C squared return ( ( (x2 - x1)**2) + ( (y2-y1)**2) )**.5 # Object 1 Location coordinate1 = 2,8 # x,y # Object 2 Location coordinate2 = 5,10 # x,y print (distance(coordinate1[0],coordinate2[0],coordinate1[1],coordinate2[1]))

Thanks.

Yes, that is correct.

-Josh

##### Share on other sites
yup. That distance equation will give you the distance between two points.

##### Share on other sites

Holy crap! Ninja'd twice!

Thank you.

##### Share on other sites
As a side note, it's also useful to include a "DistanceSquared" function, which omits the square root - If you're simply comparing which is farther, and don't need to know how far, omitting a costly square root can save significant time in time-sensitive applications.

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633656
• Total Posts
3013186
×