• Advertisement
Sign in to follow this  

Math Check: Distance Between 2 Obbjects

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to knoow if my math is correct in calculating the distance between 2 objects on a 2D plain:
[CODE]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]))[/CODE]


Thanks.

Share this post


Link to post
Share on other sites
Advertisement
[quote name='Shippou' timestamp='1326567700' post='4902719']
I would like to knoow if my math is correct in calculating the distance between 2 objects on a 2D plain:
[CODE]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]))[/CODE]


Thanks.
[/quote]

Yes, that is correct.

-Josh

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement