Repository navigation
8000
More file actions
Expand file tree
/
Copy pathvector.js
More file actions
68 lines (56 loc) · 1.89 KB
/
Copy pathvector.js
File metadata and controls
68 lines (56 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
8410
9
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
// Originally from https://github.com/raimohanska/worzone
;(function (global) {
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(),
currentRadians = this.getAngle(),
resultRadians = radians + currentRadians,
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 + ')';
}
};
global.Game = {
Point: Point,
Vector2D: Vector2D
};
}(window));