OSDN Git Service

Add intersectRectangles to Intersector
authorJustin Shapcott <support@mobidevelop.com>
Thu, 10 Oct 2013 21:18:27 +0000 (14:18 -0700)
committerJustin Shapcott <support@mobidevelop.com>
Thu, 10 Oct 2013 21:18:27 +0000 (14:18 -0700)
Closes #823.

gdx/src/com/badlogic/gdx/math/Intersector.java

index 50e9355..3f001a0 100644 (file)
@@ -720,6 +720,22 @@ public final class Intersector {
                return false;
        }
 
+       /** Determines whether the given rectangles intersect and, if they do,
+        *  sets the supplied {@code intersection} rectangle to the area of overlap.
+        * 
+        * @return Whether the rectangles intersect
+        */
+       static public boolean intersectRectangles(Rectangle rectangle1, Rectangle rectangle2, Rectangle intersection) {
+           if (rectangle1.overlaps(rectangle2)) {
+               intersection.x = Math.max(rectangle1.x, rectangle2.x);
+               intersection.width = Math.min(rectangle1.x + rectangle1.width, rectangle2.x + rectangle2.width) - intersection.x;
+               intersection.y = Math.max(rectangle1.y, rectangle2.y);
+               intersection.height = Math.min(rectangle1.y + rectangle1.height, rectangle2.y + rectangle2.height) - intersection.y;
+               return true;
+           }
+           return false;
+       }       
+       
        /** Check whether the given line segment and {@link Polygon} intersect.
         * @param p1 The first point of the segment
         * @param p2 The second point of the segment