-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector2.js
96 lines (82 loc) · 2.23 KB
/
Vector2.js
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
'use strict'
//Vector2 Class becasue I lost my mind
class Vector2 {
constructor (x, y) {
this.x = x;
this.y = y;
}
// Returns the magnitude of the current Vector2
magnitude() {
return Math.sqrt(
Math.pow(this.x, 2) +
Math.pow(this.y, 2)
);
}
// Returns a normalized version of the current Vector2
normalized() {
if (this.magnitude != 0) {
return new Vector2(
this.x / this.magnitude(),
this.y / this.magnitude()
);
}
else {
this.x = 0;
this.y = 0;
}
}
//Returns the counter-clockwise angle of the current Vector2
angle() {
return Math.atan(this.y / this.x) *
(180 / Math.PI);
}
// Returns the product of the current Vector2 and the scalar quantity.
multiply(scale) {
return new Vector2(
this.x * scale,
this.y * scale
);
}
// Returns the sum of the current and parameter Vector2.
add(vec2) {
if (vec2 instanceof Vector2) {
return new Vector2(
this.x + vec2.x,
this.y + vec2.y
);
}
else {
console.log("A Vector2 was not passed as the parameter.");
}
}
// Returns the difference of the current and parameter Vector2.
subtract(vec2) {
if (vec2 instanceof Vector2) {
return new Vector2(
this.x - vec2.x,
this.y - vec2.y
);
}
else {
console.log("A Vector2 was not passed as the parameter.");
}
}
// Returns the dot product of the currecnt and parameter Vector2
dot(vec2) {
if (vec2 instanceof Vector2) {
return this.x * vec2.x + this.y * vec2.y;
}
else {
console.log("A Vector2 was not passed as the parameter.");
}
}
// Returns the distance between two vectors
dist(vec2) {
if (vec2 instanceof Vector2) {
return this.subtract(vec2).magnitude();
}
else {
console.log("A Vector2 was not passed as the parameter.");
}
}
}