Optimize setTension() CPU usage

This commit is contained in:
Ian Gulliver
2019-07-03 20:00:05 +00:00
parent b066001423
commit 27411817b1
5 changed files with 14 additions and 9 deletions

View File

@@ -26,8 +26,9 @@ class LayoutNode {
vec[i] = aff.node.pos[i] - this.pos[i];
vecsum += Math.abs(vec[i]);
};
let distance = Math.sqrt(Math.pow(vec[0], 2) + Math.pow(vec[1], 2));
let weight = aff.distanceToWeight(distance);
// Avoid calling sqrt(), since the results are used relatively
let distanceSquared = vec[0] * vec[0] + vec[1] * vec[1];
let weight = aff.distanceToWeight(distanceSquared);
for (let i of [0, 1]) {
this.vec[i] += (weight * vec[i]) / vecsum;
}