forked from StrandedKitty/streets-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec2.ts
113 lines (87 loc) · 2.31 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import Mat3 from "./Mat3";
export default class Vec2 {
public x: number;
public y: number;
public constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
public set(x: number, y: number): void {
this.x = x;
this.y = y;
}
public equals(v: Vec2): boolean {
return this.x === v.x && this.y === v.y;
}
public static add(a: Vec2, b: Vec2): Vec2 {
return new Vec2(a.x + b.x, a.y + b.y);
}
public static sub(a: Vec2, b: Vec2): Vec2 {
return new Vec2(a.x - b.x, a.y - b.y);
}
public static addScalar(v: Vec2, s: number): Vec2 {
return new Vec2(v.x + s, v.y + s);
}
public static multiplyScalar(v: Vec2, s: number): Vec2 {
return new this(v.x * s, v.y * s);
}
public static normalize(v: Vec2): Vec2 {
const dst = new this;
const length = Math.sqrt(v.x ** 2 + v.y ** 2);
if (length > 0.00001) {
dst.x = v.x / length;
dst.y = v.y / length;
}
return dst;
}
public static getLength(v: Vec2): number {
return Math.sqrt(v.x ** 2 + v.y ** 2);
}
public static dot(a: Vec2, b: Vec2): number {
const num = (a.x * b.x) + (a.y * b.y);
return num <= -1 ? -1 : num >= 1 ? 1 : num;
}
public static distance(a: Vec2, b: Vec2): number {
return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
}
public static angleClockwise(a: Vec2, b: Vec2): number {
const dot = a.x * b.x + a.y * b.y;
const det = a.x * b.y - a.y * b.x;
return Math.atan2(det, dot);
}
public getAngle(): number {
return Math.atan2(this.y, this.x);
}
public static rotate(v: Vec2, angle: number): Vec2 {
return new Vec2(
v.x * Math.cos(angle) - v.y * Math.sin(angle),
v.x * Math.sin(angle) + v.y * Math.cos(angle)
);
}
public static rotateLeft(v: Vec2): Vec2 {
return new Vec2(-v.y, v.x);
}
public static rotateRight(v: Vec2): Vec2 {
return new Vec2(v.y, -v.x);
}
public static applyMatrix3(v: Vec2, mat: Mat3): Vec2 {
const dst = new Vec2();
const m = mat.values;
const x = v.x, y = v.y;
dst.x = m[0] * x + m[3] * y + m[6];
dst.y = m[1] * x + m[4] * y + m[7];
return dst;
}
public static lerp(v1: Vec2, v2: Vec2, amount: number): Vec2 {
return new Vec2(
(1 - amount) * v1.x + amount * v2.x,
(1 - amount) * v1.y + amount * v2.y
);
}
public static clone(v: Vec2): Vec2 {
return new this(v.x, v.y);
}
public static toArray(v: Vec2): [number, number] {
return [v.x, v.y];
}
}