-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.js
112 lines (89 loc) · 2.87 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
// This code is from the tutorial by Daniel Church.
// http://gamedevelopment.tutsplus.com/tutorials/make-your-game-pop-with-particle-effects-and-quadtrees--gamedev-2138
// This has been slightly modified and heavily commented for my own educational purposes
// in learning Object Oriented JavaScript.
// Although this class is called vectors, it should actually called Vector,
// because most of the methods are actually functions of vectors in Calculus.
function Vector( x, y ){
//alert("vector made");
this.x = x;
this.y = y;
}
// We clone this vector.
Vector.prototype.clone = function(){
return new Vector(this.x,this.y);
}
// We add another vector to this vector.
Vector.prototype.add = function( vector ){
this.x += vector.x;
this.y += vector.y;
}
// We return the sum of two vectors.
Vector.prototype.plus = function( vector ){
return this.clone().add( vector );
}
// We scale this vector.
Vector.prototype.scale = function( factor ){
this.x *= factor;
this.y *= factor;
}
// We return the scaling of this vector.
Vector.prototype.times = function( factor ){
return this.clone().scale( factor );
}
// We subtract another vector from this vector.
Vector.prototype.subtract = function( vector ){
this.x -= vector.x;
this.y -= vector.y;
}
// We return the difference of these two vectors.
Vector.prototype.minus = function( vector ){
return (this.clone()).subtract( vector );
}
// Magnitude of vector
Vector.prototype.getMagnitude = function(){
return Math.sqrt( this.x * this.x + this.y * this.y );
}
// Distance
Vector.prototype.distance = function( vector ){
return this.minus( vector ).magnitude();
}
// Dot product
Vector.prototype.dot = function( vector ){
return this.x * vector.x + this.y * vector.y;
}
// We get a vector in between.
Vector.prototype.interpolate = function( x, vector ){
return this.plus(other.minus( this ).times( x ) );
}
// Make a vector of magnitude 1.
Vector.prototype.normalize = function() {
this.scale(1.0 / this.magnitude());
}
// Return normalized copy of clone.
Vector.prototype.normalCopy = function(){
return this.clone().normalize();
}
// Rotate in radians
Vector.prototype.rotate = function ( angle ){
return new Vector( this.x * Math.cos(angle) - this.y * Math.sin(angle),
this.x * Math.sin(angle) + this.y * Math.cos(angle));
}
// Move
Vector.prototype.transform = function ( offset, angle ){
this.rotate( angle ).add( offset );
}
Vector.prototype.average = function(other) {
return this.interpolate(0.5, other);
};
Vector.fromPolar = function (angle, radius) {
//alert( "got here");
return new Vector(radius * Math.cos(angle), radius * Math.sin(angle));
};
Vector.prototype.getAngle = function () {
return Math.atan2( this.y, this.x);
}
Vector.prototype.toString = function() {
var string = "" + this.x + ", " + this.y + "";
return string;
}