npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pnt2

v1.1.6

Published

A high-performance TypeScript library for working with 2D points and vectors, supporting Float32Array, Float64Array, and plain arrays.

Readme

PNT2 (二维点)

PNT2 是一个高性能的 TypeScript 库,用于处理二维点和向量。它提供了丰富的实用函数,用于执行常见的操作,例如加法、减法、缩放、归一化、距离计算等。该库支持多种数组类型(Float32ArrayFloat64Array 和普通数组),使其灵活且适合性能关键的应用。

特性

  • 灵活的数组类型:支持 Float32ArrayFloat64Array 和普通 JavaScript 数组。
  • 动态数组类型切换:允许在运行时配置底层数组类型以优化性能。
  • 丰富的功能:包括向量数学、点操作、几何计算和三角函数转换的工具。
  • TypeScript 支持:完全用 TypeScript 编写,增强类型安全性和开发者体验。
  • 轻量级:依赖最少,性能优化。

安装

安装 PNT2,运行以下命令:

npm install pnt2

或者使用 Yarn:

yarn add pnt2

使用方法

基本示例

以下是如何使用 PNT2 创建一个二维点、对其进行缩放并计算其长度的示例:

import { create, scale, length } from 'pnt2';

// 创建一个新的二维点 [1, 2]
const point = create();
point[0] = 1;
point[1] = 2;

// 将点按因子 2 缩放
scale(point, point, 2);

// 计算缩放后点的长度
const len = length(point);

console.log(`缩放后的点: [${point[0]}, ${point[1]}], 长度: ${len}`);
// 输出: 缩放后的点: [2, 4], 长度: 4.47213595499958

动态数组类型配置

可以在运行时动态切换底层数组类型:

import { setArrayType,  create } from 'pnt2';

// 将数组类型设置为 Float64Array
setArrayType(Float64Array);

// 创建一个新的二维点
const point = create();
console.log(point instanceof Float64Array); // true

向量操作

执行常见的向量操作,例如加法、减法和归一化:

import { create, add, normalize } from 'pnt2';

const vecA = create();
vecA[0] = 3;
vecA[1] = 4;

const vecB = create();
vecB[0] = 1;
vecB[1] = 2;

// 向量相加
const result = create();
add(result, vecA, vecB);
console.log(`加法结果: [${result[0]}, ${result[1]}]`);
// 输出: 加法结果: [4, 6]

// 归一化向量
normalize(result, vecA);
console.log(`归一化向量: [${result[0]}, ${result[1]}]`);
// 输出: 归一化向量: [0.6, 0.8]

距离计算

计算两点之间的距离:

import { fromValue, distance } from 'pnt2';

const pointA = fromValue(0, 0);
const pointB = fromValue(3, 4);

const dist = distance(pointA, pointB);
console.log(`距离: ${dist}`);
// 输出: 距离: 5

三角函数转换

在角度和弧度之间进行转换:

import { create, toRad, toDeg } from 'pnt2';

const angleInDegrees = create();
angleInDegrees[0] = 90;
angleInDegrees[1] = 180;

const angleInRadians = create();
toRad(angleInRadians, angleInDegrees);

console.log(`弧度表示的角度: [${angleInRadians[0]}, ${angleInRadians[1]}]`);
// 输出: 弧度表示的角度: [1.5707963267948966, 3.141592653589793]

toDeg(angleInDegrees, angleInRadians);
console.log(`角度表示的角度: [${angleInDegrees[0]}, ${angleInDegrees[1]}]`);
// 输出: 角度表示的角度: [90, 180]

API 参考

核心函数

  • create(): ArrayType - 创建一个长度为 2 的新二维数组。
  • clone(a: ArrayType): ArrayType - 克隆现有数组。
  • fromValue(x: number, y: number): ArrayType - 从两个值创建一个新数组。
  • copy(out: ArrayType, a: ArrayType): ArrayType - 将一个数组的值复制到另一个数组。
  • set(out: ArrayType, x: number, y: number): ArrayType - 设置数组的值。

数学运算

  • add(out: ArrayType, a: ArrayType, b: ArrayType): ArrayType - 按元素将两个数组相加。
  • subtract(out: ArrayType, a: ArrayType, b: ArrayType): ArrayType - 按元素将两个数组相减。
  • multiply(out: ArrayType, a: ArrayType, b: ArrayType): ArrayType - 按元素将两个数组相乘。
  • divide(out: ArrayType, a: ArrayType, b: ArrayType): ArrayType - 按元素将两个数组相除。
  • scale(out: ArrayType, a: ArrayType, b: number): ArrayType - 按标量缩放数组。
  • equals(a: ArrayType, b: ArrayType): boolean - 检查两个数组是否相等。

几何计算

  • distance(a: ArrayType, b: ArrayType): number - 计算两点之间的欧几里得距离。
  • squaredDistance(a: ArrayType, b: ArrayType): number - 计算两点之间的平方距离。
  • length(a: ArrayType): number - 计算向量的长度。
  • squaredLength(a: ArrayType): number - 计算向量的平方长度。
  • normalize(out: ArrayType, a: ArrayType): ArrayType - 将向量归一化为单位长度。
  • dot(a: ArrayType, b: ArrayType): number - 计算两个向量的点积。
  • cross(out: any, a: ArrayType, b: ArrayType): ArrayType - 计算两个二维向量的叉积。

工具函数

  • setArrayType(type: any) - 动态设置底层数组类型。
  • zero(out: ArrayType): ArrayType - 将数组的所有元素设置为零。
  • toRad(out: ArrayType, a: ArrayType): ArrayType - 将角度从度数转换为弧度。
  • toDeg(out: ArrayType, a: ArrayType): ArrayType - 将角度从弧度转换为度数。

别名

为了方便,提供以下别名:

  • sub 对应 subtract
  • mul 对应 multiply
  • div 对应 divide
  • len 对应 length
  • dist 对应 distance

性能优化

  • 缓存机制:通过缓存中间结果减少冗余计算。
  • 运行时灵活性:根据性能需求,在 Float32ArrayFloat64Array 和普通数组之间切换。
  • 高效的数学运算:优化了向量和点操作的最小开销。

贡献

欢迎贡献!如果您发现问题或有改进建议,请提交问题或拉取请求。

许可证

MIT