-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.js
112 lines (99 loc) · 1.57 KB
/
vector.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* author :ormuseldesu
* date :2019/09/06 (September 6)
* description :my customer two-dimensional vector library
*/
class Vector
{
constructor(x = 0, y = 0)
{
this.x = x;
this.y = y;
}
set(x, y)
{
this.x = x;
this.y = y;
}
setX(scalar)
{
this.x = scalar;
}
setY(scalar)
{
this.x = scalar;
}
setMagnitude(scalar)
{
this.normalise();
this.x *= scalar;
this.y *= scalar;
}
getMagnitude()
{
let magnitude = (this.x**2) + (this.y**2);
return magnitude;
}
getX()
{
return this.x;
}
getY()
{
return this.y;
}
normalise()
{
let magnitude = this.getMagnitude();
this.x = this.x / magnitude || 0;
this.y = this.y / magnitude || 0;
}
add(vector)
{
let scalar = vector;
if(vector instanceof Vector)
{
this.x += vector.x;
this.y += vector.y;
} else {
this.x += scalar;
this.y += scalar;
}
}
sub(vector)
{
let scalar = vector;
if(vector instanceof Vector)
{
this.x -= vector.x;
this.y -= vector.y;
} else {
this.x -= scalar;
this.y -= scalar;
}
}
multiply(vector)
{
let scalar = vector;
if(vector instanceof Vector)
{
this.x *= vector.x;
this.y *= vector.y;
} else {
this.x *= scalar;
this.y *= scalar;
}
}
divide(vector)
{
let scalar = vector;
if(vector instanceof Vector)
{
this.x /= vector.x;
this.y /= vector.y;
} else {
this.x /= scalar;
this.y /= scalar;
}
}
}