-
Notifications
You must be signed in to change notification settings - Fork 1
/
Point.js
78 lines (63 loc) · 1.63 KB
/
Point.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
(function(root) {
root.Point = Point;
function Point(x, y) {
this._x = x;
this._y = y;
}
Object.defineProperty(Point.prototype, 'x', {
get: function() { return this._x; },
set: function(x) {
this._x = x;
this.trigger('change');
}
});
Object.defineProperty(Point.prototype, 'y', {
get: function() { return this._y; },
set: function(y) {
this._y = y;
this.trigger('change');
}
});
_.extend(Point.prototype, Backbone.Events, {
'toString': function toString() {
return this._x + ',' + this._y;
},
'isEqual': function isEqual(point) {
if (!point)
return false;
return this._x == point.x && this._y == point.y;
},
'clone': function clone() {
return new Point(this._x, this._y);
},
'diff': function diff(other_point) {
return new Point(this._x - other_point.x, this._y - other_point.y);
},
'add': function add(other_point) {
this._x += other_point.x;
this._y += other_point.y;
return this;
},
'scale': function scale(scale) {
this._x *= scale;
this._y *= scale;
},
'snap': function snap() {
this._x = snap(this._x);
this._y = snap(this._y);
function snap(val) {
return Math.round(val / 20) * 20;
}
return this;
},
'toJSON': function toJSON() {
return {
'x': this._x,
'y': this._y
}
}
});
Point.deserialize = function deserialize(raw_point) {
return new Point(raw_point.x, raw_point.y);
};
})(this);