Jump to content
  • Advertisement
Sign in to follow this  
RLS0812

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.

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:
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 this post


Link to post
Share on other sites
Advertisement

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 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!