Refactor: Extract functions

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-19 09:29:14 +00:00
parent 1d711f828d
commit feec778c62
12 changed files with 351 additions and 163 deletions

View File

@@ -0,0 +1,48 @@
export function takeSampleFrom(nodes, amount) {
let randElements = [],
max = nodes.length;
for (var i = 0; i < amount; ++i) {
var rand = nodes[Math.floor((Math.random() * max))];
// If the rand is already in random list or in exclude list
// ignore it and get a new value.
while (randElements.includes(rand)) {
rand = nodes[Math.floor((Math.random() * max))];
}
randElements.push(rand);
}
var remainder = nodes.filter(function (node) {
return !randElements.includes(node);
});
return {
sample: randElements,
remainder: remainder
};
}
// With a circle radius r, and center at (h,k),
// Find the coordinate of a point at angle degree
export function pointOnCircle(h, k, angle, r) {
let x = h + r*Math.cos(toRadians(angle));
let y = k + r*Math.sin(toRadians(angle));
return {
x: x,
y: y
};
}
function toRadians(degrees) {
return degrees * (Math.PI / 180);
}
export function sumDistError(node, samples, realDistances) {
let total = 0.0;
for (let i = 0; i < samples.length; i++) {
let sample = samples[i];
let lowDDistance = Math.hypot(sample.x - node.x, sample.y - node.y);
total += Math.abs(lowDDistance - realDistances[i]);
}
return total;
}