[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Math question -- point to point distance

Posted on 2000-02-12
12
Medium Priority
?
337 Views
Last Modified: 2010-04-10
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.
0
Comment
Question by:skanade
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 4

Accepted Solution

by:
nils pipenbrinck earned 200 total points
ID: 2515499
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
 
LVL 22

Expert Comment

by:nietod
ID: 2515546
The Pythagorean theorem!
0
 
LVL 1

Expert Comment

by:filipecorreia
ID: 2516273



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

sqrt is the square root and pwr is the power.


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 22

Expert Comment

by:nietod
ID: 2516289
Actually, it is probably best not to use qwr() for this, since expoential algorithms tend to take 100s of times longer than multiplies.
0
 
LVL 4

Expert Comment

by:nils pipenbrinck
ID: 2517037
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
 
LVL 1

Expert Comment

by:aperdon
ID: 2517162
Prove the Pythagorean algorithm.

Interested in mine?
0
 

Author Comment

by:skanade
ID: 2517455
Thanks!
0
 

Expert Comment

by:Crystalized
ID: 2520362
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
 
LVL 22

Expert Comment

by:nietod
ID: 2522545
>> 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
 
LVL 1

Expert Comment

by:aperdon
ID: 2524994
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
 
LVL 22

Expert Comment

by:nietod
ID: 2525136
>> 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
 
LVL 1

Expert Comment

by:aperdon
ID: 2525173
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question