forked from raimohanska/worzone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.js
51 lines (49 loc) · 1.61 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
function Point(x, y) {
return new Vector2D(x, y)
}
// Number -> Number -> Vector2D
function Vector2D(x, y) {
this.x = x;
this.y = y;
}
Vector2D.prototype = {
// Vector2D -> Vector2D
add : function(other) { return new Vector2D(this.x + other.x, this.y + other.y) },
// Vector2D -> Vector2D
subtract : function(other) { return new Vector2D(this.x - other.x, this.y - other.y) },
// Unit -> Number
getLength : function() { return Math.sqrt(this.x * this.x + this.y * this.y) },
// Number -> Vector2D
times : function(multiplier) { return new Vector2D(this.x * multiplier, this.y * multiplier) },
// Unit -> Vector2D
invert : function() { return new Vector2D(-this.x, -this.y) },
// Number -> Vector2D
withLength : function(newLength) { return this.times(newLength / this.getLength()) },
rotateRad : function(radians) {
var length = this.getLength()
var currentRadians = this.getAngle()
var resultRadians = radians + currentRadians
var rotatedUnit = new Vector2D(Math.cos(resultRadians), Math.sin(resultRadians))
return rotatedUnit.withLength(length)
},
// Number -> Vector2D
rotateDeg : function(degrees) {
var radians = degrees * 2 * Math.PI / 360
return this.rotateRad(radians)
},
// Unit -> Number
getAngle : function() {
var length = this.getLength()
unit = this.withLength(1)
return Math.atan2(unit.y, unit.x)
},
getAngleDeg : function() {
return this.getAngle() * 360 / (2 * Math.PI)
},
floor : function() {
return new Vector2D(Math.floor(this.x), Math.floor(this.y))
},
toString : function() {
return "(" + x + ", " + y + ")"
}
}