SDL 3.0
SDL_rect.h File Reference

Header file for SDL_rect definition and management functions. More...

#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_pixels.h>
#include <SDL3/SDL_rwops.h>
#include <SDL3/SDL_begin_code.h>
#include <SDL3/SDL_close_code.h>
+ Include dependency graph for SDL_rect.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_Point
 
struct  SDL_FPoint
 
struct  SDL_Rect
 
struct  SDL_FRect
 

Functions

SDL_FORCE_INLINE SDL_bool SDL_PointInRect (const SDL_Point *p, const SDL_Rect *r)
 
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty (const SDL_Rect *r)
 
SDL_FORCE_INLINE SDL_bool SDL_RectsEqual (const SDL_Rect *a, const SDL_Rect *b)
 
SDL_bool SDL_HasRectIntersection (const SDL_Rect *A, const SDL_Rect *B)
 
SDL_bool SDL_GetRectIntersection (const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result)
 
int SDL_GetRectUnion (const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result)
 
SDL_bool SDL_GetRectEnclosingPoints (const SDL_Point *points, int count, const SDL_Rect *clip, SDL_Rect *result)
 
SDL_bool SDL_GetRectAndLineIntersection (const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2)
 
SDL_FORCE_INLINE SDL_bool SDL_PointInRectFloat (const SDL_FPoint *p, const SDL_FRect *r)
 
SDL_FORCE_INLINE SDL_bool SDL_RectEmptyFloat (const SDL_FRect *r)
 
SDL_FORCE_INLINE SDL_bool SDL_RectsEqualEpsilon (const SDL_FRect *a, const SDL_FRect *b, const float epsilon)
 
SDL_FORCE_INLINE SDL_bool SDL_RectsEqualFloat (const SDL_FRect *a, const SDL_FRect *b)
 
SDL_bool SDL_HasRectIntersectionFloat (const SDL_FRect *A, const SDL_FRect *B)
 
SDL_bool SDL_GetRectIntersectionFloat (const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result)
 
int SDL_GetRectUnionFloat (const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result)
 
SDL_bool SDL_GetRectEnclosingPointsFloat (const SDL_FPoint *points, int count, const SDL_FRect *clip, SDL_FRect *result)
 
SDL_bool SDL_GetRectAndLineIntersectionFloat (const SDL_FRect *rect, float *X1, float *Y1, float *X2, float *Y2)
 

Detailed Description

Header file for SDL_rect definition and management functions.

Definition in file SDL_rect.h.

Function Documentation

◆ SDL_GetRectAndLineIntersection()

SDL_bool SDL_GetRectAndLineIntersection ( const SDL_Rect rect,
int *  X1,
int *  Y1,
int *  X2,
int *  Y2 
)
extern

Calculate the intersection of a rectangle and line segment.

This function is used to clip a line segment to a rectangle. A line segment contained entirely within the rectangle or that does not intersect will remain unchanged. A line segment that crosses the rectangle at either or both ends will be clipped to the boundary of the rectangle and the new coordinates saved in X1, Y1, X2, and/or Y2 as necessary.

Parameters
rectan SDL_Rect structure representing the rectangle to intersect
X1a pointer to the starting X-coordinate of the line
Y1a pointer to the starting Y-coordinate of the line
X2a pointer to the ending X-coordinate of the line
Y2a pointer to the ending Y-coordinate of the line
Returns
SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
Since
This function is available since SDL 3.0.0.

◆ SDL_GetRectAndLineIntersectionFloat()

SDL_bool SDL_GetRectAndLineIntersectionFloat ( const SDL_FRect rect,
float *  X1,
float *  Y1,
float *  X2,
float *  Y2 
)
extern

Calculate the intersection of a rectangle and line segment with float precision.

This function is used to clip a line segment to a rectangle. A line segment contained entirely within the rectangle or that does not intersect will remain unchanged. A line segment that crosses the rectangle at either or both ends will be clipped to the boundary of the rectangle and the new coordinates saved in X1, Y1, X2, and/or Y2 as necessary.

Parameters
rectan SDL_FRect structure representing the rectangle to intersect
X1a pointer to the starting X-coordinate of the line
Y1a pointer to the starting Y-coordinate of the line
X2a pointer to the ending X-coordinate of the line
Y2a pointer to the ending Y-coordinate of the line
Returns
SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
Since
This function is available since SDL 3.0.0.

◆ SDL_GetRectEnclosingPoints()

SDL_bool SDL_GetRectEnclosingPoints ( const SDL_Point points,
int  count,
const SDL_Rect clip,
SDL_Rect result 
)
extern

