What is the best way to find a distance between two points in 2D without using Pythagorean theorem?
-
This is for a software that I'm building and Pythagorean theorem takes some time to calculate because of its square root and powers. Something simpler would be better, even if it's long but only requires simple arithmetic expressions. Note that x and y positions can be float numbers (ex. 100.12 and 200.24) EDITED: Assume that I have an array of points somewhere on earth. I'm given x and y coordinates (longtitude and latitude), and I need to find distance between each of those points as precisely as possible and sort from shortest (nearest) to longest (furthest). The distance is more like distance between you and the nearest restaurant, and not between 2 cities.
-
Answer:
I don't know of any distance algorithm that is more efficient than applying the Euclidean distance formula (which is simply the Pythogorean in coordinate geometry form). Assuming you want the Euclidean distance between points (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2), you can easily break it down into its elementary operations. To do this, you'll need to apply the following concepts: The highest power you'll need to raise a number to in this case is 2 (i.e. a square). A square operation is just a simple arithmetic operation, e.g. x2=xâ xx^2 = x \cdot x A square root (which is where the performance penalty lies) can be computed using a variety of methods. See here for a list of methods: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots Most of them rely only on arithmetic operations. Then simply apply the above to the Euclidean distance formula to get the distance you required, up to a specified level of numerical accuracy: d=â(x1âx2)2+(y1ây2)2d = \sqrt{ (x_1 - x_2)^2 +(y_1 - y_2)^2} Having said that, I doubt this will be faster than simply programming the Euclidean distance formula directly in your programming language of choice. Most languages have built-in low-level implementations of sqrt() that will probably be more efficient than any hand-rolled solutions. Remarks If you're just comparing two distances, you don't even need to perform the square root operation: just compare the squared distances. If you only want a fast approximation of the distance, these articles may be of interest to you. Fast Approximate Distance Functions: http://www.flipcode.com/archives/Fast_Approximate_Distance_Functions.shtml Quake III's inverse square root implementation: http://www.codemaestro.com/reviews/9
Paul Artois at Quora Visit the source
Other answers
To some extent it depends on how much inaccuracy you are willing to live with and what resources you have available. Lookup tables can approximate square roots (or trigonometric functions), and a sufficiently large lookup table might work for your application. What range of values do you expect to see, and what degree of precision is necessary? Another approach is to use parallelism to increase the number of distances computed at once. If you can vectorize the operation, then the latency of a single multiplication or square-root extraction will not be as critical. For example, the Intel SSE extension contains a SQRTPS instruction which can calculate four single-precision square roots at once. You might also be able to achieve greater instruction-level parallelism by interleaving multiple distance computations.
Mark Gritter
Using trigonometric function is an alternative for Pythagorean method. Here are the two steps: 1. find the angle between horizontal line and hypotenuse. θ=tanâ1|y2ây1||x2âx1|\theta= \tan ^{ - 1}\frac{|y2-y1|}{|x2-x1|} 2. Hypotenuse length or the distance = |x2âx1|cos(θ)\frac{|x2-x1|}{cos(\theta)} Now if you have table lookups for cos and arctan functions (only in the range [0 , \pi/2] is required), execution can be faster. Comments on the Edited part of the question: The earth co-ordinates are not given in cartesian co-ordinate systems. Latitude and longitudes are angles and not distances. So, you can not simply apply Pythagoras theorem for distance calculation on earth. For more advanced algorithms, check out Harvesine formula (based on spherical model of earth) and Vincenty's formula (based on oblate spheroid model of earth).
Saptarshi Roy
There are algorithms for finding "pythagorean sums" -- the distances you refer to -- without computing square roots, or even without any multiplications or divisions. The Moler-Morrison algorithm is fastest for high-precision calculations (http://www.scribd.com/doc/29568822/Moler-Morrison-Pythagorean-Sums), and for fast calculation without hardware multiply or divide (or square root), the CORDIC arc-tangent calculation (http://www.convict.lu/Jeunes/Math/arctan.htm) uses only addition and subtraction, and produces the pythagorean sum as a byproduct. If you are interested only in rank-ordering the distances, note that you don't need to take square roots; rank ordering by the square of the distance (that is, x^2 + y^2) is sufficient. If your X,Y coordinates are latitude and longtude, the distances cannot be calculated as simple pythagorean sums.
Randy Hudson
IF, as you say, the coordinates are latitudinal and longitudinal analogues, then you wouldn't use the Pythagorean theorem at all, as you're dealing with spherical coordinates ornithologically projected. The best way, therefore to deal with this problem would be to use a modified CORDIC or BKM algorithm. This algorithm, using nothing more than the standard four mathematical operators and bit shifts,can determine hyperbolic, trigonometric, exponential, and logarithmic expressions in under 50 iterations (for 12+ place accuracy).
Meamma Noone
Related Q & A:
- What is the best way to get a job on cruise ships without spending money?Best solution by cruiseserver.net
- Whats the best way to find a room mate?Best solution by Yahoo! Answers
- What is the best way to find new members for a yahoo group?Best solution by Yahoo! Answers
- What is the best way to find a job?Best solution by Yahoo! Answers
- What is the best way to find a paralegal job?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.