1 |
// namespace |
2 |
if(window.refeed == null) { window.refeed = {}; } |
3 |
if(Refeed == null) { var Refeed = window.refeed; } |
4 |
|
5 |
/** |
6 |
* Representation of a box. |
7 |
* |
8 |
* A box has a location and a size, |
9 |
* and width & height are not negative. |
10 |
* Boxes are immutable. |
11 |
* |
12 |
* @param int x top |
13 |
* @param int x left |
14 |
* @param int w width (normalized to positive) |
15 |
* @param int h height (normalized to positive) |
16 |
*/ |
17 |
Refeed.Box = function(x, y, w, h) |
18 |
{ |
19 |
// width normalized to positive |
20 |
if(w < 0) { |
21 |
x = x + w; |
22 |
w = 0 - w; |
23 |
} |
24 |
|
25 |
// height normalized to positive |
26 |
if(h < 0) { |
27 |
y = y + h; |
28 |
h = 0 - h; |
29 |
} |
30 |
|
31 |
this._x = x; |
32 |
this._y = y; |
33 |
this._w = w; |
34 |
this._h = h; |
35 |
} |
36 |
|
37 |
Refeed.Box.prototype = { |
38 |
'x': function() { return this._x }, |
39 |
'y': function() { return this._y }, |
40 |
'width': function() { return this._w }, |
41 |
'height': function() { return this._h }, |
42 |
'top': function() { return this._y }, |
43 |
'right': function() { return this._x + this._w }, |
44 |
'bottom': function() { return this._y + this._h }, |
45 |
'left': function() { return this._x } |
46 |
} |
47 |
|
48 |
/** |
49 |
* @return Box a new box with the same properties as this one |
50 |
*/ |
51 |
Refeed.Box.prototype.copy = function() { return new Refeed.Box(this._x, this._y, this._w, this._h); } |
52 |
|
53 |
/** |
54 |
* @return string Short string representation of a box |
55 |
*/ |
56 |
Refeed.Box.prototype.toString = function() { return 'Box ' + this._w + 'x' + this._h + ' at ' + this._x + ',' + this._y; } |
57 |
|
58 |
/** |
59 |
* Determine whether two boxs overlap. |
60 |
* |
61 |
* isOverlapping() is assumed to be commutative, |
62 |
* i.e. this.isOverlapping(that) == that.isOverlapping(this). |
63 |
* |
64 |
* @param Box b A box |
65 |
* @return boolean True if they overlap |
66 |
*/ |
67 |
Refeed.Box.prototype.isOverlapping = function(b) |
68 |
{ |
69 |
// if this is clearly too far to the top, right, |
70 |
// left or bottom to touch r, there is no overlap |
71 |
if((this.top() > b.bottom()) || (this.bottom() < b.top()) || (this.left() > b.right()) || (this.right() < b.left())) { |
72 |
return false; |
73 |
} |
74 |
|
75 |
return true; |
76 |
} |
77 |
|
78 |
/** |
79 |
* Determine whether this is inside a box. |
80 |
* |
81 |
* @param Box b A box |
82 |
* @return boolean True if this is inside r |
83 |
*/ |
84 |
Refeed.Box.prototype.isInside = function(b) |
85 |
{ |
86 |
// check if this is trivially inside r on each side |
87 |
if((this.top() >= b.top()) && (this.bottom() <= b.bottom()) && (this.left() >= b.left()) && (this.right() <= b.right())) { |
88 |
return true; |
89 |
} |
90 |
|
91 |
return false; |
92 |
} |
93 |
|
94 |
/** |
95 |
* Determine whether this encloses a box. |
96 |
* |
97 |
* @param Box b A box |
98 |
* @return boolean True if r is inside this |
99 |
*/ |
100 |
Refeed.Box.prototype.isEnclosing = function(b) |
101 |
{ |
102 |
// just inverts the sense of isInside() |
103 |
return b.isInside(this); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Representation of a point. |
108 |
* |
109 |
* A point has a location. |
110 |
* Points are immutable. |
111 |
* |
112 |
* @param int x top |
113 |
* @param int x left |
114 |
*/ |
115 |
Refeed.Point = function(x, y) |
116 |
{ |
117 |
this._x = x; |
118 |
this._y = y; |
119 |
this._w = 0; |
120 |
this._h = 0; |
121 |
} |
122 |
|
123 |
Refeed.Point.prototype = new Refeed.Box(); |
124 |
|
125 |
/** |
126 |
* @return string Short string representation of a point |
127 |
*/ |
128 |
Refeed.Point.prototype.toString = function() { return 'Point at ' + this._x + ',' + this._y; } |
129 |
|
130 |
/** |
131 |
* @return Point a new point with the same properties as this one |
132 |
*/ |
133 |
Refeed.Point.prototype.copy = function() { return new Point(this._x, this._y); } |