แก้ coding style ภาค 2

This commit is contained in:
Pitchaya Boonsarngsuk
2018-03-22 16:12:25 +00:00
parent 2256af7448
commit 0cdd927444
12 changed files with 160 additions and 130 deletions

View File

@@ -1,5 +1,5 @@
import constant from "./constant";
import jiggle from "./jiggle";
import constant from './constant';
import jiggle from './jiggle';
/**
* Modified link force algorithm
@@ -8,7 +8,7 @@ import jiggle from "./jiggle";
* - removed other unused functions
* Alpha should be constant 1 for accurate simulation
*/
export default function() {
export default function () {
var dataSizeFactor,
distance = constant(30),
distances = [],
@@ -18,60 +18,62 @@ export default function() {
latestVelocityDiff = 0,
iterations = 1;
function force(alpha) {
function force (alpha) {
let n = nodes.length;
// Cache old velocity for comparison later
if (stableVeloHandler!==null && stableVelocity>=0) {
for (let i = n-1, node; i>=0; i--) {
if (stableVeloHandler !== null && stableVelocity >= 0) {
for (let i = n - 1, node; i >= 0; i--) {
node = nodes[i];
node.oldvx = node.vx;
node.oldvy = node.vy;
}
}
// Each iteration in a tick
for (var k = 0, source, target, i, j, x, y, l; k < iterations; ++k) {
// For each link
for (i = 1; i < n; i++) for (j = 0; j < i; j++) {
for (i = 1; i < n; i++) {
for (j = 0; j < i; j++) {
// jiggle so l won't be zero and divide by zero error after this
source = nodes[i];
target = nodes[j];
x = target.x + target.vx - source.x - source.vx || jiggle();
y = target.y + target.vy - source.y - source.vy || jiggle();
l = Math.sqrt(x * x + y * y);
l = (l - distances[i*(i-1)/2+j]) / l * dataSizeFactor * alpha;
x *= l, y *= l;
target.vx -= x;
target.vy -= y;
source.vx += x;
source.vy += y;
source = nodes[i];
target = nodes[j];
x = target.x + target.vx - source.x - source.vx || jiggle();
y = target.y + target.vy - source.y - source.vy || jiggle();
l = Math.sqrt(x * x + y * y);
l = (l - distances[i * (i - 1) / 2 + j]) / l * dataSizeFactor * alpha;
x *= l, y *= l;
target.vx -= x;
target.vy -= y;
source.vx += x;
source.vy += y;
}
}
}
// Calculate velocity changes, aka force applied.
if (stableVeloHandler!==null && stableVelocity>=0) {
if (stableVeloHandler !== null && stableVelocity >= 0) {
let velocityDiff = 0;
for (let i = n-1, node; i>=0; i--) {
for (let i = n - 1, node; i >= 0; i--) {
node = nodes[i];
velocityDiff += Math.abs(Math.hypot(node.vx-node.oldvx, node.vy-node.oldvy));
velocityDiff += Math.abs(Math.hypot(node.vx - node.oldvx, node.vy - node.oldvy));
}
velocityDiff /= n;
latestVelocityDiff = velocityDiff;
if(velocityDiff<stableVelocity){
if (velocityDiff < stableVelocity) {
stableVeloHandler();
}
}
}
function initialize() {
function initialize () {
if (!nodes) return;
// 0.5 to divide the force to two part for source and target node
dataSizeFactor = 0.5/(nodes.length-1);
dataSizeFactor = 0.5 / (nodes.length - 1);
initializeDistance();
}
function initializeDistance() {
function initializeDistance () {
if (!nodes) return;
for (let i = 1, n = nodes.length; i < n; i++) {
for (let j = 0; j < i; j++) {
@@ -80,17 +82,17 @@ export default function() {
}
}
force.initialize = function(_) {
force.initialize = function (_) {
nodes = _;
initialize();
};
force.iterations = function(_) {
force.iterations = function (_) {
return arguments.length ? (iterations = +_, force) : iterations;
};
force.distance = function(_) {
return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), initializeDistance(), force) : distance;
force.distance = function (_) {
return arguments.length ? (distance = typeof _ === 'function' ? _ : constant(+_), initializeDistance(), force) : distance;
};
force.latestAccel = function () {