-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvector.js
48 lines (37 loc) · 851 Bytes
/
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
var Vector = function(_x, _y)
{
this.x = _x;
this.y = _y;
this.draw = function(size, color) {
gfx.beginPath();
gfx.arc(this.x, this.y, size, 0, 2 * Math.PI, true);
gfx.fillStyle = color;
gfx.fill();
gfx.strokeStyle = color;
gfx.stroke();
}
this.add = function( vector )
{
return new Vector(this.x += vector.x, this.y += vector.y);
}
this.subtract = function( vector )
{
return new Vector(this.x -= vector.x, this.y -= vector.y);
}
this.multiply = function ( multiplier )
{
return new Vector(this.x *= multiplier, this.y *= multiplier);
}
this.length = function()
{
return Math.sqrt(this.x * this.x + this.y * this.y);
}
this.cross = function( vector )
{
return this.x * vector.y - this.y * vector.x;
}
this.dot = function( vector )
{
return this.x * vector.x + this.y * vector.y;
}
};