-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
42 lines (42 loc) · 944 Bytes
/
vector.js
File metadata and controls
42 lines (42 loc) · 944 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
function Vector(x, y)
{
this.x = x;
this.y = y;
}
Vector.prototype =
{
add: function (rhs)
{
return new Vector(this.x + rhs.x, this.y + rhs.y);
},
sub: function (rhs)
{
return new Vector(this.x - rhs.x, this.y - rhs.y);
},
toString: function ()
{
return "[" + this.x + "," + this.y + "]";
},
mag: function ()
{
return Math.sqrt(this.x * this.x + this.y * this.y);
},
unit: function ()
{
var magnitude = this.mag();
return new Vector(this.x * 1.0 / magnitude, this.y * 1.0 / magnitude);
},
dot: function (rhs)
{
return this.x * rhs.x + this.y * rhs.y;
},
scale: function (scalar)
{
return new Vector(this.x * scalar, this.y * scalar);
},
norm: function ()
{
var magnitude = this.mag();
return new Vector(this.y * 1.0 / magnitude, this.x * -1.0 / magnitude);
}
}