changeset 812:b484631f37c1 jquery

better intersect function
author hertzhaft
date Mon, 21 Feb 2011 01:00:26 +0100
parents 136fc3320f50
children 1a7b14deae3a
files client/digitallibrary/jquery/jquery.digilib.geometry.js
diffstat 1 files changed, 7 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/client/digitallibrary/jquery/jquery.digilib.geometry.js	Mon Feb 21 00:57:32 2011 +0100
+++ b/client/digitallibrary/jquery/jquery.digilib.geometry.js	Mon Feb 21 01:00:26 2011 +0100
@@ -236,18 +236,13 @@
         };
         // returns true if rectangle "rect" and this rectangle overlap
         that.overlapsRect = function(rect) {
-            return (this.containsPosition(rect.getPt1()) || this
-                    .containsPosition(rect.getPt2()));
+            return this.intersect(rect) != null;
         };
         // changes this rectangle's x/y values so it stays inside of rectangle
         // "rect", keeping the proportions
         that.stayInside = function(rect) {
-            if (this.x < rect.x) {
-                this.x = rect.x;
-            }
-            if (this.y < rect.y) {
-                this.y = rect.y;
-            }
+            this.x = Math.max(this.x, rect.x);
+            this.y = Math.max(this.y, rect.y);
             if (this.x + this.width > rect.x + rect.width) {
                 this.x = rect.x + rect.width - this.width;
             }
@@ -267,24 +262,11 @@
         };
         // returns the intersection of rectangle "rect" and this one
         that.intersect = function(rect) {
-            // FIX ME: not really, it should return null if there is no overlap
-            var sec = rect.copy();
-            if (sec.x < this.x) {
-                sec.width = sec.width - (this.x - sec.x);
-                sec.x = this.x;
-            }
-            if (sec.y < this.y) {
-                sec.height = sec.height - (this.y - sec.y);
-                sec.y = this.y;
-            }
-            if (sec.x + sec.width > this.x + this.width) {
-                sec.width = (this.x + this.width) - sec.x;
-            }
-            if (sec.y + sec.height > this.y + this.height) {
-                sec.height = (this.y + this.height) - sec.y;
-            }
-            return sec;
+            var res = rect.clipTo(this);
+            if (res.width < 0 || res.height < 0) res = null;
+            return res;
         };
+
         // returns a copy of rectangle "rect" that fits into this one
         // (moving it first)
         that.fit = function(rect) {