-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.js
76 lines (63 loc) · 1.17 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
function vector(x= 1, y= 0)
{
this.dx= x;
this.dy= y;
this.set_dxy= function(x, y)
{
this.dx= x;
this.dy= y;
}
this.set_dx= function(x)
{
this.dx= x;
}
this.set_dy= function(y)
{
this.dy= y;
}
this.get_angle= function()
{
return Math.atan2(this.dy, this.dx);
}
this.get_mag= function()
{
return Math.sqrt(this.dx * this.dx + this.dy * this.dy);
}
this.set_angle= function(angle)
{
var mag= this.get_mag();
this.dx= Math.cos(angle) * mag;
this.dy= Math.sin(angle) * mag;
}
this.set_mag= function(mag)
{
var angle= this.get_angle();
this.dx= Math.cos(angle) * mag;
this.dy= Math.sin(angle) * mag;
}
this.add= function(vect)
{
this.dx= this.dx + vect.dx;
this.dy= this.dy + vect.dy;
}
this.subtract= function(vect)
{
this.dx= this.dx - vect.dx;
this.dy= this.dy - vect.dy;
}
this.multiply= function(scalar)
{
this.dx= this.dx * scalar;
this.dy= this.dy * scalar;
}
this.divide= function(scalar)
{
this.dx= this.dx / scalar;
this.dy= this.dy / scalar;
}
this.set_vect= function(vect)
{
this.dx= vect.dx;
this.dy= vect.dy;
}
}