Mercurial > hg > digilib-old
comparison client/digitallibrary/jquery/jquery.digilib.geometry.js @ 812:b484631f37c1 jquery
better intersect function
author | hertzhaft |
---|---|
date | Mon, 21 Feb 2011 01:00:26 +0100 |
parents | 1b1728926534 |
children | c662bf335881 |
comparison
equal
deleted
inserted
replaced
811:136fc3320f50 | 812:b484631f37c1 |
---|---|
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 | 237 // returns true if rectangle "rect" and this rectangle overlap |
238 that.overlapsRect = function(rect) { | 238 that.overlapsRect = function(rect) { |
239 return (this.containsPosition(rect.getPt1()) || this | 239 return this.intersect(rect) != null; |
240 .containsPosition(rect.getPt2())); | |
241 }; | 240 }; |
242 // changes this rectangle's x/y values so it stays inside of rectangle | 241 // changes this rectangle's x/y values so it stays inside of rectangle |
243 // "rect", keeping the proportions | 242 // "rect", keeping the proportions |
244 that.stayInside = function(rect) { | 243 that.stayInside = function(rect) { |
245 if (this.x < rect.x) { | 244 this.x = Math.max(this.x, rect.x); |
246 this.x = rect.x; | 245 this.y = Math.max(this.y, rect.y); |
247 } | |
248 if (this.y < rect.y) { | |
249 this.y = rect.y; | |
250 } | |
251 if (this.x + this.width > rect.x + rect.width) { | 246 if (this.x + this.width > rect.x + rect.width) { |
252 this.x = rect.x + rect.width - this.width; | 247 this.x = rect.x + rect.width - this.width; |
253 } | 248 } |
254 if (this.y + this.height > rect.y + rect.height) { | 249 if (this.y + this.height > rect.y + rect.height) { |
255 this.y = rect.y + rect.height - this.height; | 250 this.y = rect.y + rect.height - this.height; |
265 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y))); | 260 this.setPt2(position(Math.min(this2.x, p2.x), Math.min(this2.y, p2.y))); |
266 return this; | 261 return this; |
267 }; | 262 }; |
268 // returns the intersection of rectangle "rect" and this one | 263 // returns the intersection of rectangle "rect" and this one |
269 that.intersect = function(rect) { | 264 that.intersect = function(rect) { |
270 // FIX ME: not really, it should return null if there is no overlap | 265 var res = rect.clipTo(this); |
271 var sec = rect.copy(); | 266 if (res.width < 0 || res.height < 0) res = null; |
272 if (sec.x < this.x) { | 267 return res; |
273 sec.width = sec.width - (this.x - sec.x); | 268 }; |
274 sec.x = this.x; | 269 |
275 } | |
276 if (sec.y < this.y) { | |
277 sec.height = sec.height - (this.y - sec.y); | |
278 sec.y = this.y; | |
279 } | |
280 if (sec.x + sec.width > this.x + this.width) { | |
281 sec.width = (this.x + this.width) - sec.x; | |
282 } | |
283 if (sec.y + sec.height > this.y + this.height) { | |
284 sec.height = (this.y + this.height) - sec.y; | |
285 } | |
286 return sec; | |
287 }; | |
288 // returns a copy of rectangle "rect" that fits into this one | 270 // returns a copy of rectangle "rect" that fits into this one |
289 // (moving it first) | 271 // (moving it first) |
290 that.fit = function(rect) { | 272 that.fit = function(rect) { |
291 var sec = rect.copy(); | 273 var sec = rect.copy(); |
292 sec.x = Math.max(sec.x, this.x); | 274 sec.x = Math.max(sec.x, this.x); |