-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.js
150 lines (120 loc) · 3.03 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var Vector = (function() {
function Vector(x, y) {
this.x = x || 0;
this.y = y || 0;
return this;
}
Vector.createPolar = function(phi, len) {
var v = new Vector();
v.setPolar(phi, len);
return v;
}
Vector.prototype.createFrom = function(v) {
return new Vector(v.x, v.y);
}
Vector.prototype.isEqual = function(v) {
return (this.x === v.x) && (this.y === v.y);
}
Vector.prototype.copy = function() {
return new Vector(this.x, this.y);
}
Vector.prototype.getAngle = function() {
return Math.atan2(this.y, this.x);
}
Vector.prototype.getLengthSquared = function() {
return (this.x*this.x) + (this.y*this.y);
}
Vector.prototype.getLength = function() {
return Math.sqrt(this.getLengthSquared());
}
Vector.prototype.setLength = function(len) {
var phi = this.getAngle();
return this.setPolar(phi, len);
}
Vector.prototype.setAngle = function(phi) {
var len = this.getLength();
return this.setPolar(phi, len);
}
Vector.prototype.set = function(v) {
this.x = v.x;
this.y = v.y;
return this;
}
Vector.prototype.add = function(v) {
return new Vector(this.x+v.x, this.y+v.y);
}
Vector.prototype.sub = function(v) {
return new Vector(this.x-v.x, this.y-v.y);
}
Vector.prototype.multiply = function(v) {
return new Vector(this.x*v.x, this.y*v.y);
}
Vector.prototype.divide = function(v) { // et impera
return new Vector(this.x/v.x, this.y/v.y);
}
Vector.prototype.angleTo = function(v) {
return Math.atan2(v.y-this.y, v.x-this.x);
}
Vector.prototype.setAngleTo = function(v) {
var phi = this.angleTo(v);
this.setAngle(phi);
return phi;
}
Vector.prototype.setLengthTo = function(v, len) {
var phi = this.angleTo(v);
this.setPolar(phi, len);
return this;
}
Vector.prototype.setPolar = function(phi, len) {
this.x = Math.cos(phi) * len;
this.y = Math.sin(phi) * len;
return this;
}
Vector.prototype.scale = function(scalar) {
this.x *= scalar;
this.y *= scalar;
return this;
}
Vector.prototype.addScalar = function(scalar) {
this.x += scalar;
this.y += scalar;
return this;
}
Vector.prototype.distanceTo = function(v) {
var dx = v.x-this.x,
dy = v.y-this.y;
return Math.sqrt(dx*dx + dy*dy);
}
Vector.prototype.addTo = function(v) {
this.x += v.x;
this.y += v.y;
return this;
}
Vector.prototype.subFrom = function(v) {
this.x -= v.x;
this.y -= v.y;
return this;
}
Vector.prototype.mulBy = function(v) {
this.x *= v.x;
this.y *= v.y;
return this;
}
Vector.prototype.divBy = function(v) {
this.x /= v.x;
this.y /= v.y;
return this;
}
Vector.prototype.toString = function() {
return '(' + this.x + ', ' + this.y + ')';
}
Vector.prototype.fix = function() {
this.x = Math.ceil(this.x);
this.y = Math.ceil(this.y);
return this;
}
Vector.prototype.toFixed = function() {
return copy().fix();
}
return Vector;
})();