21 lines
736 B
JavaScript
21 lines
736 B
JavaScript
/**
|
|
* Calculates the stress of a system by comparing the difference between the
|
|
* high dimensional distance and 2D distance. The lower the stress contributes
|
|
* to the better layout.
|
|
* @return {number} - stress of the layout.
|
|
*/
|
|
export function getStress(nodes, distance) {
|
|
let sumDiffSq = 0
|
|
let sumHighDistSq = 0;
|
|
for (let j = nodes.length-1; j >= 1; 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);
|
|
sumDiffSq += Math.pow(highDDist - lowDDist, 2);
|
|
sumLowDDistSq += lowDDist * lowDDist;
|
|
}
|
|
}
|
|
return Math.sqrt(sumDiffSq / sumLowDDistSq);
|
|
}
|