Update GetCollisionRec

Replace macros with ternary operators
This commit is contained in:
manuel5975p 2023-05-10 13:03:34 +02:00 committed by GitHub
parent cb2239f2f3
commit 81185bf85a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1757,31 +1757,30 @@ bool CheckCollisionPointLine(Vector2 point, Vector2 p1, Vector2 p2, int threshol
} }
// Get collision rectangle for two rectangles collision // Get collision rectangle for two rectangles collision
#define MIN_MACRO(X,Y) ((X) < (Y) ? (X) : (Y)) //Define ugly macros to get both gcc and clang Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2){
#define MAX_MACRO(X,Y) ((X) > (Y) ? (X) : (Y)) //to reduce min and max to single instructions
Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) {
Rectangle overlap; Rectangle overlap;
float left = ((rec1.x) > (rec2.x) ? (rec1.x) : (rec2.x));
float left = MAX_MACRO(rec1.x, rec2.x); float right1 = rec1.x + rec1.width;
float right = MIN_MACRO(rec1.x + rec1.width, rec2.x + rec2.width); float right2 = rec2.x + rec2.width;
float top = MAX_MACRO(rec1.y, rec2.y); float right = ((right1) < (right2) ? (right1) : (right2));
float bottom = MIN_MACRO(rec1.y + rec1.height, rec2.y + rec2.height); float top = ((rec1.y) > (rec2.y) ? (rec1.y) : (rec2.y));
if (left < right && top < bottom) { float bottom1 = rec1.y + rec1.height;
overlap.x = left; float bottom2 = rec2.y + rec2.height;
overlap.y = top; float bottom = ((bottom1) < (bottom2) ? (bottom1) : (bottom2));
overlap.width = right - left; if (left < right && top < bottom){
overlap.height = bottom - top; overlap.x = (left);
} else { overlap.y = (top);
overlap.width = (right - left );
overlap.height = (bottom - top);
}
else{
overlap.x = 0; overlap.x = 0;
overlap.y = 0; overlap.y = 0;
overlap.width = 0; overlap.width = 0;
overlap.height = 0; overlap.height = 0;
} }
return overlap; return overlap;
} }
#undef MIN_MACRO //Undefine macros again
#undef MAX_MACRO
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module specific Functions Definition // Module specific Functions Definition