Browse Source

added GetRectangleOverlap()

pull/4771/head
fmixolydian 5 days ago
parent
commit
b94cf4daca
2 changed files with 31 additions and 0 deletions
  1. +1
    -0
      src/raylib.h
  2. +30
    -0
      src/rshapes.c

+ 1
- 0
src/raylib.h View File

@ -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)

+ 30
- 0
src/rshapes.c View File

@ -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
//----------------------------------------------------------------------------------

Loading…
Cancel
Save