# @adaskothebeast/splines 中文文档

规范地址：https://npmcn.com/package/%40adaskothebeast/splines/

版本：6\.0\.0
npm 元数据更新时间：2026-11-01T16:04:30.247Z
中文译文更新时间：2026-11-06T05:16:53.362Z
软件包许可：AGPL-3\.0-or-later

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40adaskothebeast/splines
原文对照：https://npmcn.com/package/%40adaskothebeast/splines/?lang=original
项目主页：https://github.com/AdaskoTheBeAsT/Splines-TypeScript
源码仓库：https://github.com/AdaskoTheBeAsT/Splines-TypeScript

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：94.3 kB，31 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2022-05-22T13:32:40.357Z
- 最新版本发布：2026-11-01T16:04:29.914Z
- 近 30 天下载量：4888（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：19（近 12 个月 2 个）

## 中文简介

类型脚本的 Spline 插值

## 安装

```sh
npm install @adaskothebeast/splines
```

也可以使用：`pnpm add @adaskothebeast/splines`、`yarn add @adaskothebeast/splines`、`bun add @adaskothebeast/splines`

## README

# \*

[![npm version](https://img.shields.io/npm/v/@adaskothebeast/splines.svg)](https://www.npmjs.com/package/@adaskothebeast/splines) ![NPM Downloads @adaskothebeast/splines](https://img.shields.io/npm/dt/%40adaskothebeast%2Fsplines?label=splines) [![License: AGPL v3](https://img.shields.io/badge/License-AGPL_v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0) [![TypeScript](https://img.shields.io/badge/TypeScript-6.0+-blue.svg)](https://www.typescriptlang.org/) [![CodeFactor](https://www.codefactor.io/repository/github/adaskothebeast/splines-typescript/badge)](https://www.codefactor.io/repository/github/adaskothebeast/splines-typescript) [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/AdaskoTheBeAsT/Splines-TypeScript/ci.yml)](https://github.com/AdaskoTheBeAsT/Splines-TypeScript/actions/workflows/ci.yml) ![Sonar Tests](https://img.shields.io/sonar/tests/AdaskoTheBeAsT_Splines-TypeScript?server=https%3A%2F%2Fsonarcloud.io) ![Sonar Coverage](https://img.shields.io/sonar/coverage/AdaskoTheBeAsT_Splines-TypeScript?server=https%3A%2F%2Fsonarcloud.io) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=alert_status)](https://sonarcloud.io/dashboard?id=AdaskoTheBeAsT_Splines-TypeScript) [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=bugs)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=coverage)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript) [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=AdaskoTheBeAsT_Splines-TypeScript&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=AdaskoTheBeAsT_Splines-TypeScript)

**轻量级,零依赖相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相生相相生相生相生相生相生相生相生相生相相生相生相生相生相生相生相生相生相生相.**

平滑曲线通过您的数据点——不需要博士学位.

* * *

## 为什么?

永远需要:

-   **平滑出传感器数据** 没有失去趋势?
-   **键框之间的动画** 自然运动?
-   **填补时间序列中的空白** 数据智能化?
-   **创建平滑图表** 从零散的数据点?
-   **插入颜色、位置或任何数值** 顺利吗?

线性插值看起来是机器人 单体内插会野生地振荡([龙格现象](https://en.wikipedia.org/wiki/Runge%27s_phenomenon)) (中文(简体) ). **Splines)给你两个世界中最好的** ——实际通过您数据点的平滑曲线.

本库为您提供 ~3KB( 最小化) 中可制取的 Spline 插值, 包括:

-   ✅ **零依赖关系** - 只是数学
-   ✅ **类型脚本第一** ——全型安全
-   ✅ **两个算法** ——选择正确的数据工具
-   ✅ **树能震动** - 只捆绑你用的东西
-   ✅ **测试良好** 综合测试套房

* * *

## \* 安装

```bash
npm install @adaskothebeast/splines
```

```bash
yarn add @adaskothebeast/splines
```

```bash
pnpm add @adaskothebeast/splines
```

* * *

## QQ 快速启动

```typescript
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 赫米特人国际化

**最佳服务:** 永远不应超标的数据(传感器读取、百分比、概率)

```typescript
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
```

**为什么用它?**

-   保守单调——如果数据上升,插值上升
-   没有振荡和振动的文物
-   适合物理限制的数据

📚 [维基百科:莫诺通立方插图](https://en.wikipedia.org/wiki/Monotone_cubic_interpolation)

* * *

### 自然立方体 Spline

**最佳服务:** 平滑曲线,其中过度射击是可以接受的(动画,可视化)

```typescript
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 和图形中使用的经典算法

📚 [学习更多: 自然立方体 Spline](https://towardsdatascience.com/numerical-interpolation-natural-cubic-spline-52c1157b98ac)

* * *

## • API

### 输入格式

两种算法都接受数据为tuple或 `NumberPair` 对象 :

```typescript
// 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)];
```

### 方法

```typescript
const spline = new MonotoneCubicHermitInterpolation(data);

// Interpolate at any x value
const y = spline.interpolate(x);
```

### 选项

两种算法都接受可选 `options` 参数 :

```ts
type OutOfRangeStrategy = 'clamp' | 'extrapolate' | 'throw';

type SplineOptions = {
  outOfRange?: OutOfRangeStrategy;
};
```

```ts
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` 当提供无效数据时:

```ts
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.

* * *

## 我该用哪种算法?

| 假设 | 建议 |
| --- | --- |
| 传感器数据(温度、压力) | 木乃伊立方体赫米特语 |
| 进度栏,百分比 | 木乃伊立方体赫米特语 |
| 动画键框 | 天然螺纹 |
| 平滑图 | 要么,要么,要么,要么,要么 |
| 财务数据 | 木乃伊立方体赫米特语 |
| 类似贝塞尔的曲线 | 天然螺纹 |

**拇指规则:** 如果过度射击会是一个错误,使用莫诺内. 如果过度射击增加了自然运动,则使用自然运动.

* * *

## 示例:平滑有声信号

```typescript
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`。 。 。 .

```typescript
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- 或- 后期](https://npmcn.com/package/%40adaskothebeast/splines/LICENSE) 亚当·"Adasko The BeAsT" 普鲁辛斯基

* * *

## 捐款

问题和公关欢迎! 这是一个小型的,专注的图书馆——让我们保持这样.

```bash
git clone https://github.com/AdaskoTheBeAsT/Splines-TypeScript.git
cd Splines-TypeScript
yarn install
yarn test
```

## 常见问题

### @adaskothebeast/splines 是什么？

类型脚本的 Spline 插值

### 如何安装 @adaskothebeast/splines？

在项目目录运行 npm install @adaskothebeast/splines，或使用 pnpm add / yarn add / bun add。

### @adaskothebeast/splines 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @adaskothebeast/splines 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @adaskothebeast/splines 可以商用吗？

@adaskothebeast/splines 使用 AGPL-3\.0-or-later 许可。最严格的 Copyleft：除分发外，通过网络向用户提供基于它的服务也需要开源对应源码。闭源 SaaS 使用前务必评估。以上为概括，不构成法律意见，请以许可原文为准。

### @adaskothebeast/splines 的下载量有多少？

根据 npm 官方下载统计，@adaskothebeast/splines 在 2026/10/05 – 2026/11/03 这 30 天内被下载 4,888 次，日均约 163 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @adaskothebeast/splines 还在维护吗？

最新版本 6\.0\.0 发布于 2026/11/02（6 天前）；至今共发布 19 个版本，其中过去 12 个月发布了 2 个；首次发布于 2022/05/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

