Mercurial > hg > digilib
comparison client/digitallibrary/jquery/jquery.digilib.geometry.js @ 800:ac1d6b056a6f jquery
geometry: overlapsRect() plus some comment clarifications
author | hertzhaft |
---|---|
date | Sun, 20 Feb 2011 11:55:51 +0100 |
parents | 912519475259 |
children | 1f9940d4bd35 |
comparison
equal
deleted
inserted
replaced
796:de7bc4c9a4c0 | 800:ac1d6b056a6f |
---|---|
176 that.setPosition = function(pos) { | 176 that.setPosition = function(pos) { |
177 this.x = pos.x; | 177 this.x = pos.x; |
178 this.y = pos.y; | 178 this.y = pos.y; |
179 return this; | 179 return this; |
180 }; | 180 }; |
181 // adds pos to the position | |
181 that.setPt1 = that.setPosition; // TODO: not really the same | 182 that.setPt1 = that.setPosition; // TODO: not really the same |
182 // adds pos to the position | |
183 that.addPosition = function(pos) { | 183 that.addPosition = function(pos) { |
184 this.x += pos.x; | 184 this.x += pos.x; |
185 this.y += pos.y; | 185 this.y += pos.y; |
186 return this; | 186 return this; |
187 }; | 187 }; |
196 return position({ | 196 return position({ |
197 x : this.x + this.width / 2, | 197 x : this.x + this.width / 2, |
198 y : this.y + this.height / 2 | 198 y : this.y + this.height / 2 |
199 }); | 199 }); |
200 }; | 200 }; |
201 // moves this Rectangle's center to position pos | 201 // moves this rectangle's center to position pos |
202 that.setCenter = function(pos) { | 202 that.setCenter = function(pos) { |
203 this.x = pos.x - this.width / 2; | 203 this.x = pos.x - this.width / 2; |
204 this.y = pos.y - this.height / 2; | 204 this.y = pos.y - this.height / 2; |
205 return this; | 205 return this; |
206 }; | 206 }; |
207 // returns true if both rectangles have equal position and proportion | |
207 that.equals = function(other) { | 208 that.equals = function(other) { |
208 // equal props | |
209 var eq = (this.x === other.x && this.y === other.y && this.width === other.width); | 209 var eq = (this.x === other.x && this.y === other.y && this.width === other.width); |
210 return eq; | 210 return eq; |
211 }; | 211 }; |
212 // returns the area of this Rectangle | 212 // returns the area of this Rectangle |
213 that.getArea = function() { | 213 that.getArea = function() { |
227 var ct = ((pos.x >= this.x) && (pos.y >= this.y) | 227 var ct = ((pos.x >= this.x) && (pos.y >= this.y) |
228 && (pos.x <= this.x + this.width) && (pos.y <= this.y | 228 && (pos.x <= this.x + this.width) && (pos.y <= this.y |
229 + this.height)); | 229 + this.height)); |
230 return ct; | 230 return ct; |
231 }; | 231 }; |
232 // returns if rectangle "rect" is contained in this rectangle | 232 // returns true if rectangle "rect" is contained in this rectangle |
233 that.containsRect = function(rect) { | 233 that.containsRect = function(rect) { |
234 return (this.containsPosition(rect.getPt1()) && this | 234 return (this.containsPosition(rect.getPt1()) && this |
235 .containsPosition(rect.getPt2())); | 235 .containsPosition(rect.getPt2())); |
236 }; | 236 }; |
237 // returns true if rectangle "rect" and this rectangle overlap | |
238 that.overlapsRect = function(rect) { | |
239 return (this.containsPosition(rect.getPt1()) || this | |
240 .containsPosition(rect.getPt2())); | |
241 }; | |
237 // changes this rectangle's x/y values so it stays inside of rectangle | 242 // changes this rectangle's x/y values so it stays inside of rectangle |
238 // rect | 243 // "rect", keeping the proportions |
239 // keeping the proportions | |
240 that.stayInside = function(rect) { | 244 that.stayInside = function(rect) { |
241 if (this.x < rect.x) { | 245 if (this.x < rect.x) { |
242 this.x = rect.x; | 246 this.x = rect.x; |
243 } | 247 } |
244 if (this.y < rect.y) { | 248 if (this.y < rect.y) { |
250 if (this.y + this.height > rect.y + rect.height) { | 254 if (this.y + this.height > rect.y + rect.height) { |
251 this.y = rect.y + rect.height - this.height; | 255 this.y = rect.y + rect.height - this.height; |
252 } | 256 } |
253 return this; | 257 return this; |
254 }; | 258 }; |
255 // clips this rectangle so it stays inside of rectangle rect | 259 // clips this rectangle so it stays inside of rectangle "rect" |
256 that.clipTo = function(rect) { | 260 that.clipTo = function(rect) { |
257 var p1 = rect.getPt1(); | 261 var p1 = rect.getPt1(); |
258 var p2 = rect.getPt2(); | 262 var p2 = rect.getPt2(); |
259 var this2 = this.getPt2(); | 263 var this2 = this.getPt2(); |
260 this.setPosition(position(Math.max(this.x, p1.x), Math.max(this.y, p1.y))); | 264 this.setPosition(position(Math.max(this.x, p1.x), Math.max(this.y, p1.y))); |
261 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y))); | 265 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y))); |
262 return this; | 266 return this; |
263 }; | 267 }; |
264 // returns the intersection of the given Rectangle and this one | 268 // returns the intersection of rectangle "rect" and this one |
265 that.intersect = function(rect) { | 269 that.intersect = function(rect) { |
266 // FIX ME: not really, it should return null if there is no overlap | 270 // FIX ME: not really, it should return null if there is no overlap |
267 var sec = rect.copy(); | 271 var sec = rect.copy(); |
268 if (sec.x < this.x) { | 272 if (sec.x < this.x) { |
269 sec.width = sec.width - (this.x - sec.x); | 273 sec.width = sec.width - (this.x - sec.x); |
279 if (sec.y + sec.height > this.y + this.height) { | 283 if (sec.y + sec.height > this.y + this.height) { |
280 sec.height = (this.y + this.height) - sec.y; | 284 sec.height = (this.y + this.height) - sec.y; |
281 } | 285 } |
282 return sec; | 286 return sec; |
283 }; | 287 }; |
284 // returns a Rectangle that fits into this one (by moving first) | 288 // returns a copy of rectangle "rect" that fits into this one |
289 // (moving it first) | |
285 that.fit = function(rect) { | 290 that.fit = function(rect) { |
286 var sec = rect.copy(); | 291 var sec = rect.copy(); |
287 sec.x = Math.max(sec.x, this.x); | 292 sec.x = Math.max(sec.x, this.x); |
288 sec.y = Math.max(sec.y, this.x); | 293 sec.y = Math.max(sec.y, this.x); |
289 if (sec.x + sec.width > this.x + this.width) { | 294 if (sec.x + sec.width > this.x + this.width) { |
292 if (sec.y + sec.height > this.y + this.height) { | 297 if (sec.y + sec.height > this.y + this.height) { |
293 sec.y = this.y + this.height - sec.height; | 298 sec.y = this.y + this.height - sec.height; |
294 } | 299 } |
295 return sec.intersect(this); | 300 return sec.intersect(this); |
296 }; | 301 }; |
297 // adjusts position and size of $elem to this rectangle | 302 // adjusts position and size of jQuery element "$elem" to this rectangle |
298 that.adjustDiv = function($elem) { | 303 that.adjustDiv = function($elem) { |
299 $elem.offset({ | 304 $elem.offset({ |
300 left : this.x, | 305 left : this.x, |
301 top : this.y | 306 top : this.y |
302 }); | 307 }); |
303 $elem.width(this.width).height(this.height); | 308 $elem.width(this.width).height(this.height); |
304 }; | 309 }; |
305 // returns size and position in css-compatible format | 310 // returns position and size of this rectangle in css-compatible format |
306 that.getAsCss = function() { | 311 that.getAsCss = function() { |
307 return { | 312 return { |
308 left : this.x, | 313 left : this.x, |
309 top : this.y, | 314 top : this.y, |
310 width : this.width, | 315 width : this.width, |
311 height : this.height | 316 height : this.height |
312 }; | 317 }; |
313 }; | 318 }; |
319 // returns position and size of this rectangle formatted for SVG attributes | |
320 that.getAsSvg = function() { | |
321 return [this.x, this.y, this.width, this.height].join(" "); | |
322 }; | |
323 // returns size and position of this rectangle formatted for ??? (w x h@x,y) | |
314 that.toString = function() { | 324 that.toString = function() { |
315 return this.width + "x" + this.height + "@" + this.x + "," + this.y; | 325 return this.width + "x" + this.height + "@" + this.x + "," + this.y; |
316 }; | 326 }; |
317 return that; | 327 return that; |
318 }; | 328 }; |