Created
February 16, 2017 23:29
-
-
Save raykendo/c67f2a9bcc84955d0d750225420387b0 to your computer and use it in GitHub Desktop.
Getting max distance between a shape and a reference point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
//... | |
/** | |
* defining {object} point | |
* @property {number} x - x coordinate for the point. | |
* @property {number} y - y coordinate for the point. | |
*/ | |
/** | |
* @function getDistanceFromReference | |
* @param {point} reference - center reference point | |
* @returns {function} - calculates distance between some point and current reference. | |
*/ | |
function getDistanceFromReference(reference) { | |
// @param {point} point - object to calculate the distance from the reference. | |
// @returns {number} - distance betwen the point and the reference. | |
return function (point) { | |
const xdiff = reference.x - point.x; | |
const ydiff = reference.y - point.y; | |
return Math.sqrt(xdiff * xdiff + ydiff * ydiff); | |
}; | |
} | |
/** | |
* @function compareDistances | |
* @param {number} prev - previous number to compare | |
* @param {number} current - current number to compare | |
* @returns {number} - larger of the two numbers. | |
*/ | |
function compareDistances(prev, current) { | |
if (current > prev) { | |
return current; | |
} | |
return prev; | |
} | |
/** | |
* @function calculateMaxDistance | |
* @param {point[]} geometry - a list of points | |
* @param {point} reference - a reference point | |
* @returns {number} - largest distance between geometry and reference point. | |
*/ | |
function calculateMaxDistance(geometry, reference) { | |
const getDistance = getDistanceFromReference(reference); | |
const distances = geometry.map(function (geomPoint) { | |
return getDistance(geomPoint); | |
}); | |
return distances.reduce(compareDistances, 0); | |
} | |
//... | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment