# Math question -- point to point distance

Given the x,y of the starting and ending points, I want to calculate the distance travelled. I will appreciate a hint on how to do it with a simple code example.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
hehe..

ok. easy..

you want the distance between two points in 2d space...

its:

distance = sqrt ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

sqrt is the square root..

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
The Pythagorean theorem!
0
Commented:

distance=sqrt(pwr((x2-x1),2)+pwr((Y2-y1),2))

sqrt is the square root and pwr is the power.

0
Commented:
Actually, it is probably best not to use qwr() for this, since expoential algorithms tend to take 100s of times longer than multiplies.
0
Commented:
jep. and they tend to be less accurate than a multiplication by two. this normally doesn't make a difference, but in extreme cases it can be the drop of water that causes a divide by zero..

Nils
0
Commented:
Prove the Pythagorean algorithm.

Interested in mine?
0
Author Commented:
Thanks!
0
Commented:
Okay, got this from a website, but can't remember the addy. anyways:

This small operation calcuates the distance between two points. The routine can work in any number of dimensions, so you cold apply it to 2D or 3D.

In 2D
Define your two points. Point 1 at (x1, y1) and Point 2 at (x2, y2).

xd = x2-x1
yd = y2-y1
Distance = SquareRoot(xd*xd + yd*yd)
In 3D
Define your two points. Point 1 at (x1, y1, z1) and Point 2 at (x2, y2, z2).

xd = x2-x1
yd = y2-y1
zd = z2-z1
Distance = SquareRoot(xd*xd + yd*yd + zd*zd)
As you can see, this requires that you perform a square root. Square roots should be avoided like the plague if you want to write fast code. Only perform a Square Root if you really need to.
Ways to avoid Square Roots:

If you don't need a very accurate distance, you can use a lookup table to calculate it.

If, for example, you are performing collision detection between spheres, and all you want to know is whether or not two have collided, then you do not need to use a square root. Simply change the piece of code from:
if SquareRoot(xd*xd + yd*yd) < Diameter
to:
if (xd*xd + yd*yd) < (Diameter*Diameter)
0
Commented:
>> square roots should be avoided like the
>> plague if you want to write fast code
Obviously you want to avoid any code when there is a faster alternative that is as easy and as relaible, but I don't think that is the case here.  Square roots are bad, but not that bad. On a pentium 1, they take 70 clock cycles, to so you con do still do millions of them a second (assuming you aren't using the CPU for anything else :-) ).  So I would not go to great lengths to avoid them.  The code for the circle is a good example, the lengths you go to is not that far and it saves most of the 70 clock cycles.  but using a look-up table is too much work, probably won't save much (if any) time and won't be accurate.  Now if you are dealign with heavily optimized integer code that must do enormous numbers of squareoots, like you might find in a graphics algorithm, it might make sense to do a look-up, but that is not going to be a typcial case and you are probalby goign to loose any advantage you might gain if you don't write the code in assembly.
0
Commented:

Why using C++ then??? Just write it in assembly. Nowadays you should not be worried about slow code. Only optimize it when really necesarry.
0
Commented:
>> Why using C++ then???
Its a very good language with too many benefits to mention.  Why are you suggesting we shouldn't?

>> Nowadays you should not be