UNPKG

@adaskothebeast/splines

Version:
109 lines (104 loc) 3.8 kB
'use strict'; /** * Copyright (C) 2022 Adam Pluciński * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ class NumberPair { constructor(x, y) { this.x = x; this.y = y; } } function isNumberPairArray(v) { var _a; return ((_a = v[0]) === null || _a === void 0 ? void 0 : _a.x) !== undefined; } function isNumberTupleArray(v) { return v[0][0] !== undefined; } /** * Copyright (C) 2022 Adam Pluciński * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ const sortNumberPairIndexes = values => { const indexes = []; for (let i = 0; i < values.length; i++) { indexes.push(i); } indexes.sort((a, b) => values[a].x < values[b].x ? -1 : 1); return indexes; }; const sortNumberTupleIndexes = values => { const indexes = []; for (let i = 0; i < values.length; i++) { indexes.push(i); } indexes.sort((a, b) => values[a][0] < values[b][0] ? -1 : 1); return indexes; }; /** * Copyright (C) 2022 Adam Pluciński * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ class SplineBase { constructor(values) { this.xs = []; this.ys = []; // Rearrange xs and ys so that xs is sorted if (isNumberPairArray(values)) { const indexes = sortNumberPairIndexes(values); for (let i = 0; i < values.length; i++) { this.xs.push(values[indexes[i]].x); this.ys.push(values[indexes[i]].y); } } else if (isNumberTupleArray(values)) { const indexes = sortNumberTupleIndexes(values); for (let i = 0; i < values.length; i++) { this.xs.push(values[indexes[i]][0]); this.ys.push(values[indexes[i]][1]); } } } } exports.NumberPair = NumberPair; exports.SplineBase = SplineBase; exports.isNumberPairArray = isNumberPairArray; exports.isNumberTupleArray = isNumberTupleArray; exports.sortNumberPairIndexes = sortNumberPairIndexes; exports.sortNumberTupleIndexes = sortNumberTupleIndexes;