source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/src/math/Box2.js@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 4.2 KB
Line 
1/**
2 * @author bhouston / http://exocortex.com
3 */
4
5THREE.Box2 = function ( min, max ) {
6
7 this.min = ( min !== undefined ) ? min : new THREE.Vector2( Infinity, Infinity );
8 this.max = ( max !== undefined ) ? max : new THREE.Vector2( -Infinity, -Infinity );
9
10};
11
12THREE.Box2.prototype = {
13
14 constructor: THREE.Box2,
15
16 set: function ( min, max ) {
17
18 this.min.copy( min );
19 this.max.copy( max );
20
21 return this;
22
23 },
24
25 setFromPoints: function ( points ) {
26
27 if ( points.length > 0 ) {
28
29 var point = points[ 0 ];
30
31 this.min.copy( point );
32 this.max.copy( point );
33
34 for ( var i = 1, il = points.length; i < il; i ++ ) {
35
36 point = points[ i ];
37
38 if ( point.x < this.min.x ) {
39
40 this.min.x = point.x;
41
42 } else if ( point.x > this.max.x ) {
43
44 this.max.x = point.x;
45
46 }
47
48 if ( point.y < this.min.y ) {
49
50 this.min.y = point.y;
51
52 } else if ( point.y > this.max.y ) {
53
54 this.max.y = point.y;
55
56 }
57
58 }
59
60 } else {
61
62 this.makeEmpty();
63
64 }
65
66 return this;
67
68 },
69
70 setFromCenterAndSize: function () {
71
72 var v1 = new THREE.Vector2();
73
74 return function ( center, size ) {
75
76 var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
77 this.min.copy( center ).sub( halfSize );
78 this.max.copy( center ).add( halfSize );
79
80 return this;
81
82 };
83
84 }(),
85
86 copy: function ( box ) {
87
88 this.min.copy( box.min );
89 this.max.copy( box.max );
90
91 return this;
92
93 },
94
95 makeEmpty: function () {
96
97 this.min.x = this.min.y = Infinity;
98 this.max.x = this.max.y = -Infinity;
99
100 return this;
101
102 },
103
104 empty: function () {
105
106 // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
107
108 return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
109
110 },
111
112 center: function ( optionalTarget ) {
113
114 var result = optionalTarget || new THREE.Vector2();
115 return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
116
117 },
118
119 size: function ( optionalTarget ) {
120
121 var result = optionalTarget || new THREE.Vector2();
122 return result.subVectors( this.max, this.min );
123
124 },
125
126 expandByPoint: function ( point ) {
127
128 this.min.min( point );
129 this.max.max( point );
130
131 return this;
132 },
133
134 expandByVector: function ( vector ) {
135
136 this.min.sub( vector );
137 this.max.add( vector );
138
139 return this;
140 },
141
142 expandByScalar: function ( scalar ) {
143
144 this.min.addScalar( -scalar );
145 this.max.addScalar( scalar );
146
147 return this;
148 },
149
150 containsPoint: function ( point ) {
151
152 if ( point.x < this.min.x || point.x > this.max.x ||
153 point.y < this.min.y || point.y > this.max.y ) {
154
155 return false;
156
157 }
158
159 return true;
160
161 },
162
163 containsBox: function ( box ) {
164
165 if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
166 ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) ) {
167
168 return true;
169
170 }
171
172 return false;
173
174 },
175
176 getParameter: function ( point, optionalTarget ) {
177
178 // This can potentially have a divide by zero if the box
179 // has a size dimension of 0.
180
181 var result = optionalTarget || new THREE.Vector2();
182
183 return result.set(
184 ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
185 ( point.y - this.min.y ) / ( this.max.y - this.min.y )
186 );
187
188 },
189
190 isIntersectionBox: function ( box ) {
191
192 // using 6 splitting planes to rule out intersections.
193
194 if ( box.max.x < this.min.x || box.min.x > this.max.x ||
195 box.max.y < this.min.y || box.min.y > this.max.y ) {
196
197 return false;
198
199 }
200
201 return true;
202
203 },
204
205 clampPoint: function ( point, optionalTarget ) {
206
207 var result = optionalTarget || new THREE.Vector2();
208 return result.copy( point ).clamp( this.min, this.max );
209
210 },
211
212 distanceToPoint: function () {
213
214 var v1 = new THREE.Vector2();
215
216 return function ( point ) {
217
218 var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
219 return clampedPoint.sub( point ).length();
220
221 };
222
223 }(),
224
225 intersect: function ( box ) {
226
227 this.min.max( box.min );
228 this.max.min( box.max );
229
230 return this;
231
232 },
233
234 union: function ( box ) {
235
236 this.min.min( box.min );
237 this.max.max( box.max );
238
239 return this;
240
241 },
242
243 translate: function ( offset ) {
244
245 this.min.add( offset );
246 this.max.add( offset );
247
248 return this;
249
250 },
251
252 equals: function ( box ) {
253
254 return box.min.equals( this.min ) && box.max.equals( this.max );
255
256 },
257
258 clone: function () {
259
260 return new THREE.Box2().copy( this );
261
262 }
263
264};
Note: See TracBrowser for help on using the repository browser.