Skip to content

Home > @esengine/ecs-framework-monorepo > MathUtils

MathUtils class

数学工具函数集合

提供常用的数学运算、插值、随机数生成等实用工具函数

Signature:

typescript
export declare class MathUtils

Properties

Property

Modifiers

Type

Description

DEG_TO_RAD

static

readonly

number

角度到弧度转换系数

EPSILON

static

readonly

number

默认浮点数比较容差

GOLDEN_RATIO

static

readonly

number

黄金比例

HALF_PI

static

readonly

number

π/2

PI

static

readonly

number

圆周率

QUARTER_PI

static

readonly

number

π/4

RAD_TO_DEG

static

readonly

number

弧度到角度转换系数

TWO_PI

static

readonly

number

Methods

Method

Modifiers

Description

angleDifference(from, to)

static

计算两个角度之间的最短角度差

approximately(a, b, epsilon)

static

浮点数相等比较

arithmeticSum(first, last, count)

static

等差数列求和

average(values)

static

计算数组的平均值

catmullRom(p0, p1, p2, p3, t)

static

Catmull-Rom样条插值

clamp(value, min, max)

static

限制数值在指定范围内

clamp01(value)

static

限制数值在0到1之间

cubicBezier(p0, p1, p2, p3, t)

static

贝塞尔三次曲线

degToRad(degrees)

static

角度转弧度

factorial(n)

static

阶乘

fastInverseSqrt(value)

static

快速平方根倒数(用于归一化)

fastPow(base, exponent)

static

快速幂运算(整数指数)

fibonacci(n)

static

斐波那契数列

gcd(a, b)

static

最大公约数

geometricSum(first, ratio, count)

static

等比数列求和

inRange(value, min, max)

static

检查数值是否在指定范围内

inverseLerp(a, b, value)

static

反向线性插值(获取插值参数)

isZero(value, epsilon)

static

检查数值是否为零

lcm(a, b)

static

最小公倍数

lerp(a, b, t)

static

线性插值

lerpAngle(from, to, t)

static

角度插值(处理角度环绕)

max(values)

static

获取数组中的最大值

median(values)

static

计算数组的中位数

min(values)

static

获取数组中的最小值

noise(x, y, seed)

static

简单伪随机噪声(基于种子)

normalizeAngle(radians)

static

规范化角度到[0, 2π)范围

normalizeAngleSigned(radians)

static

规范化角度到(-π, π]范围

quadraticBezier(p0, p1, p2, t)

static

贝塞尔二次曲线

radToDeg(radians)

static

弧度转角度

random(min, max)

static

生成指定范围内的随机数

randomBoolean(probability)

static

生成随机布尔值

randomChoice(array)

static

随机选择数组中的一个元素

randomInt(min, max)

static

生成指定范围内的随机整数

randomInUnitCircle()

static

生成单位圆内的随机点

randomOnUnitCircle()

static

生成单位圆上的随机点

remap(value, inMin, inMax, outMin, outMax)

static

重映射数值从一个范围到另一个范围

sign(value)

static

获取数值的符号

smootherStep(t)

static

更平滑的阶跃函数

smoothNoise(x, y, seed)

static

平滑噪声

smoothStep(t)

static

平滑阶跃函数(Hermite插值)

toPrecision(value, precision)

static

将数值转换为指定精度

基于 MIT 许可证发布