Răsfoiți Sursa

Merge pull request #309 from dgough/master

Fixed Rectangle::intersects()
Sean Paul Taylor 13 ani în urmă
părinte
comite
e0e487422d
2 a modificat fișierele cu 7 adăugiri și 6 ștergeri
  1. 6 6
      gameplay/src/Rectangle.cpp
  2. 1 0
      gameplay/src/Rectangle.h

+ 6 - 6
gameplay/src/Rectangle.cpp

@@ -95,12 +95,12 @@ bool Rectangle::contains(const Rectangle& r) const
 
 bool Rectangle::intersects(float x, float y, float width, float height) const
 {
-    const float left   = max(this->x, x);
-    const float top    = max(this->y, y);
-    const float right  = min(x + width, x + width);
-    const float bottom = min(y + height, y + height);
-
-    return (right > left && bottom > top);
+    float t;
+    if ((t = x - this->x) > this->width || -t > width)
+        return false;
+    if ((t = y - this->y) > this->height || -t > height)
+        return false;
+    return true;
 }
 
 bool Rectangle::intersects(const Rectangle& r) const

+ 1 - 0
gameplay/src/Rectangle.h

@@ -169,6 +169,7 @@ public:
 
     /**
      * Determines whether a specified rectangle intersects with this rectangle.
+     * Rectangles intersect if there is a common point that is contained in both rectangles.
      *
      * @param x The x-coordinate of the rectangle.
      * @param y The y-coordinate of the rectangle.