## Finding distance in 2 dimensions

It's fairly easy to find the distance between two points on a two-dimensional plane. By that we mean we'll be using points that can be written as a pair of coordinates, labeled "X" and "Y".

You've probably already been exposed to the algorithm used in this process. It's the "Pythagorean theorem". And it's written like so...

...or alternately written in plain speak:

"A squared plus B squared equals C squared."

Now if you read the actual summary of what this does you'll see it relates to finding the "hypotenuse" of of a right-angled triangle. In other words the longest side of a triangle where two of the sides are 90 degrees to one another.

How does this help us find distance? Simple, if we think of all space as a grid then we can imagine a rectangle shape on that grid where two of the opposite corners of the rectangle are defined by two specific points. We don't need four points to define the entire rectangle because, since it exists on a grid, we can follow the lines from the two pre-existing points to a new point of intersection any time we want.

Furthermore, if we think of a rectangle as if it were two triangles together, then we can treat half of that rectangle as a right-angled triangle and use those same two points to find the diagonal line between those two points. A diagonal line that cuts a rectangle in half is no different than the hypotenuse of a right angled triangle.

Here is an interactive summary of how this works.

To actually write the equation we need to start by finding "A" and "B".

It won't matter if "A" refers to the horizontal or vertical directions, but for the sake of this project, we'll presume "A" is the horizontal "X" direction within our 2D space.

```
A = p2.x - p1.x;
```

In the code above "p2" refers to "Point 2" and "p1" will of course refer to "Point 1". If we subtract the "X" position of p2 from p1 we'll always get the difference which is equivalent to the distance between the two on the x axis. Even if the "X" or p2 is lower than p1, resulting in a negative value, the distance value is still the same with the exception of it being a negative number.

Whether the final value is positive or negative doesn't matter as when we later on multiply it by itself it will, because of the rules of math, automatically become positive. Because of this it also doesn't matter if the equation subtracts p1 from p2 or p2 form p1.

Now you'll probably be asking how exactly one would go about writing exponential powers in code. In most languages you simply can't. But that just means we need to rephrase how the theorem is written. Since it only needs powers of "2" that means whatever has that exponent needs to be multiplied by itself.

Thus we can write out a power of two like so...

(take "Asp" as "A to the second power")

```
Asp = (p2.x - p1.x) * (p2.x - p1.x);
```

Of course you'll need the above math twice, one for the "A" value, and once for the "B" value.

Then there's the power of the result value "C".

We can't start with "C" times itself since we'd need to know what "C" is before we could actually multiply it. Remember our equation returns "C to the 2nd power" and not just "C". Because of that we're actually looking for the **square root** of "C to the 2nd power" for our final value.

Luckily, as talked about in previous sections, most programming environments include a built-in ability to compute a square root.

Thus we can write the entire equation like so...

```
A = (p2.x - p1.x) * (p2.x - p1.x);
B = (p2.x - p1.x) * (p2.x - p1.x);
Csp = A + B;
distance = Math.sqrt( Csp );
```

Even better, we can simplify the entire thing to a single line...

```
distance = Math.sqrt( ( (p2.x - p1.x) * (p2.x - p1.x) ) + ( (p2.y - p1.y) * (p2.y - p1.y) ) );
```

If you want a longer explanation of the algorithm and its various uses you can read the Wikipedia page for extra smarty pants points.

## Finding distance in 3 dimensions

Finding the distance between two points in a three dimensional space is amazingly logical. We are adding an extra dimension, so we take the same algorithm, and add a third line to the calculation.

It couldn't be simpler...

```
distance = Math.sqrt( (p2.x - p1.x) * (p2.x - p1.x) +
(p2.y - p1.y) * (p2.y - p1.y) +
(p2.z - p1.z) * (p2.z - p1.z) );
```

As you can see we are essentially still findaing the length of a diagonal edge in a right angle triangle but doing so in 3d space instead of 2d.

And that's it.