Calculate a minimal rectangle enclosing a set of points.

If clip is not NULL then only points inside of the clipping rectangle are considered.

Parameters
pointsan array of SDL_Point structures representing points to be enclosed
countthe number of structures in the points array
clipan SDL_Rect used for clipping or NULL to enclose all points
resultan SDL_Rect structure filled in with the minimal enclosing rectangle
Returns
SDL_TRUE if any points were enclosed or SDL_FALSE if all the points were outside of the clipping rectangle.
Since
This function is available since SDL 3.0.0.

◆ SDL_GetRectEnclosingPointsFloat()

SDL_bool SDL_GetRectEnclosingPointsFloat ( const SDL_FPoint points,
int  count,
const SDL_FRect clip,
SDL_FRect result 
)
extern

Calculate a minimal rectangle enclosing a set of points with float precision.

If clip is not NULL then only points inside of the clipping rectangle are considered.

Parameters
pointsan array of SDL_FPoint structures representing points to be enclosed
countthe number of structures in the points array
clipan SDL_FRect used for clipping or NULL to enclose all points
resultan SDL_FRect structure filled in with the minimal enclosing rectangle
Returns
SDL_TRUE if any points were enclosed or SDL_FALSE if all the points were outside of the clipping rectangle.
Since
This function is available since SDL 3.0.0.

◆ SDL_GetRectIntersection()

SDL_bool SDL_GetRectIntersection ( const SDL_Rect A,
const SDL_Rect B,
SDL_Rect result 
)
extern

Calculate the intersection of two rectangles.

If result is NULL then this function will return SDL_FALSE.

Parameters
Aan SDL_Rect structure representing the first rectangle
Ban SDL_Rect structure representing the second rectangle
resultan SDL_Rect structure filled in with the intersection of rectangles A and B
Returns
SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
Since
This function is available since SDL 3.0.0.
See also
SDL_HasRectIntersection

◆ SDL_GetRectIntersectionFloat()

SDL_bool SDL_GetRectIntersectionFloat ( const SDL_FRect A,
const SDL_FRect B,
SDL_FRect result 
)
extern

Calculate the intersection of two rectangles with float precision.

If result is NULL then this function will return SDL_FALSE.

Parameters
Aan SDL_FRect structure representing the first rectangle
Ban SDL_FRect structure representing the second rectangle
resultan SDL_FRect structure filled in with the intersection of rectangles A and B
Returns
SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
Since
This function is available since SDL 3.0.0.
See also
SDL_HasRectIntersectionFloat

◆ SDL_GetRectUnion()

int SDL_GetRectUnion ( const SDL_Rect A,
const SDL_Rect B,
SDL_Rect result 
)
extern

Calculate the union of two rectangles.

Parameters
Aan SDL_Rect structure representing the first rectangle
Ban SDL_Rect structure representing the second rectangle
resultan SDL_Rect structure filled in with the union of rectangles A and B
Returns
0 on success or a negative error code on failure; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.0.

◆ SDL_GetRectUnionFloat()

int SDL_GetRectUnionFloat ( const SDL_FRect A,
const SDL_FRect B,
SDL_FRect result 
)
extern

Calculate the union of two rectangles with float precision.

Parameters
Aan SDL_FRect structure representing the first rectangle
Ban SDL_FRect structure representing the second rectangle
resultan SDL_FRect structure filled in with the union of rectangles A and B
Returns
0 on success or a negative error code on failure; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.0.

◆ SDL_HasRectIntersection()

SDL_bool SDL_HasRectIntersection ( const SDL_Rect A,
const SDL_Rect B 
)
extern

Determine whether two rectangles intersect.

If either pointer is NULL the function will return SDL_FALSE.

Parameters
Aan SDL_Rect structure representing the first rectangle
Ban SDL_Rect structure representing the second rectangle
Returns
SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
Since
This function is available since SDL 3.0.0.
See also
SDL_GetRectIntersection

◆ SDL_HasRectIntersectionFloat()

SDL_bool SDL_HasRectIntersectionFloat ( const SDL_FRect A,
const SDL_FRect B 
)
extern

Determine whether two rectangles intersect with float precision.

If either pointer is NULL the function will return SDL_FALSE.

Parameters
Aan SDL_FRect structure representing the first rectangle
Ban SDL_FRect structure representing the second rectangle
Returns
SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
Since
This function is available since SDL 3.0.0.
See also
SDL_GetRectIntersection

◆ SDL_PointInRect()

