d3/d3-force

if you change a variable in an expression and use it twice gives undetermined result ( a = i++ + i;)

Closed this issue · 0 comments

In collide.js there are 2 lines with this issue

d3-force/src/collide.js

Lines 52 to 53 in c3e73cf

l = (r - (l = Math.sqrt(l))) / l * strength;
node.vx += (x *= l) * (r = (rj *= rj) / (ri2 + rj));

in line 52 what value of l is used in / l * strength

in line 53 what value of rj is used in ri2 + rj and it no longer is a radius it is a radius2

The compiler/interpreter is free to choose whichever sub expression to calculate first. Thus the resulting value depends on the compiler/interpreter used.

Better to rewrite the code to make it unambiguous what is calculated:

l = Math.sqrt(l);
l = (r - l) / l * strength;
rj2 = rj * rj;
node.vx += (x *= l) * (f = rj2 / (ri2 + rj2));
node.vy += (y *= l) * f;
data.vx -= x * (f = 1 - f);
data.vy -= y * f;

In the function r is used as a distance between center points. Re-using the name for a correction factor is confusing.