Refactor: Extract getStress

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-19 10:56:30 +00:00
parent 8ed4500280
commit 816a086ebd
6 changed files with 30 additions and 53 deletions

19
src/stress.js Normal file
View File

@@ -0,0 +1,19 @@
/**
* Calculates the stress. Basically, it computes the difference between
* high dimensional distance and real distance. The lower the stress is,
* the better layout.
* @return {number} - stress of the layout.
*/
export function getStress(nodes, distance) {
let totalDiffSq = 0, totalHighDistSq = 0;
for (let j = 0; j < nodes.length; j++) {
for (let i = 0; i < j; i++) {
let source = nodes[i], target = nodes[j];
let lowDDist = Math.hypot(target.x - source.x, target.y - source.y);
let highDDist = distance(source, target);
totalDiffSq += Math.pow(highDDist - lowDDist, 2);
totalHighDistSq += highDDist * highDDist;
}
}
return Math.sqrt(totalDiffSq / totalHighDistSq);
}