-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector2.js
121 lines (120 loc) · 3.49 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var Vector2 = function(x, y) {
this.x = x || 0;
this.y = y || 0;
};
Vector2.prototype = {
reset: function(x, y) {
this.x = x;
this.y = y;
return this;
},
toString: function(decPlaces) {
decPlaces = decPlaces || 3;
var scalar = Math.pow(10, decPlaces);
return "[" + Math.round(this.x * scalar) / scalar + ", " + Math.round (this.y * scalar) / scalar + "]";
},
clone: function() {
return new Vector2(this.x, this.y);
},
copyTo: function(v) {
v.x = this.x;
v.y = this.y;
},
copyFrom: function(v) {
this.x = v.x;
this.y = v.y;
},
magnitude: function() {
return Math.sqrt((this.x * this.x) + (this.y * this.y));
},
magnitudeSquared: function() {
return (this.x * this.x) + (this.y * this.y);
},
normalise: function() {
var m = this.magnitude();
this.x = this.x / m;
this.y = this.y / m;
return this;
},
reverse: function() {
this.x =- this.x;
this.y =- this.y;
return this;
},
plusEq: function(v) {
this.x += v.x;
this.y += v.y;
return this;
},
plusNew: function(v) {
return new Vector2(this.x + v.x, this.y + v.y);
},
minusEq: function(v) {
this.x -= v.x;
this.y -= v.y;
return this;
},
minusNew: function(v) {
return new Vector2(this.x - v.x, this.y - v.y);
},
multiplyEq: function(scalar) {
this.x*=scalar;
this.y*=scalar;
return this;
},
multiplyNew: function(scalar) {
var returnvec = this.clone();
return returnvec.multiplyEq(scalar);
},
divideEq: function(scalar) {
this.x/=scalar;
this.y/=scalar;
return this;
},
divideNew: function(scalar) {
var returnvec = this.clone();
return returnvec.divideEq(scalar);
},
dot: function(v) {
return (this.x * v.x) + (this.y * v.y);
},
angle: function(useRadians) {
return Math.atan2(this.y, this.x) * (useRadians ? 1 : Vector2Const.TO_DEGREES);
},
rotate: function(angle, useRadians) {
var cosRY = Math.cos(angle * (useRadians ? 1 : Vector2Const.TO_RADIANS));
var sinRY = Math.sin(angle * (useRadians ? 1 : Vector2Const.TO_RADIANS));
Vector2Const.temp.copyFrom(this);
this.x = (Vector2Const.temp.x * cosRY) - (Vector2Const.temp.y * sinRY);
this.y = (Vector2Const.temp.x * sinRY) + (Vector2Const.temp.y * cosRY);
return this;
},
equals: function(v) {
return ((this.x == v.x) && (this.y == v.x));
},
isCloseTo: function(v, tolerance) {
if (this.equals(v))
return true;
Vector2Const.temp.copyFrom(this);
Vector2Const.temp.minusEq(v);
return (Vector2Const.temp.magnitudeSquared() < tolerance * tolerance);
},
rotateAroundPoint: function(point, angle, useRadians) {
Vector2Const.temp.copyFrom(this);
Vector2Const.temp.minusEq(point);
Vector2Const.temp.rotate(angle, useRadians);
Vector2Const.temp.plusEq(point);
this.copyFrom(Vector2Const.temp);
},
isMagLessThan: function(distance) {
return (this.magnitudeSquared() < distance * distance);
},
isMagGreaterThan: function(distance) {
return (this.magnitudeSquared() > distance * distance);
}
};
Vector2Const = {
TO_DEGREES: 180 / Math.PI,
TO_RADIANS: Math.PI / 180,
temp: new Vector2()
};