-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoord.js
More file actions
84 lines (68 loc) · 1.94 KB
/
coord.js
File metadata and controls
84 lines (68 loc) · 1.94 KB
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
define(function () {
/** Coordinate object. Represents a point in 2-space.
*/
function Coord(opt) {
this.x = 0;
this.y = 0;
this.initialize(opt);
// this.x = (x || 0);
// this.y = (y || 0);
}
Coord.prototype.initialize = function (opt) {
var key = null,
default_args = {
x : 0,
y : 0
};
opt = (opt || default_args);
for(key in default_args) {
if(typeof opt[key] == "undefined") opt[key] = default_args[key];
}
// opt[] has all the data - user provided and optional.
for(key in opt) {
//console.log(key + " = " + opt[key]);
this[key] = opt[key];
}
};
/** Move this Coord to a new (x, y) location.
* @param {Number} x
* @param {Number} y
*/
Coord.prototype.move = function (x, y) {
this.x = x;
this.y = y;
return this;
};
/** Add [Coord] other to this and return the result.
* @param {Coord} other
*/
Coord.prototype.plus = function (other) {
return new Coord({x : this.x + other.x, y : this.y + other.y});
};
/** Subtracts [Coord] other from this and return the result.
* @param {Coord} other
*/
Coord.prototype.minus = function (other) {
return new Coord({x : this.x - other.x, y : this.y - other.y});
};
/** Scales this [Coord] by the factor specified.
* @param {Number} scale
*/
Coord.prototype.scale = function (scale) {
return new Coord({x : this.x * scale, y : this.y * scale});
};
/** Returns distance from this [Coord] to [Coord] b.
* @param {Coord}
*/
Coord.prototype.dist = function (b) {
return Math.sqrt((this.x - b.x)*(this.x - b.x) + (this.y - b.y)*(this.y - b.y));
};
/** Returns the midpoint of this [Coord] and [Coord] b.
* @param {Coord}
*/
Coord.prototype.midpoint = function (b) {
return new Coord({x : (this.x + b.x) / 2, y : (this.y + b.y) / 2});
// return new Coord((this.x + b.x) / 2, (this.y + b.y) / 2);
};
return Coord;
});