Skip to content

Instantly share code, notes, and snippets.

@raykendo

raykendo/example.js

Created Feb 16, 2017
Embed
What would you like to do?
Getting max distance between a shape and a reference point
(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