99 lines
3.3 KiB
TypeScript
99 lines
3.3 KiB
TypeScript
![]() |
/*** 数学计算工具函数
|
||
|
*/
|
||
|
|
||
|
/*** 将数值限制在指定范围内
|
||
|
* @paramvalue 要限制的值
|
||
|
* @parammin 最小值
|
||
|
* @parammax 最大值
|
||
|
*/export const clamp = (value: number, min: number, max: number): number => {
|
||
|
return Math.min(Math.max(value, min), max);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 将数值四舍五入到指定小数位
|
||
|
* @param value要四舍五入的值* @param decimals 保留的小数位数
|
||
|
*/export const round = (value: number, decimals: number = 0): number => {
|
||
|
const multiplier = Math.pow(10, decimals);
|
||
|
return Math.round(value * multiplier) / multiplier;
|
||
|
};/**
|
||
|
* 生成指定范围内的随机数* @param min 最小值* @param max 最大值* @param isInteger 是否为整数,默认false
|
||
|
*/
|
||
|
export const random = (min: number, max: number, isInteger: boolean = false): number => {
|
||
|
const value = Math.random() * (max - min) + min;
|
||
|
return isInteger ? Math.floor(value) : value;
|
||
|
};
|
||
|
|
||
|
/*** 计算数组的平均值* @param numbers 数字数组
|
||
|
*/export const average = (numbers: number[]): number => {
|
||
|
if (numbers.length === 0) return 0;
|
||
|
return numbers.reduce((sum, num) => sum + num, 0) / numbers.length;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 计算数组的总和
|
||
|
* @param numbers 数字数组
|
||
|
*/export const sum = (numbers: number[]): number => {
|
||
|
return numbers.reduce((sum, num) => sum + num, 0);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
*角度转弧度
|
||
|
* @paramdegrees 角度
|
||
|
*/
|
||
|
export const degreesToRadians = (degrees: number): number => {
|
||
|
return degrees * (Math.PI / 180);
|
||
|
};/**
|
||
|
* 弧度转角度
|
||
|
* @param radians弧度
|
||
|
*/
|
||
|
export const radiansToDegrees = (radians: number): number => {
|
||
|
return radians * (180 / Math.PI);
|
||
|
};/**
|
||
|
* 格式化数字为千分位表示
|
||
|
* @param value 要格式化的数值*/
|
||
|
export const formatThousands = (value: number): string => {
|
||
|
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
||
|
};
|
||
|
|
||
|
/*** 计算百分比* @param value 当前值
|
||
|
*@param total 总值
|
||
|
*@param decimals 保留小数位数
|
||
|
*/
|
||
|
export const percentage = (value: number, total: number, decimals: number = 2): number => {
|
||
|
if (total === 0) return 0; return round((value / total) * 100, decimals);
|
||
|
};
|
||
|
|
||
|
/*** 判断两个数值是否近似相等
|
||
|
* @param a第一个数值
|
||
|
* @paramb 第二个数值
|
||
|
*@param epsilon 误差范围,默认0.000001
|
||
|
*/
|
||
|
export const approximatelyEqual = (a: number, b: number, epsilon: number = 0.000001): boolean => {
|
||
|
return Math.abs(a - b) < epsilon;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 将数值转换为带单位的字符串(K, M, B等)
|
||
|
* @param value 要转换的数值
|
||
|
*/
|
||
|
export const formatUnit = (value: number): string => {
|
||
|
const units = ["", "K", "M", "B", "T"];
|
||
|
const order = Math.floor(Math.log10(Math.abs(value)) / 3); if (order < 0) return value.toString();
|
||
|
const unit = units[order]; const scaled = value / Math.pow(10, order * 3);
|
||
|
return `${round(scaled, 1)}${unit}`;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 计算两点之间的距离* @param x1 第一个点的x坐标
|
||
|
*@param y1 第一个点的y坐标
|
||
|
* @param x2 第二个点的x坐标
|
||
|
* @param y2 第二个点的y坐标*/
|
||
|
export const distance = (x1: number, y1: number, x2: number, y2: number): number => {
|
||
|
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
|
||
|
};/**
|
||
|
* 线性插值* @param start 起始值
|
||
|
* @param end 结束值
|
||
|
* @param t 插值系数(0-1)*/
|
||
|
export const lerp = (start: number, end: number, t: number): number => {
|
||
|
return start + (end - start) * clamp(t, 0, 1);
|
||
|
};
|