Files
esengine/source/src/Math/MathHelper.ts

122 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-07-23 11:00:46 +08:00
module es {
export class MathHelper {
public static readonly Epsilon: number = 0.00001;
public static readonly Rad2Deg = 57.29578;
public static readonly Deg2Rad = 0.0174532924;
/**
* pi除以2的值(1.57079637)
*/
public static readonly PiOver2 = Math.PI / 2;
2020-06-16 00:04:28 +08:00
2020-07-23 11:00:46 +08:00
/**
*
* @param radians
*/
2020-07-28 16:25:20 +08:00
public static toDegrees(radians: number) {
2020-07-23 11:00:46 +08:00
return radians * 57.295779513082320876798154814105;
}
2020-06-08 11:49:45 +08:00
2020-07-23 11:00:46 +08:00
/**
*
* @param degrees
*/
2020-07-28 16:25:20 +08:00
public static toRadians(degrees: number) {
2020-07-23 11:00:46 +08:00
return degrees * 0.017453292519943295769236907684886;
}
2020-07-23 11:00:46 +08:00
/**
* mapps值(leftMin - leftMax范围内)rightMin - rightMax范围内的值
* @param value
* @param leftMin
* @param leftMax
* @param rightMin
* @param rightMax
*/
2020-07-28 16:25:20 +08:00
public static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number) {
2020-07-23 11:00:46 +08:00
return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin);
}
2020-07-28 16:25:20 +08:00
public static lerp(value1: number, value2: number, amount: number) {
2020-07-23 11:00:46 +08:00
return value1 + (value2 - value1) * amount;
}
2020-06-15 08:46:38 +08:00
2020-07-28 16:25:20 +08:00
public static clamp(value: number, min: number, max: number) {
2020-07-23 11:00:46 +08:00
if (value < min)
return min;
2020-07-23 11:00:46 +08:00
if (value > max)
return max;
2020-07-23 11:00:46 +08:00
return value;
}
/**
* 03
* @param circleCenter
* @param radius
* @param angleInDegrees
*/
2020-07-28 16:25:20 +08:00
public static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number) {
2020-07-23 11:00:46 +08:00
let radians = MathHelper.toRadians(angleInDegrees);
return new Vector2(Math.cos(radians) * radians + circleCenter.x,
Math.sin(radians) * radians + circleCenter.y);
2020-07-23 11:00:46 +08:00
}
2020-07-23 11:00:46 +08:00
/**
* true
* @param value
*/
2020-07-28 16:25:20 +08:00
public static isEven(value: number) {
2020-07-23 11:00:46 +08:00
return value % 2 == 0;
}
2020-07-09 16:16:04 +08:00
2020-07-23 11:00:46 +08:00
/**
* 0-1
* @param value
*/
2020-07-28 16:25:20 +08:00
public static clamp01(value: number) {
2020-07-23 11:00:46 +08:00
if (value < 0)
return 0;
2020-07-17 11:07:57 +08:00
2020-07-23 11:00:46 +08:00
if (value > 1)
return 1;
2020-07-17 11:07:57 +08:00
2020-07-23 11:00:46 +08:00
return value;
}
2020-07-17 11:07:57 +08:00
2020-07-28 16:25:20 +08:00
public static angleBetweenVectors(from: Vector2, to: Vector2) {
2020-07-23 11:00:46 +08:00
return Math.atan2(to.y - from.y, to.x - from.x);
}
2020-08-12 12:16:35 +08:00
public static angleToVector(angleRadians: number, length: number){
return new Vector2(Math.cos(angleRadians) * length, Math.sin(angleRadians) * length);
}
2020-08-12 12:16:35 +08:00
/**
* t并确保它总是大于或等于0并且小于长度
* @param t
* @param length
*/
public static incrementWithWrap(t: number, length: number){
t ++;
if (t == length)
return 0;
return t;
}
/**
* start可以小于或大于end例如:开始是21046
* @param start
* @param end
* @param shift
*/
public static approach(start: number, end: number, shift: number): number {
if (start < end)
return Math.min(start + shift, end);
return Math.max(start - shift, end);
}
2020-07-09 16:16:04 +08:00
}
2020-07-23 11:00:46 +08:00
}