Distance between 2 points

I want to calculate the rough distance between two points and reduce the computational overhead. I use the following formula

The distance between (x1, y1) and (x2, y2)

Dist = Mod (x1 - x2) + Mod (y1 - y2) 

Where Mod is the module operator Mod (x) = | X |.

This seems to work.

I want to know if I missed something ...

+3
source share
7 answers

As long as you get the absolute value (as you declared | X |) and not use the module function, this will give you the distance in Manhattan between two points

If this is what you want, you haven't missed anything

If you want straight-line distance to use the Pythagorean theorem. This is sqrt ((x1 - x2) ^ 2 + (y1 - y2) ^ 2)

+9
source

A graphical representation of three common distances:

enter image description here

+10
source

Are you sure you have a Modulus statement? It looks like you are using MOD as ABSOLUTE

http://en.wikipedia.org/wiki/Modulo_operation

in any case, as Mehrdad says, using the Pythagorean theory:

 Dist = Sqrt( (x1-x2)^2 + (y1-y2)^2 ) 
+3
source

You must be specific in terms of the distance you want to calculate.

Distance formula:. Given two points (x1, y1) and (x2, y2), the distance between these points is determined by the formula: enter image description here

This is the standard formula that we use in Co-Ordinate geometry to find the distance between points and the MinKowski specialization distance for One Dimension.

+2
source

Your distance metric is great for an approximate distance. But (x 2 - x 1 ) 2 + (y 2 - y 1 ) 2 will give you the square of the actual distance. As long as you remember that this is the square of the distance, it will be more accurate. And depending on the architecture in which you implement this, it may be faster - multiplication may take less time than a branch in a module, or it may well take the same time for hardware implementations. Of course, you need to navigate.

+1
source

If you want to compare distances and save time, use not the distance itself, but its square: (x1-x2) ^ 2 + (y1-y2) ^ 2. Do not take sqrt. Thus, your distances will work exactly the same as regular ones, but quickly. Counting dx = x1-x2 and dx2 = dx * dx is even faster than accepting ABS (you meant this, not MOD really), because the latter is a function, and you have to pay for it.

ABS distance is correct - theoretically. But what is its use if it is crude for your purposes?

+1
source

I made this algorithm to calculate the distance between two points:

 var distance = function(x1, y1, x2, y2) { //Distance Horizantally var horizontalDistance = 0; /Distance Vertically var verticalDistance = 0; if(x1 > x2) { horizantalDistance = x1 - x2; } else { horizantalDistance = x2 - x1; } if(y1 > y2) { verticalDistance = y1 - y2; } else { verticalDistance = y2 - y1; } var answer = 0; if(verticalDistance !== 0 && horizantalDistance !== 0) { //Use the Pathagoreum Theorum answer = Math.sqrt(verticalDistance + horizantalDistance); } else if(horizantalDistance === 0) { //Use the Vertical Distance answer = verticalDistance; } else if (verticalDistance === 0) { //Use the Horizantal distance answer = horizantalDistance; } //Return the answer return answer; } 
0
source

All Articles