partey_workadventure/front/src/Utils/MathUtils.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-12-01 14:48:14 +01:00
export class MathUtils {
/**
2021-12-03 10:11:16 +01:00
*
2021-12-01 14:48:14 +01:00
* @param p Position to check.
* @param r Rectangle to check the overlap against.
* @returns true is overlapping
*/
public static isOverlappingWithRectangle(
2021-12-03 10:11:16 +01:00
p: { x: number; y: number },
r: { x: number; y: number; width: number; height: number }
2021-12-01 14:48:14 +01:00
): boolean {
2021-12-03 10:11:16 +01:00
return this.isBetween(p.x, r.x, r.x + r.width) && this.isBetween(p.y, r.y, r.y + r.height);
2021-12-01 14:48:14 +01:00
}
/**
2021-12-03 10:11:16 +01:00
*
2021-12-01 14:48:14 +01:00
* @param value Value to check
* @param min inclusive min value
* @param max inclusive max value
* @returns true if value is in <min, max>
*/
public static isBetween(value: number, min: number, max: number): boolean {
2021-12-03 10:11:16 +01:00
return value >= min && value <= max;
2021-12-01 14:48:14 +01:00
}
2022-01-18 12:33:46 +01:00
public static distanceBetween(
p1: { x: number; y: number },
p2: { x: number; y: number },
squared: boolean = true
): number {
const distance = Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
return squared ? Math.sqrt(distance) : distance;
}
public static randomFromArray<T>(array: T[]): T {
return array[Math.floor(Math.random() * array.length)];
}
2021-12-03 10:11:16 +01:00
}