Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
Created May 20, 2015 15:47
Show Gist options
  • Save ben-bradley/b82761df74f5758a1f50 to your computer and use it in GitHub Desktop.
Save ben-bradley/b82761df74f5758a1f50 to your computer and use it in GitHub Desktop.
Calculate the distance between two points in 3d space
// http://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space
/**
* Calculate the square root of a number
* @param {Number} n The number for which to find a square root
* @returns {Number} Returns the square root of n
*/
function sqrt(n) {
return Math.sqrt(n);
}
/**
* Calculate the absolute value of a number
* @param {Number} n The number for which to find an absolute value
* @returns {Number} Returns the absolute value of n
*/
function abs(n) {
return Math.abs(n);
}
/**
* Squares a number
* @param {Number} n The number to square
* @returns {Number} Returns the square of n
*/
function sq(n) {
return n * n;
}
/**
* Calculate the Pythagorean Theorem
* @param {Number} a The "a" in the formula
* @param {Number} b The "b" in the formula
* @returns {Number} Returns the length of the hypotenuse
*/
function pt(a, b) {
return sqrt(sq(a) + sq(b));
}
/**
* Calculates the distance between points
* @param {Mixed} a An array or an object with coordinates
* @param {Mixed} b An array or an object with coordinates
* @returns {Object} Returns an array or object with distances
*/
function distance(a, b) {
var x, y, z;
if (Array.isArray(a)) {
x = abs(a[0] - b[0]);
y = abs(a[1] - b[1]);
z = abs(a[2] - b[2]);
} else {
x = abs(a.x - b.x),
y = abs(a.y - b.y),
z = abs(a.z - b.z);
}
return pt(pt(x, y), z);
}
// calculating with two objects
var d1 = distance(
{ x: 10, y: 10, z: 10 },
{ x: 10, y: 8, z: 9 }
)
// calculating with an array
var d2 = distance(
[10, 10, 10],
[-1, -2, -3]
);
console.log(d1, d2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment