-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2d.js
More file actions
56 lines (43 loc) · 957 Bytes
/
vector2d.js
File metadata and controls
56 lines (43 loc) · 957 Bytes
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
//Jacob H 2023
function deg2Rad(deg) {
return deg * Math.PI / 180;
}
function rad2Deg(rad)
{
return rad * 180/Math.PI;
}
class vector2d {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(other) {
return new vector2d(this.x + other.x, this.y + other.y);
}
sub(other) {
return new vector2d(this.x - other.x, this.y - other.y);
}
mult(scalar) {
return new vector2d(this.x * scalar, this.y * scalar);
}
mag() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
dot(other) {
return other.x * this.x + other.y * this.y;
}
copy() {
return new vector2d(this.x, this.y);
}
div(scalar) {
return new vector2d(this.x / scalar, this.y / scalar);
}
}
//angle unit in radians
//construct a vector from angle and magnitude
function vector2dFromAngle(angle, mag) {
return new vector2d(mag * Math.cos(angle), mag * Math.sin(angle));
}
function angleBetween(a, b) {
return Math.acos((a.dot(b)) / (a.mag() * b.mag()));
}