Files
gornodox.dev/src/lib/vmath.ts
T
2026-03-27 01:43:35 +01:00

40 lines
1.4 KiB
TypeScript

import Vector2 from "./vector";
class VMath {
public static toRadians(angle: number): number {
return angle * (Math.PI / 180);
}
public static rotate(vec: Vector2, angle: number, origin: Vector2): void {
const x: number = vec.x - origin.x;
const y: number = vec.y - origin.y;
const cos: number = Math.cos(this.toRadians(angle));
const sin: number = Math.sin(this.toRadians(angle));
let xPrim: number = (x * cos) - (y * sin);
let yPrim: number = (x * sin) + (y * cos);
xPrim += origin.x;
yPrim += origin.y;
vec.set(xPrim, yPrim);
}
public static compare(x: number, y: number): boolean;
public static compare(vec1: Vector2, vec2: Vector2): boolean;
public static compare(arg1: Vector2 | number, arg2: Vector2 | number): boolean {
if(typeof arg1 === 'number' && typeof arg2 === 'number')
return Math.abs(arg1 - arg2) <= Number.MIN_VALUE * Math.max(1, Math.abs(arg1), Math.abs(arg2));
else if (typeof arg1 !== 'number' && typeof arg2 !== 'number')
return Math.abs(arg1.x - arg2.x) <= Number.MIN_VALUE * Math.max(1, Math.abs(arg1.x), Math.abs(arg2.x)) &&
Math.abs(arg1.y - arg2.y) <= Number.MIN_VALUE * Math.max(1, Math.abs(arg1.y), Math.abs(arg2.y))
else {
throw new Error('unexpected usage of compare method in vmath library');
}
}
}
export default VMath;