diff --git a/src/raylib.h b/src/raylib.h index 791e16c00..398202373 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1318,6 +1318,7 @@ RLAPI bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int th RLAPI bool CheckCollisionPointPoly(Vector2 point, const Vector2 *points, int pointCount); // Check if point is within a polygon described by array of vertices RLAPI bool CheckCollisionLines(Vector2 startPos1, Vector2 endPos1, Vector2 startPos2, Vector2 endPos2, Vector2 *collisionPoint); // Check the collision between two lines defined by two points each, returns collision point by reference RLAPI Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2); // Get collision rectangle for two rectangles collision +RLAPI Rectangle GetRectangleOverlap(Rectangle a, Rectangle b); // Get smallest rectangle entirely overlapping both rectangles //------------------------------------------------------------------------------------ // Texture Loading and Drawing Functions (Module: textures) diff --git a/src/rshapes.c b/src/rshapes.c index 704d74f40..caf8d96be 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -2405,6 +2405,36 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) return overlap; } +// Get smallest rectangle entirely overlapping both rectangles +Rectangle GetRectangleOverlap(Rectangle a, Rectangle b) { + + Rectangle overlap; + Rectangle left, right, top, bottom; + + if (a.x == b.x) { + left = (a.width == fmin(a.width, b.width)) ? a : b; + right = (a.width == fmax(a.width, b.width)) ? a : b; + } else { + left = (a.x < b.x) ? a : b; + right = (a.x > b.x) ? a : b; + } + + if (a.y == b.y) { + top = (a.height == fmin(a.height, b.height)) ? a : b; + bottom = (a.height == fmax(a.height, b.height)) ? a : b; + } else { + top = (a.y < b.y) ? a : b; + bottom = (a.y > b.y) ? a : b; + } + + overlap.x = fmin(a.x, b.x); + overlap.y = fmin(a.y, b.y); + overlap.width = fmax(left.width + (right.x - (left.x + left.width) + right.width), left.width); + overlap.height = fmax(top.height + (bottom.y - (top.y + top.height) + bottom.height), top.height); + + return overlap; +} + //---------------------------------------------------------------------------------- // Module specific Functions Definition //----------------------------------------------------------------------------------