Jump to content

  • Log In with Google      Sign In   
  • Create Account


Math Check: Distance Between 2 Obbjects


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Shippou   Members   -  Reputation: 1471

Like
0Likes
Like

Posted 14 January 2012 - 01:01 PM

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.

 Reactions To Technologies:
1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works.
2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it.
3. Anything invented after you're thirty-five is against the natural order of things.

- Douglas Adams 2002


 


Sponsor:

#2 jjd   GDNet+   -  Reputation: 2075

Like
0Likes
Like

Posted 14 January 2012 - 01:15 PM

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

--www.physicaluncertainty.com
--linkedin
--irc.freenode.net#gdnet


#3 HappyCoder   Members   -  Reputation: 2561

Like
0Likes
Like

Posted 14 January 2012 - 01:16 PM

yup. That distance equation will give you the distance between two points.

#4 Cornstalks   Crossbones+   -  Reputation: 6974

Like
0Likes
Like

Posted 14 January 2012 - 01:16 PM

Yes, your math is correct.

[edit]

Holy crap! Ninja'd twice!
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#5 Shippou   Members   -  Reputation: 1471

Like
0Likes
Like

Posted 14 January 2012 - 01:42 PM

Thank you.

 Reactions To Technologies:
1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works.
2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it.
3. Anything invented after you're thirty-five is against the natural order of things.

- Douglas Adams 2002


 


#6 Narf the Mouse   Members   -  Reputation: 318

Like
1Likes
Like

Posted 16 January 2012 - 03:37 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS