 # How to calculate Euclidean Distance? How to measure the distance between two points?

## What is Euclidean Distance?

In mathematics, the Euclidean distance or Euclidean metric is the "ordinary" straight-line distance between two points in Euclidean space.
-wikipedia

## One Dimensional

To calcualte the distance between two points in one dimensional is that substract one point from another point. For example, we have two points which are 8 and 2; by subtracting 8 from 2 equals 6. So the distance between these two points are 6. The order of the substraction doesn't matter. If we substract 3 from 8 equals -6. To get the absolute value, we can square the number (-62, `Math.pow(-6, 2)`) which equals to 36. And then we square root of that number (`Math.sqrt(36)`) equals to 6.

``````
/**
* Calculate the distance between two points in one dimensional space
* @param {number} a first point
* @param {number} b second point
* @return {number}
* @time complexity: O(1)
* @space complexity: O(1)
*/
function oneDimensional(a, b) {
let dist = a - b;

if (dist < 0) {
dist = Math.sqrt(Math.pow(dist, 2));
}

return dist;

// Or
// return Math.abs(a - b);
};
```
```

## Two Dimensional

This is the Euclidean Distance formula for two dimensional.  Subtract the x coordinate of the second point from the x coordinate of the first point, and then square that number. Do the same for y coordinates. After that square root the sum of those two number.

For example, we have two points (2, 4) and (-3, 8). First, substract -3 from 2 (-3 - 2) equals -5, and then square -5 equals 25. Second, substract 8 from 4 (8 - 4) equals 4, and then square 4 equals 16. Finally, square root the sum of 25 and 16 (25 + 16), equals 6.403.

``````
/**
* Calculate the distance between two points in two dimensional space
* @param {number[]} a first point, Ex. [2, 4]
* @param {number[]} b second point, Ex. [-3, 8]
* @return {number}
* @time complexity: O(1)
* @space complexity: O(1)
*/
function twoDimensional(a, b) {
return Math.sqrt(Math.pow(b - a, 2) + Math.pow(b - a, 2));
}
```
```

## Three Dimensional

To calculate the Euclidean Distance in three dimensional is similar to two dimensional. The formula as following.  Mike Mai   Brooklyn, New York
I am full-stack web developer, passionate about building world class web applications. Knowledge in designing, coding, testing, and debugging. I love to solve problems.