Finding the distance between two points

Started by
5 comments, last by mrtie_dye 19 years ago
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.
Marriage is the #1 cause of divorce
Advertisement
think pythagoras



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



where x1,y1 is point1 and y1,y2 is point2
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.
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.
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)
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] ) ) );
}
Wow. You guys are great. Thanks for the help. I think I got it now.
Marriage is the #1 cause of divorce

This topic is closed to new replies.

Advertisement