Path: blob/master/thirdparty/sdl/include/SDL3/SDL_rect.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* # CategoryRect23*24* Some helper functions for managing rectangles and 2D points, in both25* integer and floating point versions.26*/2728#ifndef SDL_rect_h_29#define SDL_rect_h_3031#include <SDL3/SDL_stdinc.h>32#include <SDL3/SDL_error.h>3334#include <SDL3/SDL_begin_code.h>35/* Set up for C function definitions, even when using C++ */36#ifdef __cplusplus37extern "C" {38#endif3940/**41* The structure that defines a point (using integers).42*43* \since This struct is available since SDL 3.2.0.44*45* \sa SDL_GetRectEnclosingPoints46* \sa SDL_PointInRect47*/48typedef struct SDL_Point49{50int x;51int y;52} SDL_Point;5354/**55* The structure that defines a point (using floating point values).56*57* \since This struct is available since SDL 3.2.0.58*59* \sa SDL_GetRectEnclosingPointsFloat60* \sa SDL_PointInRectFloat61*/62typedef struct SDL_FPoint63{64float x;65float y;66} SDL_FPoint;676869/**70* A rectangle, with the origin at the upper left (using integers).71*72* \since This struct is available since SDL 3.2.0.73*74* \sa SDL_RectEmpty75* \sa SDL_RectsEqual76* \sa SDL_HasRectIntersection77* \sa SDL_GetRectIntersection78* \sa SDL_GetRectAndLineIntersection79* \sa SDL_GetRectUnion80* \sa SDL_GetRectEnclosingPoints81*/82typedef struct SDL_Rect83{84int x, y;85int w, h;86} SDL_Rect;878889/**90* A rectangle, with the origin at the upper left (using floating point91* values).92*93* \since This struct is available since SDL 3.2.0.94*95* \sa SDL_RectEmptyFloat96* \sa SDL_RectsEqualFloat97* \sa SDL_RectsEqualEpsilon98* \sa SDL_HasRectIntersectionFloat99* \sa SDL_GetRectIntersectionFloat100* \sa SDL_GetRectAndLineIntersectionFloat101* \sa SDL_GetRectUnionFloat102* \sa SDL_GetRectEnclosingPointsFloat103* \sa SDL_PointInRectFloat104*/105typedef struct SDL_FRect106{107float x;108float y;109float w;110float h;111} SDL_FRect;112113114/**115* Convert an SDL_Rect to SDL_FRect116*117* \param rect a pointer to an SDL_Rect.118* \param frect a pointer filled in with the floating point representation of119* `rect`.120*121* \threadsafety It is safe to call this function from any thread.122*123* \since This function is available since SDL 3.2.0.124*/125SDL_FORCE_INLINE void SDL_RectToFRect(const SDL_Rect *rect, SDL_FRect *frect)126{127frect->x = (float)rect->x;128frect->y = (float)rect->y;129frect->w = (float)rect->w;130frect->h = (float)rect->h;131}132133/**134* Determine whether a point resides inside a rectangle.135*136* A point is considered part of a rectangle if both `p` and `r` are not NULL,137* and `p`'s x and y coordinates are >= to the rectangle's top left corner,138* and < the rectangle's x+w and y+h. So a 1x1 rectangle considers point (0,0)139* as "inside" and (0,1) as not.140*141* Note that this is a forced-inline function in a header, and not a public142* API function available in the SDL library (which is to say, the code is143* embedded in the calling program and the linker and dynamic loader will not144* be able to find this function inside SDL itself).145*146* \param p the point to test.147* \param r the rectangle to test.148* \returns true if `p` is contained by `r`, false otherwise.149*150* \threadsafety It is safe to call this function from any thread.151*152* \since This function is available since SDL 3.2.0.153*/154SDL_FORCE_INLINE bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)155{156return ( p && r && (p->x >= r->x) && (p->x < (r->x + r->w)) &&157(p->y >= r->y) && (p->y < (r->y + r->h)) ) ? true : false;158}159160/**161* Determine whether a rectangle has no area.162*163* A rectangle is considered "empty" for this function if `r` is NULL, or if164* `r`'s width and/or height are <= 0.165*166* Note that this is a forced-inline function in a header, and not a public167* API function available in the SDL library (which is to say, the code is168* embedded in the calling program and the linker and dynamic loader will not169* be able to find this function inside SDL itself).170*171* \param r the rectangle to test.172* \returns true if the rectangle is "empty", false otherwise.173*174* \threadsafety It is safe to call this function from any thread.175*176* \since This function is available since SDL 3.2.0.177*/178SDL_FORCE_INLINE bool SDL_RectEmpty(const SDL_Rect *r)179{180return ((!r) || (r->w <= 0) || (r->h <= 0)) ? true : false;181}182183/**184* Determine whether two rectangles are equal.185*186* Rectangles are considered equal if both are not NULL and each of their x,187* y, width and height match.188*189* Note that this is a forced-inline function in a header, and not a public190* API function available in the SDL library (which is to say, the code is191* embedded in the calling program and the linker and dynamic loader will not192* be able to find this function inside SDL itself).193*194* \param a the first rectangle to test.195* \param b the second rectangle to test.196* \returns true if the rectangles are equal, false otherwise.197*198* \threadsafety It is safe to call this function from any thread.199*200* \since This function is available since SDL 3.2.0.201*/202SDL_FORCE_INLINE bool SDL_RectsEqual(const SDL_Rect *a, const SDL_Rect *b)203{204return (a && b && (a->x == b->x) && (a->y == b->y) &&205(a->w == b->w) && (a->h == b->h)) ? true : false;206}207208/**209* Determine whether two rectangles intersect.210*211* If either pointer is NULL the function will return false.212*213* \param A an SDL_Rect structure representing the first rectangle.214* \param B an SDL_Rect structure representing the second rectangle.215* \returns true if there is an intersection, false otherwise.216*217* \threadsafety It is safe to call this function from any thread.218*219* \since This function is available since SDL 3.2.0.220*221* \sa SDL_GetRectIntersection222*/223extern SDL_DECLSPEC bool SDLCALL SDL_HasRectIntersection(const SDL_Rect *A, const SDL_Rect *B);224225/**226* Calculate the intersection of two rectangles.227*228* If `result` is NULL then this function will return false.229*230* \param A an SDL_Rect structure representing the first rectangle.231* \param B an SDL_Rect structure representing the second rectangle.232* \param result an SDL_Rect structure filled in with the intersection of233* rectangles `A` and `B`.234* \returns true if there is an intersection, false otherwise.235*236* \since This function is available since SDL 3.2.0.237*238* \sa SDL_HasRectIntersection239*/240extern SDL_DECLSPEC bool SDLCALL SDL_GetRectIntersection(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result);241242/**243* Calculate the union of two rectangles.244*245* \param A an SDL_Rect structure representing the first rectangle.246* \param B an SDL_Rect structure representing the second rectangle.247* \param result an SDL_Rect structure filled in with the union of rectangles248* `A` and `B`.249* \returns true on success or false on failure; call SDL_GetError() for more250* information.251*252* \since This function is available since SDL 3.2.0.253*/254extern SDL_DECLSPEC bool SDLCALL SDL_GetRectUnion(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result);255256/**257* Calculate a minimal rectangle enclosing a set of points.258*259* If `clip` is not NULL then only points inside of the clipping rectangle are260* considered.261*262* \param points an array of SDL_Point structures representing points to be263* enclosed.264* \param count the number of structures in the `points` array.265* \param clip an SDL_Rect used for clipping or NULL to enclose all points.266* \param result an SDL_Rect structure filled in with the minimal enclosing267* rectangle.268* \returns true if any points were enclosed or false if all the points were269* outside of the clipping rectangle.270*271* \since This function is available since SDL 3.2.0.272*/273extern SDL_DECLSPEC bool SDLCALL SDL_GetRectEnclosingPoints(const SDL_Point *points, int count, const SDL_Rect *clip, SDL_Rect *result);274275/**276* Calculate the intersection of a rectangle and line segment.277*278* This function is used to clip a line segment to a rectangle. A line segment279* contained entirely within the rectangle or that does not intersect will280* remain unchanged. A line segment that crosses the rectangle at either or281* both ends will be clipped to the boundary of the rectangle and the new282* coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.283*284* \param rect an SDL_Rect structure representing the rectangle to intersect.285* \param X1 a pointer to the starting X-coordinate of the line.286* \param Y1 a pointer to the starting Y-coordinate of the line.287* \param X2 a pointer to the ending X-coordinate of the line.288* \param Y2 a pointer to the ending Y-coordinate of the line.289* \returns true if there is an intersection, false otherwise.290*291* \since This function is available since SDL 3.2.0.292*/293extern SDL_DECLSPEC bool SDLCALL SDL_GetRectAndLineIntersection(const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2);294295296/* SDL_FRect versions... */297298/**299* Determine whether a point resides inside a floating point rectangle.300*301* A point is considered part of a rectangle if both `p` and `r` are not NULL,302* and `p`'s x and y coordinates are >= to the rectangle's top left corner,303* and <= the rectangle's x+w and y+h. So a 1x1 rectangle considers point304* (0,0) and (0,1) as "inside" and (0,2) as not.305*306* Note that this is a forced-inline function in a header, and not a public307* API function available in the SDL library (which is to say, the code is308* embedded in the calling program and the linker and dynamic loader will not309* be able to find this function inside SDL itself).310*311* \param p the point to test.312* \param r the rectangle to test.313* \returns true if `p` is contained by `r`, false otherwise.314*315* \threadsafety It is safe to call this function from any thread.316*317* \since This function is available since SDL 3.2.0.318*/319SDL_FORCE_INLINE bool SDL_PointInRectFloat(const SDL_FPoint *p, const SDL_FRect *r)320{321return ( p && r && (p->x >= r->x) && (p->x <= (r->x + r->w)) &&322(p->y >= r->y) && (p->y <= (r->y + r->h)) ) ? true : false;323}324325/**326* Determine whether a floating point rectangle can contain any point.327*328* A rectangle is considered "empty" for this function if `r` is NULL, or if329* `r`'s width and/or height are < 0.0f.330*331* Note that this is a forced-inline function in a header, and not a public332* API function available in the SDL library (which is to say, the code is333* embedded in the calling program and the linker and dynamic loader will not334* be able to find this function inside SDL itself).335*336* \param r the rectangle to test.337* \returns true if the rectangle is "empty", false otherwise.338*339* \threadsafety It is safe to call this function from any thread.340*341* \since This function is available since SDL 3.2.0.342*/343SDL_FORCE_INLINE bool SDL_RectEmptyFloat(const SDL_FRect *r)344{345return ((!r) || (r->w < 0.0f) || (r->h < 0.0f)) ? true : false;346}347348/**349* Determine whether two floating point rectangles are equal, within some350* given epsilon.351*352* Rectangles are considered equal if both are not NULL and each of their x,353* y, width and height are within `epsilon` of each other. If you don't know354* what value to use for `epsilon`, you should call the SDL_RectsEqualFloat355* function instead.356*357* Note that this is a forced-inline function in a header, and not a public358* API function available in the SDL library (which is to say, the code is359* embedded in the calling program and the linker and dynamic loader will not360* be able to find this function inside SDL itself).361*362* \param a the first rectangle to test.363* \param b the second rectangle to test.364* \param epsilon the epsilon value for comparison.365* \returns true if the rectangles are equal, false otherwise.366*367* \threadsafety It is safe to call this function from any thread.368*369* \since This function is available since SDL 3.2.0.370*371* \sa SDL_RectsEqualFloat372*/373SDL_FORCE_INLINE bool SDL_RectsEqualEpsilon(const SDL_FRect *a, const SDL_FRect *b, float epsilon)374{375return (a && b && ((a == b) ||376((SDL_fabsf(a->x - b->x) <= epsilon) &&377(SDL_fabsf(a->y - b->y) <= epsilon) &&378(SDL_fabsf(a->w - b->w) <= epsilon) &&379(SDL_fabsf(a->h - b->h) <= epsilon))))380? true : false;381}382383/**384* Determine whether two floating point rectangles are equal, within a default385* epsilon.386*387* Rectangles are considered equal if both are not NULL and each of their x,388* y, width and height are within SDL_FLT_EPSILON of each other. This is often389* a reasonable way to compare two floating point rectangles and deal with the390* slight precision variations in floating point calculations that tend to pop391* up.392*393* Note that this is a forced-inline function in a header, and not a public394* API function available in the SDL library (which is to say, the code is395* embedded in the calling program and the linker and dynamic loader will not396* be able to find this function inside SDL itself).397*398* \param a the first rectangle to test.399* \param b the second rectangle to test.400* \returns true if the rectangles are equal, false otherwise.401*402* \threadsafety It is safe to call this function from any thread.403*404* \since This function is available since SDL 3.2.0.405*406* \sa SDL_RectsEqualEpsilon407*/408SDL_FORCE_INLINE bool SDL_RectsEqualFloat(const SDL_FRect *a, const SDL_FRect *b)409{410return SDL_RectsEqualEpsilon(a, b, SDL_FLT_EPSILON);411}412413/**414* Determine whether two rectangles intersect with float precision.415*416* If either pointer is NULL the function will return false.417*418* \param A an SDL_FRect structure representing the first rectangle.419* \param B an SDL_FRect structure representing the second rectangle.420* \returns true if there is an intersection, false otherwise.421*422* \since This function is available since SDL 3.2.0.423*424* \sa SDL_GetRectIntersection425*/426extern SDL_DECLSPEC bool SDLCALL SDL_HasRectIntersectionFloat(const SDL_FRect *A, const SDL_FRect *B);427428/**429* Calculate the intersection of two rectangles with float precision.430*431* If `result` is NULL then this function will return false.432*433* \param A an SDL_FRect structure representing the first rectangle.434* \param B an SDL_FRect structure representing the second rectangle.435* \param result an SDL_FRect structure filled in with the intersection of436* rectangles `A` and `B`.437* \returns true if there is an intersection, false otherwise.438*439* \since This function is available since SDL 3.2.0.440*441* \sa SDL_HasRectIntersectionFloat442*/443extern SDL_DECLSPEC bool SDLCALL SDL_GetRectIntersectionFloat(const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result);444445/**446* Calculate the union of two rectangles with float precision.447*448* \param A an SDL_FRect structure representing the first rectangle.449* \param B an SDL_FRect structure representing the second rectangle.450* \param result an SDL_FRect structure filled in with the union of rectangles451* `A` and `B`.452* \returns true on success or false on failure; call SDL_GetError() for more453* information.454*455* \since This function is available since SDL 3.2.0.456*/457extern SDL_DECLSPEC bool SDLCALL SDL_GetRectUnionFloat(const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result);458459/**460* Calculate a minimal rectangle enclosing a set of points with float461* precision.462*463* If `clip` is not NULL then only points inside of the clipping rectangle are464* considered.465*466* \param points an array of SDL_FPoint structures representing points to be467* enclosed.468* \param count the number of structures in the `points` array.469* \param clip an SDL_FRect used for clipping or NULL to enclose all points.470* \param result an SDL_FRect structure filled in with the minimal enclosing471* rectangle.472* \returns true if any points were enclosed or false if all the points were473* outside of the clipping rectangle.474*475* \since This function is available since SDL 3.2.0.476*/477extern SDL_DECLSPEC bool SDLCALL SDL_GetRectEnclosingPointsFloat(const SDL_FPoint *points, int count, const SDL_FRect *clip, SDL_FRect *result);478479/**480* Calculate the intersection of a rectangle and line segment with float481* precision.482*483* This function is used to clip a line segment to a rectangle. A line segment484* contained entirely within the rectangle or that does not intersect will485* remain unchanged. A line segment that crosses the rectangle at either or486* both ends will be clipped to the boundary of the rectangle and the new487* coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.488*489* \param rect an SDL_FRect structure representing the rectangle to intersect.490* \param X1 a pointer to the starting X-coordinate of the line.491* \param Y1 a pointer to the starting Y-coordinate of the line.492* \param X2 a pointer to the ending X-coordinate of the line.493* \param Y2 a pointer to the ending Y-coordinate of the line.494* \returns true if there is an intersection, false otherwise.495*496* \since This function is available since SDL 3.2.0.497*/498extern SDL_DECLSPEC bool SDLCALL SDL_GetRectAndLineIntersectionFloat(const SDL_FRect *rect, float *X1, float *Y1, float *X2, float *Y2);499500/* Ends C function definitions when using C++ */501#ifdef __cplusplus502}503#endif504#include <SDL3/SDL_close_code.h>505506#endif /* SDL_rect_h_ */507508509