BlueGit

timfa frameworkv3

Files

frameworkv3 / vector.js

vector.js

Initial Commit
timfa Tim Falken · pushed by timfa · 10 years ago
d075adff History
vector.js 3.2 KiB Download Raw
1//<DOC> category vector()
2
3function vector(x, y)
4{
5 //<DOC> <parameter> vector.x<br><br>the X axis of the vector.
6 this.x = x;
7 //<DOC> <parameter> vector.y<br><br>the Y axis of the vector.
8 this.y = y;
9
10 //<DOC> <function> vector.length()<br><br>returns the length of the vector.
11 this.length = function()
12 {
13 return Math.sqrt((this.x * this.x) + (this.y * this.y));
14 }
15
16 //<DOC> <function> vector.toAngle()<br><br>Get the angle towards the vector's direction in degrees.
17 this.toAngle = function()
18 {
19 return (Math.atan2(this.y, this.x) * 180 / Math.PI) + 90;
20 }
21
22 //<DOC> <function> vector.normalize()<br><br>returns a normalized copy of the vector. Original vector is not modified.
23 this.normalize = function()
24 {
25 var newVector = this.getCopy();
26
27 if(newVector.length() != 0)
28 {
29 var length = newVector.length();
30 newVector.x /= length;
31 newVector.y /= length;
32 }
33
34 return newVector;
35 }
36
37 //<DOC> <function> vector.lerpTo(otherVector, t)<br><br> Returns a new vector between this vector and 'otherVector' at point 't'. 0t = original vector, 1t = 'otherVector', 0.5t is halfway in between.
38 this.lerpTo = function(otherVector, t)
39 {
40 var delta = otherVector.subtract(this);
41
42 return this.add(delta.stretch(t));
43 }
44
45 //<DOC> <function> vector.distanceTo(otherVector)<br><br>returns the distance to another vector.
46 this.distanceTo = function(otherVector)
47 {
48 var dX = this.x - otherVector.x;
49 var dY = this.y - otherVector.y;
50
51 return new vector(dX, dY).length();
52 }
53
54 //<DOC> <function> vector.add(otherVector)<br><br>adds the two vectors together, and returns as a copy. Original vector is not modified.
55 this.add = function(otherVector)
56 {
57 var newVector = this.getCopy();
58
59 newVector.x += otherVector.x;
60 newVector.y += otherVector.y;
61
62 return newVector;
63 }
64
65 //<DOC> <function> vector.subtract(otherVector)<br><br>subtracts the second vector from this vector, and returns as a copy. Original vector is not modified.
66 this.subtract = function(otherVector)
67 {
68 var newVector = this.getCopy();
69
70 newVector.x -= otherVector.x;
71 newVector.y -= otherVector.y;
72
73 return newVector;
74 }
75
76 //<DOC> <function> vector.multiply(otherVector)<br><br>multiplies the first vector X by the second vector's X, and the first's Y by the second's Y, and returns as a copy. Original vector is not modified.
77 this.multiply = function(otherVector)
78 {
79 var newVector = this.getCopy();
80
81 newVector.x *= otherVector.x;
82 newVector.y *= otherVector.y;
83
84 return newVector;
85 }
86
87 //<DOC> <function> vector.divide(otherVector)<br><br>divides the first vector by the second, and returns as a copy. Original vector is not modified.
88 this.divide = function(otherVector)
89 {
90 var newVector = this.getCopy();
91
92 newVector.x /= otherVector.x;
93 newVector.y /= otherVector.y;
94
95 return newVector;
96 }
97
98 //<DOC> <function> vector.stretch(length)<br><br>Multiplies the vector by the given numeric value, and returns as a copy. Original vector is not modified.
99 this.stretch = function(length)
100 {
101 var newVector = this.getCopy();
102
103 newVector.x *= length;
104 newVector.y *= length;
105
106 return newVector;
107 }
108
109 //<DOC> <function> vector.getCopy()<br><br>returns a copy of this vector.
110 this.getCopy = function()
111 {
112 return new vector(this.x, this.y);
113 }
114}