Using absolute values when determining distance between two points.

1.6k Views Asked by At

When determining distance between two points using trigonometry like so:

x = p2.x - p1.x
y = p2.y - p1.y

distance = Math.sqrt(x * x + y * y)

Should I use the absolute value of X and Y? so:

x = Math.abs(p2.x - p1.x)
y = Math.abs(p2.y - p1.y)

distance = Math.sqrt(x * x + y * y)

Since distance can't be negative? I ask because all of the examples I see online, do not use absolute values, with this exception:

http://www.themathpage.com/alg/pythagorean-distance.htm

But the language I am working with (JavaScript) won't allow me try and get the square root of a negative number. I just want to confirm that getting the absolute value is correct, and doesn't change the distance / value returned.

2

There are 2 best solutions below

2
On BEST ANSWER

The answer is, it does not matter because you immediately square $x$ and $y$. You will get the same answer. $\forall x\in\mathbb{R},x^2\ge 0$. So it will always be the case that $x^2=|x|^2$, and your computation of distance will not differ depending on whether or not you take the absolute value. So calling the function abs() is not necessary.

0
On

Absolute value doesn't change the outcome at all, but is unnecessary. When you square $x$ and $y$ the result will be positive.