*
轻量级,零依赖相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相相生相生相生相生相生相生相生相生相相生相生相生相生相生相生相生相生相生相.
平滑曲线通过您的数据点——不需要博士学位.
为什么?
永远需要:
- 平滑出传感器数据 没有失去趋势?
- 键框之间的动画 自然运动?
- 填补时间序列中的空白 数据智能化?
- 创建平滑图表 从零散的数据点?
- 插入颜色、位置或任何数值 顺利吗?
线性插值看起来是机器人 单体内插会野生地振荡(龙格现象) (中文(简体) ). Splines)给你两个世界中最好的 ——实际通过您数据点的平滑曲线.
本库为您提供 ~3KB( 最小化) 中可制取的 Spline 插值, 包括:
- ✅ 零依赖关系 - 只是数学
- ✅ 类型脚本第一 ——全型安全
- ✅ 两个算法 ——选择正确的数据工具
- ✅ 树能震动 - 只捆绑你用的东西
- ✅ 测试良好 综合测试套房
* 安装
npm install @adaskothebeast/splines
yarn add @adaskothebeast/splines
pnpm add @adaskothebeast/splines
QQ 快速启动
import { MonotoneCubicHermitInterpolation } from '@adaskothebeast/splines';
// Your data points (x, y pairs)
const data = [
[0, 0],
[1, 2],
[2, 3],
[3, 5],
[4, 4],
];
// Create the spline
const spline = new MonotoneCubicHermitInterpolation(data);
// Interpolate at any x value
spline.interpolate(1.5); // → ~2.5 (smooth value between points)
spline.interpolate(2.7); // → ~4.1
计算
Monotone Cubic 赫米特人国际化
最佳服务: 永远不应超标的数据(传感器读取、百分比、概率)
import { MonotoneCubicHermitInterpolation } from '@adaskothebeast/splines';
const spline = new MonotoneCubicHermitInterpolation([
[0, 0],
[1, 100],
[2, 100], // flat section
[3, 50],
]);
// Guarantees: if your data goes 0 → 100, interpolated values stay in [0, 100]
// No overshooting, no undershooting
为什么用它?
- 保守单调——如果数据上升,插值上升
- 没有振荡和振动的文物
- 适合物理限制的数据
自然立方体 Spline
最佳服务: 平滑曲线,其中过度射击是可以接受的(动画,可视化)
import { NaturalSpline } from '@adaskothebeast/splines';
const spline = new NaturalSpline([
[0, 0],
[1, 2],
[2, 0],
[3, 2],
]);
// Produces the smoothest possible curve (minimizes curvature)
// May overshoot at peaks/valleys — that's often desirable for animation
// Note: values outside the data range are clamped to the nearest endpoint
为什么用它?
- 最大平滑——连续的第一和第二个衍生工具
- 可视化的自然外观曲线
- CAD 和图形中使用的经典算法
• API
输入格式
两种算法都接受数据为tuple或 NumberPair 对象 :
// Tuple format (recommended)
const data: [number, number][] = [[0, 0], [1, 2], [2, 4]];
// Object format
import { NumberPair } from '@adaskothebeast/splines';
const data = [new NumberPair(0, 0), new NumberPair(1, 2), new NumberPair(2, 4)];
方法
const spline = new MonotoneCubicHermitInterpolation(data);
// Interpolate at any x value
const y = spline.interpolate(x);
选项
两种算法都接受可选 options 参数 :
type OutOfRangeStrategy = 'clamp' | 'extrapolate' | 'throw';
type SplineOptions = {
outOfRange?: OutOfRangeStrategy;
};
import { NaturalSpline } from '@adaskothebeast/splines';
const spline = new NaturalSpline(data, { outOfRange: 'extrapolate' });
注释:
- 数据由 x 值自动排序
MonotoneCubicHermiteInterpolation默认为outOfRange: 'extrapolate'NaturalSpline默认为outOfRange: 'clamp'- 若为
outOfRange: 'throw', (中文(简体) ),interpolate(x)丢出一个RangeError用于数据范围以外的x NaturalSpline通过线性插值支持2分( 3+ 点使用天然立方丝线)- 空数组会出错
- X 值必须是有限度的,在排序后必须严格增加(不支持重复的 x 值)
输入验证出错( E)
构造器验证前面的输入点并扔出一个 SplineInputError 带有 SplineErrorCode 当提供无效数据时:
import {
NaturalSpline,
SplineErrorCode,
SplineInputError,
validateInput,
} from '@adaskothebeast/splines';
try {
new NaturalSpline([
[1, 10],
[1, 20], // duplicate x
]);
} catch (err) {
if (err instanceof SplineInputError) {
console.error(err.code); // SplineErrorCode.NonIncreasingX ('NON_INCREASING_X')
}
}
错误代码( E)SplineErrorCode(中文(简体) ) | 数值 | 条件 |
|---|---|---|
EmptyInput | 'EMPTY_INPUT' | 输入数组为空 |
InvalidFormat | 'INVALID_FORMAT' | 输入不是一个 [number, number] 二联或 { x, y } 双对 |
NonFinite | 'NON_FINITE' | 任意 x 或者说 y 值为 NaN, (中文(简体) ), Infinity,或 -Infinity |
NonIncreasingX | 'NON_INCREASING_X' | 复制 x 排序后值存在 |
v6.0.0中的新内容
- 可配置的外出行为( )
SplineOptions(中文(简体) ): 传出{ outOfRange: 'clamp' | 'extrapolate' | 'throw' }改为NaturalSpline(违约)'clamp')或(或)MonotoneCubicHermiteInterpolation(违约)'extrapolate') (中文(简体) ). - 严格输入验证( E)
SplineInputError打开SplineErrorCode(中文(简体) ): 构造器和导出的validateInput编号 :normalizeAndValidate帮助者验证输入是非空的、有限度的,并严格增加了x排序后值, 扔出类型SplineInputError实例. NaturalSpline精确和 2- 点支持数字 :- 固定的三相解析器边界处理,所以所有相线段都精确地通过每个结点.
- 构建时添加直接线性插值支持
NaturalSpline和两分.
- 业绩优化数字 :
- 将内部数字数组替换为
Float64Array示线系数的缓冲. - 添加一个快速路径, 当输入点已被命令时跳过索引排序
x。 。 。 .
- 将内部数字数组替换为
- 子路径导出 & 现代工具链数字 :
- 从两者中导出验证和选项类型/帮助
@adaskothebeast/splines和亚路径入站点(@adaskothebeast/splines/NaturalSpline, (中文(简体) ),@adaskothebeast/splines/MonotoneCubicHermiteInterpolation) (中文(简体) ). - 将构建和测试工具链升级到Nx 24, TypeScript 6和Node 26.
- 从两者中导出验证和选项类型/帮助
我该用哪种算法?
| 假设 | 建议 |
|---|---|
| 传感器数据(温度、压力) | 木乃伊立方体赫米特语 |
| 进度栏,百分比 | 木乃伊立方体赫米特语 |
| 动画键框 | 天然螺纹 |
| 平滑图 | 要么,要么,要么,要么,要么 |
| 财务数据 | 木乃伊立方体赫米特语 |
| 类似贝塞尔的曲线 | 天然螺纹 |
拇指规则: 如果过度射击会是一个错误,使用莫诺内. 如果过度射击增加了自然运动,则使用自然运动.
示例:平滑有声信号
import { MonotoneCubicHermitInterpolation } from '@adaskothebeast/splines';
// Sparse measurements
const measurements = [
[0, 20.1],
[5, 22.3],
[10, 25.8],
[15, 24.2],
[20, 26.5],
];
const spline = new MonotoneCubicHermitInterpolation(measurements);
// Generate smooth curve with 100 points
const smoothCurve = [];
for (let x = 0; x <= 20; x += 0.2) {
smoothCurve.push({ x, y: spline.interpolate(x) });
}
例:热力学——蒸发压到温度
倒置蒸汽台数据以从饱和蒸汽压得到温度.
标准表格给你 T → P但有时你需要 P → T。 。 。 .
import { MonotoneCubicHermitInterpolation } from '@adaskothebeast/splines';
// Water saturation vapor pressure data (from steam tables)
// Format: [Pressure in kPa, Temperature in °C]
const vaporPressureToTemp = [
[0.6113, 0], // Triple point
[1.2281, 10],
[2.3392, 20],
[4.2470, 30],
[7.3849, 40],
[12.352, 50],
[19.946, 60],
[31.201, 70],
[47.414, 80],
[70.182, 90],
[101.33, 100], // Boiling point at 1 atm
[143.38, 110],
[198.67, 120],
];
// Create inverse lookup: Pressure → Temperature
const pressureToTempSpline = new MonotoneCubicHermitInterpolation(vaporPressureToTemp);
// Now you can find saturation temperature for any pressure
pressureToTempSpline.interpolate(101.33); // → 100°C (boiling point at 1 atm)
pressureToTempSpline.interpolate(50); // → ~81.3°C
pressureToTempSpline.interpolate(20); // → ~60.1°C
// Useful for:
// - HVAC calculations
// - Psychrometric charts
// - Boiler/steam system design
// - Dew point calculations
为什么这里是莫诺恩立方体赫米特? 活体压与温度严格为单调相通——使用单调相通插能保证物理上有意义的结果(没有负压,没有温度倒置).
许可证
AGPL-3.0- 或- 后期 亚当·"Adasko The BeAsT" 普鲁辛斯基
捐款
问题和公关欢迎! 这是一个小型的,专注的图书馆——让我们保持这样.
git clone https://github.com/AdaskoTheBeAsT/Splines-TypeScript.git
cd Splines-TypeScript
yarn install
yarn test