changeset 1698:c3db126434e9

reimplement Rectangle::intersectsWith()
author Johannes Hofmann <Johannes.Hofmann@gmx.de>
date Fri, 27 Aug 2010 12:31:18 +0200
parents 6eb8f4815460
children f4a6b351012d
files dw/types.cc
diffstat 1 files changed, 15 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/dw/types.cc	Wed Aug 25 13:05:12 2010 +0200
+++ b/dw/types.cc	Fri Aug 27 12:31:18 2010 +0200
@@ -1,7 +1,3 @@
-// Rectangle::intersectsWith() has code that was derived from gdkrectangle.c.
-// gdkrectangle.c bears the notice that GDK, the GIMP Drawing Kit, is
-// "Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald".
-
 /*
  * Dillo Widget
  *
@@ -56,55 +52,24 @@
  */
 bool Rectangle::intersectsWith (Rectangle *otherRect, Rectangle *dest)
 {
-   Rectangle *src1 = this, *src2 = otherRect, *temp;
-   int src1_x2, src1_y2;
-   int src2_x2, src2_y2;
-   bool return_val;
-
-   return_val = false;
-
-   if (src2->x < src1->x) {
-      temp = src1;
-      src1 = src2;
-      src2 = temp;
-   }
-   dest->x = src2->x;
-
-   src1_x2 = src1->x + src1->width;
-   src2_x2 = src2->x + src2->width;
-
-   if (src2->x < src1_x2) {
-      if (src1_x2 < src2_x2)
-         dest->width = src1_x2 - dest->x;
-      else
-         dest->width = src2_x2 - dest->x;
+   bool doIntersect =
+      this->x < otherRect->x + otherRect->width &&
+      this->y < otherRect->y + otherRect->height &&
+      otherRect->x < this->x + this->width &&
+      otherRect->y < this->y + this->height;
 
-      if (src2->y < src1->y) {
-         temp = src1;
-         src1 = src2;
-         src2 = temp;
-      }
-      dest->y = src2->y;
-
-      src1_y2 = src1->y + src1->height;
-      src2_y2 = src2->y + src2->height;
-
-      if (src2->y < src1_y2) {
-         return_val = true;
-
-         if (src1_y2 < src2_y2)
-            dest->height = src1_y2 - dest->y;
-         else
-            dest->height = src2_y2 - dest->y;
-
-         if (dest->height == 0)
-            return_val = false;
-         if (dest->width == 0)
-            return_val = false;
-      }
+   if (doIntersect) {
+      dest->x = misc::max(this->x, otherRect->x);
+      dest->y = misc::max(this->y, otherRect->y);
+      dest->width  = misc::min(this->x + this->width,
+                               otherRect->x + otherRect->width) - dest->x;
+      dest->height = misc::min(this->y + this->height,
+                               otherRect->y + otherRect->height) - dest->y;
+   } else {
+      dest->x = dest->y = dest->width = dest->height = 0;
    }
 
-   return return_val;
+   return doIntersect; 
 }
 
 /*