aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/3D/rectangle.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/3D/rectangle.js')
-rwxr-xr-xjs/helper-classes/3D/rectangle.js234
1 files changed, 234 insertions, 0 deletions
diff --git a/js/helper-classes/3D/rectangle.js b/js/helper-classes/3D/rectangle.js
new file mode 100755
index 00000000..094316a9
--- /dev/null
+++ b/js/helper-classes/3D/rectangle.js
@@ -0,0 +1,234 @@
1/* <copyright>
2Copyright (c) 2012, Motorola Mobility LLC.
3All Rights Reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
8* Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11* Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15* Neither the name of Motorola Mobility LLC nor the names of its
16 contributors may be used to endorse or promote products derived from this
17 software without specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29POSSIBILITY OF SUCH DAMAGE.
30</copyright> */
31
32///////////////////////////////////////////////////////////////////////
33// Class Rectangle
34// 2D rectangle
35///////////////////////////////////////////////////////////////////////
36var Rectangle = exports.Rectangle = Object.create(Object.prototype, {
37 ///////////////////////////////////////////////////////////////////////
38 // Instance variables
39 ///////////////////////////////////////////////////////////////////////
40 m_top: { value: null, writable: true },
41 m_left: { value: null, writable: true },
42 m_width: { value: null, writable: true },
43 m_height: { value: null, writable: true },
44
45
46 ///////////////////////////////////////////////////////////////////////
47 // Property accessors
48 ///////////////////////////////////////////////////////////////////////
49 set: { value: function(l,t,w,h) { this.m_left = l; this.m_top = t; this.m_width = w; this.m_height = h; } },
50
51 getLeft: { value: function() { return this.m_left; } },
52 setLeft: { value: function(l) { this.m_left = l; } },
53
54 getRight: { value: function() { return this.m_left + this.m_width; } },
55 setRight: { value: function(r) { this.m_width = r - this.m_left; } },
56
57 getBottom: { value: function() { return this.m_top + this.m_height; } },
58 setBottom: { value: function(b) { this.m_height = b - this.m_top; } },
59
60 getTop: { value: function() { return this.m_top; } },
61 setTop: { value: function(t) { this.m_top = t; } },
62
63 getCenter: { value: function() { return [this.m_left + 0.5*this.m_width, this.m_top + 0.5*this.m_height]; } },
64
65 getWidth: { value: function() { return this.m_width; } },
66 setWidth: { value: function(w) { this.m_width = w; } },
67
68 getHeight: { value: function() { return this.m_height; } },
69 setHeight: { value: function(h) { this.m_height = h; } },
70
71 geomType: { value: function() { return this.GEOM_TYPE_RECTANGLE; } },
72
73 ///////////////////////////////////////////////////////////////////////
74 // Methods
75 ///////////////////////////////////////////////////////////////////////
76 contains: {
77 value: function( x, y )
78 {
79 if (x < this.getLeft()) return false;
80 if (x > this.getRight()) return false;
81 if (y < this.getTop()) return false;
82 if (y > this.getBottom()) return false;
83
84 return true;
85 }
86 },
87
88 dup: {
89 value: function()
90 {
91 var rtnRec = Object.create(Rectangle, {});
92 rtnRec.m_top = this.m_top;
93 rtnRec.m_left = this.m_left;
94 rtnRec.m_width = this.m_width;
95 rtnRec.m_height = this.m_height;
96
97 return rtnRec;
98 }
99 },
100
101 onBoundary: {
102 value: function( x, y )
103 {
104 if ((MathUtils.fpCmp(y,this.getTop()) >= 0) && (MathUtils.fpCmp(y,this.getBottom()) <= 0))
105 {
106 if ((MathUtils.fpCmp(x, this.getLeft()) == 0) || (MathUtils.fpCmp(x, this.getRight()) == 0)) return true;
107
108 if ((MathUtils.fpCmp(x,this.getLeft()) >= 0) && (MathUtils.fpCmp(x,this.getRight()) <= 0))
109 {
110 if ((MathUtils.fpCmp(y, this.getTop()) == 0) || (MathUtils.fpCmp(y, this.getBottom()) == 0)) return true;
111 }
112 }
113
114 return false;
115 }
116 },
117
118 setToPoint: {
119 value: function( pt )
120 {
121 this.m_left = pt[0]; this.m_top = pt[1];
122 this.m_width = 0; this.m_height = 0;
123 }
124 },
125
126 setToBounds: {
127 value: function( bounds )
128 {
129 var pt = bounds[0];
130 this.setToPoint( pt );
131 for (var i=1; i<4; i++)
132 this.unionPoint( bounds[i] );
133 }
134 },
135
136 getPoint: {
137 value: function(i)
138 {
139 if (i < 0) throw( "invalid point index in Rectangle.getPoint: " + i );
140
141 i = i % 4;
142 var pt = [0,0];
143 switch (i)
144 {
145 case 0:
146 pt[0] = this.getLeft();
147 pt[1] = this.getTop();
148 break;
149
150 case 1:
151 pt[0] = this.getLeft();
152 pt[1] = this.getBottom();
153 break;
154
155 case 2:
156 pt[0] = this.getRight();
157 pt[1] = this.getBottom();
158 break;
159
160 case 3:
161 pt[0] = this.getRight();
162 pt[1] = this.getTop();
163 break;
164 }
165
166 return pt;
167 }
168 },
169
170 getQuadrant: {
171 value: function( iQuad )
172 {
173 // quadrant ordering starts at upper left and continues around counter-clockwise
174
175 var rtnQuad = this.dup();
176 var hw = 0.5*this.m_width, hh = 0.5*this.m_height;
177 rtnQuad.m_width = hw;
178 rtnQuad.m_height = hh;
179 switch (iQuad)
180 {
181 case 0:
182 // no-op
183 break;
184
185 case 1:
186 rtnQuad.m_top += hh;
187 break;
188
189 case 2:
190 rtnQuad.m_left += hw;
191 rtnQuad.m_top += hh;
192 break;
193
194 case 3:
195 rtnQuad.m_left += hw;
196 break;
197
198 default:
199 throw new Error( "invalid quadrant to Rectangle.getQuadrant: " + iQuad );
200 break;
201 }
202
203 return rtnQuad;
204 }
205 },
206
207 unionPoint: {
208 value: function( pt )
209 {
210 var x = pt[0];
211 var xMin = this.getLeft(), xMax = this.getRight();
212 if (x < xMin) xMin =x;
213 else if (x > xMax) xMax = x;
214
215 var y = pt[1];
216 var yMin = this.getTop(), yMax = this.getBottom();
217 if (y < yMin) yMin = y;
218 else if (y > yMax) yMax = y;
219
220 this.setLeft( xMin ); this.setWidth( xMax - xMin );
221 this.setTop( yMin ); this.setHeight( yMax - yMin );
222 }
223 },
224
225
226 translate: {
227 value: function( dx, dy )
228 {
229 this.m_left += dx;
230 this.m_top += dy;
231 }
232 }
233});
234