SDL_FORCE_INLINE SDL_bool SDL_PointInRect ( const SDL_Point p,
const SDL_Rect r 
)

Returns true if point resides inside a rectangle.

Definition at line 110 of file SDL_rect.h.

111{
112 return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
113 (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
114}
@ SDL_TRUE
Definition SDL_stdinc.h:132
@ SDL_FALSE
Definition SDL_stdinc.h:131
int h
Definition SDL_rect.h:81
int w
Definition SDL_rect.h:81
int y
Definition SDL_rect.h:80
int x
Definition SDL_rect.h:80

References SDL_Rect::h, SDL_FALSE, SDL_TRUE, SDL_Rect::w, SDL_Point::x, SDL_Rect::x, SDL_Point::y, and SDL_Rect::y.

◆ SDL_PointInRectFloat()

SDL_FORCE_INLINE SDL_bool SDL_PointInRectFloat ( const SDL_FPoint p,
const SDL_FRect r 
)

Returns true if point resides inside a rectangle.

Definition at line 235 of file SDL_rect.h.

236{
237 return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
238 (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
239}
float x
Definition SDL_rect.h:62
float y
Definition SDL_rect.h:63
float h
Definition SDL_rect.h:103
float x
Definition SDL_rect.h:100
float w
Definition SDL_rect.h:102
float y
Definition SDL_rect.h:101

References SDL_FRect::h, SDL_FALSE, SDL_TRUE, SDL_FRect::w, SDL_FPoint::x, SDL_FRect::x, SDL_FPoint::y, and SDL_FRect::y.

◆ SDL_RectEmpty()

SDL_FORCE_INLINE SDL_bool SDL_RectEmpty ( const SDL_Rect r)

Returns true if the rectangle has no area.

Definition at line 119 of file SDL_rect.h.

120{
121 return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
122}

References SDL_Rect::h, SDL_FALSE, SDL_TRUE, and SDL_Rect::w.

◆ SDL_RectEmptyFloat()

SDL_FORCE_INLINE SDL_bool SDL_RectEmptyFloat ( const SDL_FRect r)

Returns true if the rectangle has no area.

Since
This function is available since SDL 3.0.0.

Definition at line 246 of file SDL_rect.h.

247{
248 return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
249}

References SDL_FRect::h, SDL_FALSE, SDL_TRUE, and SDL_FRect::w.

◆ SDL_RectsEqual()

SDL_FORCE_INLINE SDL_bool SDL_RectsEqual ( const SDL_Rect a,
const SDL_Rect b 
)

Returns true if the two rectangles are equal.

Definition at line 127 of file SDL_rect.h.

128{
129 return (a && b && (a->x == b->x) && (a->y == b->y) &&
130 (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
131}

References SDL_Rect::h, SDL_FALSE, SDL_TRUE, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

◆ SDL_RectsEqualEpsilon()

SDL_FORCE_INLINE SDL_bool SDL_RectsEqualEpsilon ( const SDL_FRect a,
const SDL_FRect b,
const float  epsilon 
)

Returns true if the two rectangles are equal, within some given epsilon.

Since
This function is available since SDL 3.0.0.

Definition at line 256 of file SDL_rect.h.

257{
258 return (a && b && ((a == b) ||
259 ((SDL_fabsf(a->x - b->x) <= epsilon) &&
260 (SDL_fabsf(a->y - b->y) <= epsilon) &&
261 (SDL_fabsf(a->w - b->w) <= epsilon) &&
262 (SDL_fabsf(a->h - b->h) <= epsilon))))
264}
float SDL_fabsf(float x)

References SDL_FRect::h, SDL_fabsf(), SDL_FALSE, SDL_TRUE, SDL_FRect::w, SDL_FRect::x, and SDL_FRect::y.

Referenced by SDL_RectsEqualFloat().

◆ SDL_RectsEqualFloat()

SDL_FORCE_INLINE SDL_bool SDL_RectsEqualFloat ( const SDL_FRect a,
const SDL_FRect b 
)

Returns true if the two rectangles are equal, using a default epsilon.

Since
This function is available since SDL 3.0.0.

Definition at line 271 of file SDL_rect.h.

272{
274}
SDL_FORCE_INLINE SDL_bool SDL_RectsEqualEpsilon(const SDL_FRect *a, const SDL_FRect *b, const float epsilon)
Definition SDL_rect.h:256
#define SDL_FLT_EPSILON
Definition SDL_stdinc.h:196

References SDL_FLT_EPSILON, and SDL_RectsEqualEpsilon().