You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1012 B

/**
* Calculates the centroid of an array of entries.
* The centroid is the point that is the mean of all the points in the array.
* The mean is calculated using the weight of each point, which defaults to 1.
* @param {Object[]} entries - The array of entries to calculate the centroid from.
* @param {function} [distanceFunction] - A distance function to use to calculate the weight of each point.
* @returns {number[]} The centroid of the array of entries.
*/
function calculateCentroid(entries, distanceFunction) {
if (entries.length === 0)
return;
const length = entries[0].point.length;
const sum = new Array(length).fill(0);
let totalWeight = 0;
for (const entry of entries) {
const weight = entry.radius || 1;
//const weight = distanceFunction(entry.point, this.point);
for (let i = 0; i < length; i++) {
sum[i] += entry.point[i] * weight;
}
totalWeight += weight;
}
return sum.map(val => val / totalWeight);
}
module.exports = { calculateCentroid };

Powered by TurnKey Linux.