Files
d3-spring-model/examples/js/distances/euclideanDistance.js
Pitchaya Boonsarngsuk 61f2be55fe Init from given files
2017-11-07 21:33:16 +00:00

33 lines
1.1 KiB
JavaScript

/**
* Calculate the distances by using the numbers, strings and dates.
* @param {node} source
* @param {node} target
* @param {array} properties - the properties of the nodes.
* @return {number} the distance between source and target nodes.
*/
function calculateEuclideanDistance(source, target, properties, normArgs) {
var sumDiff = 0.0;
// console.log(normArgs);
// Iterate through every column of data
for (var i = 0; i < properties.length; i++) {
property = properties[i];
if (property.toLowerCase() !== "class" && property.toLowerCase() !== "app" && property.toLowerCase() !== "user" && property.toLowerCase() !== "weekday") {
var s = source[property],
t = target[property];
if (normArgs.sig[i] !== 0) {
s = (s - normArgs.avg[i]) / (2.0 * normArgs.sig[i]);
t = (t - normArgs.avg[i]) / (2.0 * normArgs.sig[i]);
}
sumDiff += (s - t) * (s - t);
}
}
// console.log(Math.sqrt(sumDiff)/cols);
// console.log(cols);
// sumDiff = Math.sqrt(sumDiff);
// console.log(sumDiff);
return Math.sqrt(sumDiff);
}