2020-06-08 11:49:45 +08:00
|
|
|
class MathHelper {
|
|
|
|
|
/**
|
|
|
|
|
* 将弧度转换成角度。
|
|
|
|
|
* @param radians 用弧度表示的角
|
|
|
|
|
*/
|
2020-06-08 16:23:48 +08:00
|
|
|
public static toDegrees(radians: number){
|
2020-06-08 11:49:45 +08:00
|
|
|
return radians * 57.295779513082320876798154814105;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将角度转换为弧度
|
|
|
|
|
* @param degrees
|
|
|
|
|
*/
|
2020-06-08 16:23:48 +08:00
|
|
|
public static toRadians(degrees: number){
|
2020-06-08 11:49:45 +08:00
|
|
|
return degrees * 0.017453292519943295769236907684886;
|
|
|
|
|
}
|
2020-06-09 11:09:26 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* mapps值(在leftMin - leftMax范围内)到rightMin - rightMax范围内的值
|
|
|
|
|
* @param value
|
|
|
|
|
* @param leftMin
|
|
|
|
|
* @param leftMax
|
|
|
|
|
* @param rightMin
|
|
|
|
|
* @param rightMax
|
|
|
|
|
*/
|
|
|
|
|
public static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number){
|
|
|
|
|
return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 08:46:38 +08:00
|
|
|
public static lerp(value1: number, value2: number, amount: number){
|
|
|
|
|
return value1 + (value2 - value1) * amount;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 11:09:26 +08:00
|
|
|
public static clamp(value: number, min: number, max: number){
|
|
|
|
|
if (value < min)
|
|
|
|
|
return min;
|
|
|
|
|
|
|
|
|
|
if (value > max)
|
|
|
|
|
return max;
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2020-06-10 17:41:53 +08:00
|
|
|
|
|
|
|
|
public static minOf(a: number, b: number, c: number, d: number){
|
|
|
|
|
return Math.min(a, Math.min(b, Math.min(c, d)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static maxOf(a: number, b: number, c: number, d: number){
|
|
|
|
|
return Math.max(a, Math.max(b, Math.max(c, d)));
|
|
|
|
|
}
|
2020-06-08 11:49:45 +08:00
|
|
|
}
|