แก้ coding style ภาค 2
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import constant from "./constant";
|
||||
import jiggle from "./jiggle";
|
||||
import constant from './constant';
|
||||
import jiggle from './jiggle';
|
||||
/**
|
||||
* An implementation of Chalmers' 1996 Neighbour and Sampling algorithm.
|
||||
* It uses random sampling to find the most suited neighbours from the
|
||||
* data set.
|
||||
*/
|
||||
|
||||
function sortDistances(a, b) {
|
||||
function sortDistances (a, b) {
|
||||
return b[1] - a[1];
|
||||
}
|
||||
|
||||
@@ -25,18 +25,18 @@ export default function () {
|
||||
* Apply spring forces at each simulation iteration.
|
||||
* @param {number} alpha - multiplier for amount of force applied
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = n-1, node, samples; i>=0; i--) {
|
||||
for (let i = n - 1, node, samples; i >= 0; i--) {
|
||||
node = nodes[i];
|
||||
samples = createRandomSamples(i);
|
||||
|
||||
@@ -52,16 +52,16 @@ export default function () {
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ export default function () {
|
||||
* @param {number} dist - high dimensional distance between the two nodes
|
||||
* @param {number} alpha - multiplier for the amount of force applied
|
||||
*/
|
||||
function setVelocity(source, target, dist, alpha) {
|
||||
function setVelocity (source, target, dist, alpha) {
|
||||
let x, y, l;
|
||||
// jiggle so l won't be zero and divide by zero error after this
|
||||
x = target.x + target.vx - source.x - source.vx || jiggle();
|
||||
@@ -90,11 +90,11 @@ export default function () {
|
||||
}
|
||||
|
||||
// Called on nodes change and added to a simulation
|
||||
function initialize() {
|
||||
function initialize () {
|
||||
if (!nodes) return;
|
||||
|
||||
// Initialize for each node some random neighbours.
|
||||
for (let i = nodes.length-1; i>=0; i--) {
|
||||
for (let i = nodes.length - 1; i >= 0; i--) {
|
||||
let neighbs = pickRandomNodesFor(i, [i], neighbourSize);
|
||||
// Sort the neighbour set by the distances.
|
||||
neighbours[i] = new Map(neighbs.sort(sortDistances));
|
||||
@@ -103,8 +103,8 @@ export default function () {
|
||||
initDataSizeFactor();
|
||||
}
|
||||
|
||||
function initDataSizeFactor(){
|
||||
dataSizeFactor = 0.5/(neighbourSize+sampleSize);
|
||||
function initDataSizeFactor () {
|
||||
dataSizeFactor = 0.5 / (neighbourSize + sampleSize);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,7 +116,7 @@ export default function () {
|
||||
* @param {number} size - max number of elements in the map to return.
|
||||
* @return {array}
|
||||
*/
|
||||
function pickRandomNodesFor(index, exclude, size) {
|
||||
function pickRandomNodesFor (index, exclude, size) {
|
||||
let randElements = [];
|
||||
let max = nodes.length;
|
||||
|
||||
@@ -133,7 +133,7 @@ export default function () {
|
||||
}
|
||||
randElements.push(rand);
|
||||
}
|
||||
for(let i=randElements.length-1, rand; i>=0; i--){
|
||||
for (let i = randElements.length - 1, rand; i >= 0; i--) {
|
||||
rand = randElements[i];
|
||||
randElements[i] = [rand, distance(nodes[index], nodes[rand])];
|
||||
}
|
||||
@@ -146,7 +146,7 @@ export default function () {
|
||||
* @param {number} index - index of the node to generate sample for
|
||||
* @return {map}
|
||||
*/
|
||||
function createRandomSamples(index) {
|
||||
function createRandomSamples (index) {
|
||||
// Ignore the current neighbours of the node and itself.
|
||||
let exclude = [index];
|
||||
exclude = exclude.concat(Array.from(neighbours[index].keys()));
|
||||
@@ -160,13 +160,12 @@ export default function () {
|
||||
* @param {map} samples - map of samples
|
||||
* @return {map} - new map of neighbours
|
||||
*/
|
||||
function findNewNeighbours(neighbours, samples) {
|
||||
function findNewNeighbours (neighbours, samples) {
|
||||
let combined = [...neighbours.entries()].concat([...samples.entries()]);
|
||||
combined = combined.sort(sortDistances);
|
||||
return new Map(combined.slice(0, neighbourSize));
|
||||
}
|
||||
|
||||
|
||||
// API for initializing the algorithm and setting parameters
|
||||
force.initialize = function (_) {
|
||||
nodes = _;
|
||||
@@ -186,7 +185,7 @@ export default function () {
|
||||
};
|
||||
|
||||
force.distance = function (_) {
|
||||
return arguments.length ? (distance = typeof _ === "function" ? _ : constant(+_), force) : distance;
|
||||
return arguments.length ? (distance = typeof _ === 'function' ? _ : constant(+_), force) : distance;
|
||||
};
|
||||
|
||||
force.latestAccel = function () {
|
||||
|
||||
Reference in New Issue
Block a user