1
0
mirror of https://github.com/Pomax/BezierInfo-2.git synced 2025-08-17 22:11:38 +02:00

more than one S(t) for curve fitting

This commit is contained in:
Pomax
2018-06-22 18:55:34 -07:00
parent 15f50ae53f
commit 5047e74b4e
8 changed files with 94 additions and 41 deletions

View File

@@ -1,6 +1,8 @@
var invert = require('./matrix-invert.js');
var matrices = [];
const POLYGONAL = 'polygonal', EQUIDISTANT = 'equidistant';
var binomialCoefficients = [[1],[1,1]];
function binomial(n,k) {
@@ -108,7 +110,9 @@ function computeBasisMatrix(n) {
return M;
}
function computeTimeValues(P, n) {
var computeTimeValues = {};
computeTimeValues[POLYGONAL] = function computePolygonalTimeValues(P, n) {
n = n || P.length;
var D = [0];
for(var i = 1; i<n; i++) {
@@ -119,6 +123,10 @@ function computeTimeValues(P, n) {
return S;
}
computeTimeValues[EQUIDISTANT] = function computeEquidistantTimeValues(P, n) {
return '0'.repeat(n).split('').map((_,i) =>i/(n-1));
}
function raiseRowPower(row, i) {
return row.map(v => Math.pow(v,i));
}
@@ -150,13 +158,17 @@ function computeBestFit(P, M, S, n) {
return { x: Cx, y: Cy };
}
function fit(points) {
function fit(points, mode) {
mode = mode || 0;
console.log("mode: ", mode);
var n = points.length,
P = Array.from(points),
M = computeBasisMatrix(n),
S = computeTimeValues(P, n),
S = computeTimeValues[fit.modes[mode]](P, n),
C = computeBestFit(P, M, S, n);
return { n, P, M, S, C };
}
fit.modes = [ POLYGONAL, EQUIDISTANT];
module.exports = window.makeFit = fit;