diff options
Diffstat (limited to 'js/helper-classes/3D/Rectangle.js')
-rw-r--r-- | js/helper-classes/3D/Rectangle.js | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/js/helper-classes/3D/Rectangle.js b/js/helper-classes/3D/Rectangle.js new file mode 100644 index 00000000..e797eedf --- /dev/null +++ b/js/helper-classes/3D/Rectangle.js | |||
@@ -0,0 +1,209 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /////////////////////////////////////////////////////////////////////// | ||
8 | // Class Rectangle | ||
9 | // 2D rectangle | ||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | var Rectangle = exports.Rectangle = Object.create(Object.prototype, { | ||
12 | /////////////////////////////////////////////////////////////////////// | ||
13 | // Instance variables | ||
14 | /////////////////////////////////////////////////////////////////////// | ||
15 | m_top: { value: null, writable: true }, | ||
16 | m_left: { value: null, writable: true }, | ||
17 | m_width: { value: null, writable: true }, | ||
18 | m_height: { value: null, writable: true }, | ||
19 | |||
20 | |||
21 | /////////////////////////////////////////////////////////////////////// | ||
22 | // Property accessors | ||
23 | /////////////////////////////////////////////////////////////////////// | ||
24 | set: { value: function(l,t,w,h) { this.m_left = l; this.m_top = t; this.m_width = w; this.m_height = h; } }, | ||
25 | |||
26 | getLeft: { value: function() { return this.m_left; } }, | ||
27 | setLeft: { value: function(l) { this.m_left = l; } }, | ||
28 | |||
29 | getRight: { value: function() { return this.m_left + this.m_width; } }, | ||
30 | setRight: { value: function(r) { this.m_width = r - this.m_left; } }, | ||
31 | |||
32 | getBottom: { value: function() { return this.m_top + this.m_height; } }, | ||
33 | setBottom: { value: function(b) { this.m_height = b - this.m_top; } }, | ||
34 | |||
35 | getTop: { value: function() { return this.m_top; } }, | ||
36 | setTop: { value: function(t) { this.m_top = t; } }, | ||
37 | |||
38 | getCenter: { value: function() { return Vector.create( [this.m_left + 0.5*this.m_width, this.m_top + 0.5*this.m_height] ); } }, | ||
39 | |||
40 | getWidth: { value: function() { return this.m_width; } }, | ||
41 | setWidth: { value: function(w) { this.m_width = w; } }, | ||
42 | |||
43 | getHeight: { value: function() { return this.m_height; } }, | ||
44 | setHeight: { value: function(h) { this.m_height = h; } }, | ||
45 | |||
46 | geomType: { value: function() { return this.GEOM_TYPE_RECTANGLE; } }, | ||
47 | |||
48 | /////////////////////////////////////////////////////////////////////// | ||
49 | // Methods | ||
50 | /////////////////////////////////////////////////////////////////////// | ||
51 | contains: { | ||
52 | value: function( x, y ) | ||
53 | { | ||
54 | if (x < this.getLeft()) return false; | ||
55 | if (x > this.getRight()) return false; | ||
56 | if (y < this.getTop()) return false; | ||
57 | if (y > this.getBottom()) return false; | ||
58 | |||
59 | return true; | ||
60 | } | ||
61 | }, | ||
62 | |||
63 | dup: { | ||
64 | value: function() | ||
65 | { | ||
66 | var rtnRec = Object.create(Rectangle, {}); | ||
67 | rtnRec.m_top = this.m_top; | ||
68 | rtnRec.m_left = this.m_left; | ||
69 | rtnRec.m_width = this.m_width; | ||
70 | rtnRec.m_height = this.m_height; | ||
71 | |||
72 | return rtnRec; | ||
73 | } | ||
74 | }, | ||
75 | |||
76 | onBoundary: { | ||
77 | value: function( x, y ) | ||
78 | { | ||
79 | if ((MathUtils.fpCmp(y,this.getTop()) >= 0) && (MathUtils.fpCmp(y,this.getBottom()) <= 0)) | ||
80 | { | ||
81 | if ((MathUtils.fpCmp(x, this.getLeft()) == 0) || (MathUtils.fpCmp(x, this.getRight()) == 0)) return true; | ||
82 | |||
83 | if ((MathUtils.fpCmp(x,this.getLeft()) >= 0) && (MathUtils.fpCmp(x,this.getRight()) <= 0)) | ||
84 | { | ||
85 | if ((MathUtils.fpCmp(y, this.getTop()) == 0) || (MathUtils.fpCmp(y, this.getBottom()) == 0)) return true; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | return false; | ||
90 | } | ||
91 | }, | ||
92 | |||
93 | setToPoint: { | ||
94 | value: function( pt ) | ||
95 | { | ||
96 | this.m_left = pt[0]; this.m_top = pt[1]; | ||
97 | this.m_width = 0; this.m_height = 0; | ||
98 | } | ||
99 | }, | ||
100 | |||
101 | setToBounds: { | ||
102 | value: function( bounds ) | ||
103 | { | ||
104 | var pt = bounds[0]; | ||
105 | this.setToPoint( pt ); | ||
106 | for (var i=1; i<4; i++) | ||
107 | this.unionPoint( bounds[i] ); | ||
108 | } | ||
109 | }, | ||
110 | |||
111 | getPoint: { | ||
112 | value: function(i) | ||
113 | { | ||
114 | if (i < 0) throw( "invalid point index in Rectangle.getPoint: " + i ); | ||
115 | |||
116 | i = i % 4; | ||
117 | var pt = [0,0]; | ||
118 | switch (i) | ||
119 | { | ||
120 | case 0: | ||
121 | pt[0] = this.getLeft(); | ||
122 | pt[1] = this.getTop(); | ||
123 | break; | ||
124 | |||
125 | case 1: | ||
126 | pt[0] = this.getLeft(); | ||
127 | pt[1] = this.getBottom(); | ||
128 | break; | ||
129 | |||
130 | case 2: | ||
131 | pt[0] = this.getRight(); | ||
132 | pt[1] = this.getBottom(); | ||
133 | break; | ||
134 | |||
135 | case 3: | ||
136 | pt[0] = this.getRight(); | ||
137 | pt[1] = this.getTop(); | ||
138 | break; | ||
139 | } | ||
140 | |||
141 | return pt; | ||
142 | } | ||
143 | }, | ||
144 | |||
145 | getQuadrant: { | ||
146 | value: function( iQuad ) | ||
147 | { | ||
148 | // quadrant ordering starts at upper left and continues around counter-clockwise | ||
149 | |||
150 | var rtnQuad = this.dup(); | ||
151 | var hw = 0.5*this.m_width, hh = 0.5*this.m_height; | ||
152 | rtnQuad.m_width = hw; | ||
153 | rtnQuad.m_height = hh; | ||
154 | switch (iQuad) | ||
155 | { | ||
156 | case 0: | ||
157 | // no-op | ||
158 | break; | ||
159 | |||
160 | case 1: | ||
161 | rtnQuad.m_top += hh; | ||
162 | break; | ||
163 | |||
164 | case 2: | ||
165 | rtnQuad.m_left += hw; | ||
166 | rtnQuad.m_top += hh; | ||
167 | break; | ||
168 | |||
169 | case 3: | ||
170 | rtnQuad.m_left += hw; | ||
171 | break; | ||
172 | |||
173 | default: | ||
174 | throw new Error( "invalid quadrant to Rectangle.getQuadrant: " + iQuad ); | ||
175 | break; | ||
176 | } | ||
177 | |||
178 | return rtnQuad; | ||
179 | } | ||
180 | }, | ||
181 | |||
182 | unionPoint: { | ||
183 | value: function( pt ) | ||
184 | { | ||
185 | var x = pt[0]; | ||
186 | var xMin = this.getLeft(), xMax = this.getRight(); | ||
187 | if (x < xMin) xMin =x; | ||
188 | else if (x > xMax) xMax = x; | ||
189 | |||
190 | var y = pt[1]; | ||
191 | var yMin = this.getTop(), yMax = this.getBottom(); | ||
192 | if (y < yMin) yMin = y; | ||
193 | else if (y > yMax) yMax = y; | ||
194 | |||
195 | this.setLeft( xMin ); this.setWidth( xMax - xMin ); | ||
196 | this.setTop( yMin ); this.setHeight( yMax - yMin ); | ||
197 | } | ||
198 | }, | ||
199 | |||
200 | |||
201 | translate: { | ||
202 | value: function( dx, dy ) | ||
203 | { | ||
204 | this.m_left += dx; | ||
205 | this.m_top += dy; | ||
206 | } | ||
207 | } | ||
208 | }); | ||
209 | |||