Add an API entry to calculate stress
This commit is contained in:
3
index.js
3
index.js
@@ -18,3 +18,6 @@ export { default as forceLinkTweaked}
|
|||||||
|
|
||||||
export { default as hybridSimulation}
|
export { default as hybridSimulation}
|
||||||
from "./src/hybridSimulation";
|
from "./src/hybridSimulation";
|
||||||
|
|
||||||
|
export { getStress as calculateStress }
|
||||||
|
from "./src/stress";
|
||||||
|
|||||||
@@ -1,19 +1,20 @@
|
|||||||
/**
|
/**
|
||||||
* Calculates the stress. Basically, it computes the difference between
|
* Calculates the stress of a system by comparing the difference between the
|
||||||
* high dimensional distance and real distance. The lower the stress is,
|
* high dimensional distance and 2D distance. The lower the stress contributes
|
||||||
* the better layout.
|
* to the better layout.
|
||||||
* @return {number} - stress of the layout.
|
* @return {number} - stress of the layout.
|
||||||
*/
|
*/
|
||||||
export function getStress(nodes, distance) {
|
export function getStress(nodes, distance) {
|
||||||
let totalDiffSq = 0, totalHighDistSq = 0;
|
let sumDiffSq = 0
|
||||||
for (let j = 0; j < nodes.length; j++) {
|
let sumHighDistSq = 0;
|
||||||
|
for (let j = nodes.length-1; j >= 1; j++) {
|
||||||
for (let i = 0; i < j; i++) {
|
for (let i = 0; i < j; i++) {
|
||||||
let source = nodes[i], target = nodes[j];
|
let source = nodes[i], target = nodes[j];
|
||||||
let lowDDist = Math.hypot(target.x - source.x, target.y - source.y);
|
let lowDDist = Math.hypot(target.x - source.x, target.y - source.y);
|
||||||
let highDDist = distance(source, target);
|
let highDDist = distance(source, target);
|
||||||
totalDiffSq += Math.pow(highDDist - lowDDist, 2);
|
sumDiffSq += Math.pow(highDDist - lowDDist, 2);
|
||||||
totalLowDDistSq += lowDDist * lowDDist;
|
sumLowDDistSq += lowDDist * lowDDist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Math.sqrt(totalDiffSq / totalLowDDistSq);
|
return Math.sqrt(sumDiffSq / sumLowDDistSq);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user