/
Vec2.ts
84 lines (67 loc) · 1.8 KB
/
Vec2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Vector2Like } from "../types";
export class Vec2 {
x: number;
y: number;
constructor(xOrVector2Like?: number | Vector2Like, y: number = 0) {
if (typeof xOrVector2Like === "number") {
this.x = xOrVector2Like;
this.y = y;
} else {
const v = xOrVector2Like || Vec2.zero;
this.x = v.x;
this.y = v.y;
}
}
static readonly zero = { x: 0, y: 0 };
clone(): Vec2 {
return new Vec2(this.x, this.y);
}
add(other: Vector2Like): Vec2 {
this.x += other.x;
this.y += other.y;
return this;
}
sub(other: Vector2Like): Vec2 {
this.x -= other.x;
this.y -= other.y;
return this;
}
scale(value: number): Vec2 {
this.x *= value;
this.y *= value;
return this;
}
dot(other: Vector2Like): number {
return this.x * other.x + this.y * other.y;
}
cross(other: Vector2Like): number {
return this.x * other.y - this.y * other.x;
}
squaredLength(): number {
return this.x * this.x + this.y * this.y;
}
length(): number {
return Math.sqrt(this.squaredLength());
}
normalize(): Vec2 {
const len = this.length() || 1;
this.x /= len;
this.y /= len;
return this;
}
rotate(angle: number): Vec2 {
const c = Math.cos(angle);
const s = Math.sin(angle);
const x = c * this.x - s * this.y;
const y = s * this.x + c * this.y;
this.x = x;
this.y = y;
return this;
}
static dot(v1: Vector2Like, v2: Vector2Like): number {
return v1.x * v2.x + v1.y * v2.y;
}
static cross(v1: Vector2Like, v2: Vector2Like): number {
return v1.x * v2.y - v1.y * v2.x;
}
}