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.
skanadeAsked:
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.

nils pipenbrinckCommented:
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

Your issues matter to us.

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

Start your 7-day free trial
nietodCommented:
The Pythagorean theorem!
0
filipecorreiaCommented:



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

sqrt is the square root and pwr is the power.


0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

nietodCommented:
Actually, it is probably best not to use qwr() for this, since expoential algorithms tend to take 100s of times longer than multiplies.
0
nils pipenbrinckCommented:
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
aperdonCommented:
Prove the Pythagorean algorithm.

Interested in mine?
0
skanadeAuthor Commented:
Thanks!
0
CrystalizedCommented:
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
nietodCommented:
>> 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
aperdonCommented:
Talking about slow/fast.

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
nietodCommented:
>> 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
>> worried about slow code
Yes, then there is good chance that is what you'll get.

>> Only optimize it when really necesarry.
That was my point.  A single squareroot is not worth optimizing.  Even a hundred isn't worth it.  But when you have thousands, as might in some situations, it might be worth it.  Graphics algorithms often do so.  They also tend to optimize trig funnctions which are even worse than squareroots.
0
aperdonCommented:
It was just a joke about C++. Using C++ makes your code dramatically slower, instead of using C or assembly. So why then bother about a few clock-cycles in comparison to the clock-cycles C++ uses for all its internal checking.
You are right about Graphics, but to optimize often depends on your context. And is proven lots of times that it is not always best to optimize the code you expect in the first place. And in the end you discover you should focus on code totally at unexpected places.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.