Sign in to follow this  
mrtie_dye

Finding the distance between two points

Recommended Posts

mrtie_dye    184
Could someone refresh my memory on how to find the distance between two points on a 2d grid? I am pretty sure it involves a right triangle, but the formula escapes me. Thanks for any help.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
think pythagoras



distance = squareroot( (x1-x2)^2 + (y1-y2)^2 )



where x1,y1 is point1 and y1,y2 is point2

Share this post


Link to post
Share on other sites
kzar    170
if i remember correctly you treat it like a triangle. You go horizontaly along from the first point untill you are below / above the second point and then you go up / down to the second point. You also join the two points and you have a triangle. You know the length of the horizontal line and the vertical line and you can then use pythagorus (however its spelt) to find the line thats between the two points.

Share this post


Link to post
Share on other sites
choffstein    1090
Quote:
Original post by Anonymous Poster
think pythagoras



distance = squareroot( (x1-x2)^2 + (y1-y2)^2 )



where x1,y1 is point1 and y1,y2 is point2


He means x1,y1 is point 1, x2,y2 is point 2.

Share this post


Link to post
Share on other sites
mattor    154
Yeah it's derived from the Pythagorean theorem. The distance is
sqrt((x2-x1)^2 + (y2-y1)^2)

(obviously you can switch the point orders since you're squaring the difference anyway)

Share this post


Link to post
Share on other sites
Justin2006    134
The distance between 2 points on a 2D plane is the hypotenuse of the right triangle they form.

So it would look like this:
__/|
c/ |b
/__|
_a
Where the bottom left point and the top point are the 2 points in question. The hypotenuse is the distance between them. To find this distance you can use trig ( this is how you get the formula ).

Since you know that c = the x value of point A - the x value of point B
and that b = the y value of point A - the y value of point B,
you can figure the value of A through several ways:
Pythagorean ( a squared + b squared = c squared )
or Trigonometrically ( cosecant of b / a = value of c )

Most use the pythagorean though.. which means your formula will be:

float getDistance( float *point1, float *point2 )
{
return (float)abs( ( ( point2[0] - point1[0] ) * ( point2[0] - point1[0] ) ) + ( ( point2[1] - point1[1] ) * ( point2[1] - point1[1] ) ) );
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this