Refactor: Extract functions
This commit is contained in:
48
src/interpolation/helpers.js
Normal file
48
src/interpolation/helpers.js
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user