SDL_rect.h File Reference
`#include <SDL3/SDL_stdinc.h>`
`#include <SDL3/SDL_error.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 void | SDL_RectToFRect (const SDL_Rect *rect, SDL_FRect *frect)
SDL_FORCE_INLINE bool | SDL_PointInRect (const SDL_Point *p, const SDL_Rect *r)
SDL_FORCE_INLINE bool | SDL_RectEmpty (const SDL_Rect *r)
SDL_FORCE_INLINE bool | SDL_RectsEqual (const SDL_Rect *a, const SDL_Rect *b)
bool | SDL_HasRectIntersection (const SDL_Rect *A, const SDL_Rect *B)
bool | SDL_GetRectIntersection (const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result)
bool | SDL_GetRectUnion (const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result)
bool | SDL_GetRectEnclosingPoints (const SDL_Point *points, int count, const SDL_Rect *clip, SDL_Rect *result)
bool | SDL_GetRectAndLineIntersection (const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2)
SDL_FORCE_INLINE bool | SDL_PointInRectFloat (const SDL_FPoint *p, const SDL_FRect *r)
SDL_FORCE_INLINE bool | SDL_RectEmptyFloat (const SDL_FRect *r)
SDL_FORCE_INLINE bool | SDL_RectsEqualEpsilon (const SDL_FRect *a, const SDL_FRect *b, float epsilon)
SDL_FORCE_INLINE bool | SDL_RectsEqualFloat (const SDL_FRect *a, const SDL_FRect *b)
bool | SDL_HasRectIntersectionFloat (const SDL_FRect *A, const SDL_FRect *B)
bool | SDL_GetRectIntersectionFloat (const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result)
bool | SDL_GetRectUnionFloat (const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result)
bool | SDL_GetRectEnclosingPointsFloat (const SDL_FPoint *points, int count, const SDL_FRect *clip, SDL_FRect *result)
bool | SDL_GetRectAndLineIntersectionFloat (const SDL_FRect *rect, float *X1, float *Y1, float *X2, float *Y2)

## Function Documentation

## SDL_GetRectAndLineIntersection()

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

  rect | an SDL_Rect structure representing the rectangle to intersect.
  X1 | a pointer to the starting X-coordinate of the line.
  Y1 | a pointer to the starting Y-coordinate of the line.
  X2 | a pointer to the ending X-coordinate of the line.
  Y2 | a pointer to the ending Y-coordinate of the line.

Returns
  true if there is an intersection, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

## SDL_GetRectAndLineIntersectionFloat()

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

  rect | an SDL_FRect structure representing the rectangle to intersect.
  X1 | a pointer to the starting X-coordinate of the line.
  Y1 | a pointer to the starting Y-coordinate of the line.
  X2 | a pointer to the ending X-coordinate of the line.
  Y2 | a pointer to the ending Y-coordinate of the line.

Returns
  true if there is an intersection, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

## SDL_GetRectEnclosingPoints()

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

  points | an array of SDL_Point structures representing points to be enclosed.
  count | the number of structures in the points array.
  clip | an SDL_Rect used for clipping or NULL to enclose all points.
  result | an SDL_Rect structure filled in with the minimal enclosing rectangle.

Returns
  true if any points were enclosed or false if all the points were outside of the clipping rectangle.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

## SDL_GetRectEnclosingPointsFloat()

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

  points | an array of SDL_FPoint structures representing points to be enclosed.
  count | the number of structures in the points array.
  clip | an SDL_FRect used for clipping or NULL to enclose all points.
  result | an SDL_FRect structure filled in with the minimal enclosing rectangle.

Returns
  true if any points were enclosed or false if all the points were outside of the clipping rectangle.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

## SDL_GetRectIntersection()

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 false.

Parameters

  A | an SDL_Rect structure representing the first rectangle.
  B | an SDL_Rect structure representing the second rectangle.
  result | an SDL_Rect structure filled in with the intersection of rectangles A and B.

Returns
  true if there is an intersection, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

See also
  SDL_HasRectIntersection

## SDL_GetRectIntersectionFloat()

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 false.

Parameters

  A | an SDL_FRect structure representing the first rectangle.
  B | an SDL_FRect structure representing the second rectangle.
  result | an SDL_FRect structure filled in with the intersection of rectangles A and B.

Returns
  true if there is an intersection, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

See also
  SDL_HasRectIntersectionFloat

## SDL_GetRectUnion()

bool SDL_GetRectUnion | ( | const SDL_Rect * | A,
|  | const SDL_Rect * | B,
|  | SDL_Rect * | result )

extern

Calculate the union of two rectangles.

Parameters

  A | an SDL_Rect structure representing the first rectangle.
  B | an SDL_Rect structure representing the second rectangle.
  result | an SDL_Rect structure filled in with the union of rectangles A and B.

Returns
  true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

## SDL_GetRectUnionFloat()

bool SDL_GetRectUnionFloat | ( | const SDL_FRect * | A,
|  | const SDL_FRect * | B,
|  | SDL_FRect * | result )

extern

Calculate the union of two rectangles with float precision.

Parameters

  A | an SDL_FRect structure representing the first rectangle.
  B | an SDL_FRect structure representing the second rectangle.
  result | an SDL_FRect structure filled in with the union of rectangles A and B.

Returns
  true on success or false on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

## SDL_HasRectIntersection()

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 false.

Parameters

  A | an SDL_Rect structure representing the first rectangle.
  B | an SDL_Rect structure representing the second rectangle.

Returns
  true if there is an intersection, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

See also
  SDL_GetRectIntersection

## SDL_HasRectIntersectionFloat()

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 false.

Parameters

  A | an SDL_FRect structure representing the first rectangle.
  B | an SDL_FRect structure representing the second rectangle.

Returns
  true if there is an intersection, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

See also
  SDL_GetRectIntersectionFloat

## SDL_PointInRect()

SDL_FORCE_INLINE bool SDL_PointInRect | ( | const SDL_Point * | p,
|  | const SDL_Rect * | r )

Determine whether a point resides inside a rectangle.

A point is considered part of a rectangle if both p and r are not NULL, and p's x and y coordinates are >= to the rectangle's top left corner, and < the rectangle's x+w and y+h. So a 1x1 rectangle considers point (0,0) as "inside" and (0,1) as not.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters

  p | the point to test.
  r | the rectangle to test.

Returns
  true if p is contained by r, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

Definition at line 158 of file SDL_rect.h.

{
return ( p && r && (p->x >= r->x) && (p->x < (r->x + r->w)) &&
(p->y >= r->y) && (p->y < (r->y + r->h)) ) ? true : false;
}
true
#define true
Definition SDL_stdinc.h:101
SDL_Point::x
int x
Definition SDL_rect.h:51
SDL_Point::y
int y
Definition SDL_rect.h:52
SDL_Rect::h
int h
Definition SDL_rect.h:86
SDL_Rect::w
int w
Definition SDL_rect.h:86
SDL_Rect::y
int y
Definition SDL_rect.h:85
SDL_Rect::x
int x
Definition SDL_rect.h:85

References SDL_Rect::h, SDL_FORCE_INLINE, true, SDL_Rect::w, SDL_Point::x, SDL_Rect::x, SDL_Point::y, and SDL_Rect::y.

## SDL_PointInRectFloat()

SDL_FORCE_INLINE bool SDL_PointInRectFloat | ( | const SDL_FPoint * | p,
|  | const SDL_FRect * | r )

Determine whether a point resides inside a floating point rectangle.

A point is considered part of a rectangle if both p and r are not NULL, and p's x and y coordinates are >= to the rectangle's top left corner, and <= the rectangle's x+w and y+h. So a 1x1 rectangle considers point (0,0) and (0,1) as "inside" and (0,2) as not.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters

  p | the point to test.
  r | the rectangle to test.

Returns
  true if p is contained by r, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

Definition at line 331 of file SDL_rect.h.

{
return ( p && r && (p->x >= r->x) && (p->x <= (r->x + r->w)) &&
(p->y >= r->y) && (p->y <= (r->y + r->h)) ) ? true : false;
}
SDL_FPoint::x
float x
Definition SDL_rect.h:65
SDL_FPoint::y
float y
Definition SDL_rect.h:66
SDL_FRect::h
float h
Definition SDL_rect.h:114
SDL_FRect::x
float x
Definition SDL_rect.h:111
SDL_FRect::w
float w
Definition SDL_rect.h:113
SDL_FRect::y
float y
Definition SDL_rect.h:112

References SDL_FRect::h, SDL_FORCE_INLINE, true, SDL_FRect::w, SDL_FPoint::x, SDL_FRect::x, SDL_FPoint::y, and SDL_FRect::y.

## SDL_RectEmpty()

SDL_FORCE_INLINE bool SDL_RectEmpty | ( | const SDL_Rect * | r | ) |

Determine whether a rectangle has no area.

A rectangle is considered "empty" for this function if r is NULL, or if r's width and/or height are <= 0.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters

  r | the rectangle to test.

Returns
  true if the rectangle is "empty", false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

Definition at line 182 of file SDL_rect.h.

{
return ((!r) || (r->w <= 0) || (r->h <= 0)) ? true : false;
}

References SDL_Rect::h, SDL_FORCE_INLINE, true, and SDL_Rect::w.

## SDL_RectEmptyFloat()

SDL_FORCE_INLINE bool SDL_RectEmptyFloat | ( | const SDL_FRect * | r | ) |

Determine whether a floating point rectangle takes no space.

A rectangle is considered "empty" for this function if r is NULL, or if r's width and/or height are < 0.0f.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters

  r | the rectangle to test.

Returns
  true if the rectangle is "empty", false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

Definition at line 355 of file SDL_rect.h.

{
return ((!r) || (r->w < 0.0f) || (r->h < 0.0f)) ? true : false;
}

References SDL_FRect::h, SDL_FORCE_INLINE, true, and SDL_FRect::w.

## SDL_RectsEqual()

SDL_FORCE_INLINE bool SDL_RectsEqual | ( | const SDL_Rect * | a,
|  | const SDL_Rect * | b )

Determine whether two rectangles are equal.

Rectangles are considered equal if both are not NULL and each of their x, y, width and height match.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters

  a | the first rectangle to test.
  b | the second rectangle to test.

Returns
  true if the rectangles are equal, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

Definition at line 206 of file SDL_rect.h.

{
return (a && b && (a->x == b->x) && (a->y == b->y) &&
(a->w == b->w) && (a->h == b->h)) ? true : false;
}

References SDL_Rect::h, SDL_FORCE_INLINE, true, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

## SDL_RectsEqualEpsilon()

SDL_FORCE_INLINE bool SDL_RectsEqualEpsilon | ( | const SDL_FRect * | a,
|  | const SDL_FRect * | b,
|  | float | epsilon )

Determine whether two floating point rectangles are equal, within some given epsilon.

Rectangles are considered equal if both are not NULL and each of their x, y, width and height are within epsilon of each other. If you don't know what value to use for epsilon, you should call the SDL_RectsEqualFloat function instead.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters

  a | the first rectangle to test.
  b | the second rectangle to test.
  epsilon | the epsilon value for comparison.

Returns
  true if the rectangles are equal, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

See also
  SDL_RectsEqualFloat

Definition at line 385 of file SDL_rect.h.

{
return (a && b && ((a == b) ||
((SDL_fabsf(a->x - b->x) <= epsilon) &&
(SDL_fabsf(a->y - b->y) <= epsilon) &&
(SDL_fabsf(a->w - b->w) <= epsilon) &&
(SDL_fabsf(a->h - b->h) <= epsilon))))
? true : false;
}
SDL_fabsf
float SDL_fabsf(float x)

References SDL_FRect::h, SDL_fabsf(), SDL_FORCE_INLINE, true, SDL_FRect::w, SDL_FRect::x, and SDL_FRect::y.

Referenced by SDL_RectsEqualFloat().

## SDL_RectsEqualFloat()

SDL_FORCE_INLINE bool SDL_RectsEqualFloat | ( | const SDL_FRect * | a,
|  | const SDL_FRect * | b )

Determine whether two floating point rectangles are equal, within a default epsilon.

Rectangles are considered equal if both are not NULL and each of their x, y, width and height are within SDL_FLT_EPSILON of each other. This is often a reasonable way to compare two floating point rectangles and deal with the slight precision variations in floating point calculations that tend to pop up.

Note that this is a forced-inline function in a header, and not a public API function available in the SDL library (which is to say, the code is embedded in the calling program and the linker and dynamic loader will not be able to find this function inside SDL itself).

Parameters

  a | the first rectangle to test.
  b | the second rectangle to test.

Returns
  true if the rectangles are equal, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

See also
  SDL_RectsEqualEpsilon

Definition at line 420 of file SDL_rect.h.

{
return SDL_RectsEqualEpsilon(a, b, SDL_FLT_EPSILON);
}
SDL_RectsEqualEpsilon
SDL_FORCE_INLINE bool SDL_RectsEqualEpsilon(const SDL_FRect *a, const SDL_FRect *b, float epsilon)
Definition SDL_rect.h:385
SDL_FLT_EPSILON
#define SDL_FLT_EPSILON
Definition SDL_stdinc.h:544

References SDL_FLT_EPSILON, SDL_FORCE_INLINE, and SDL_RectsEqualEpsilon().

## SDL_RectToFRect()

SDL_FORCE_INLINE void SDL_RectToFRect | ( | const SDL_Rect * | rect,
|  | SDL_FRect * | frect )

Convert an SDL_Rect to SDL_FRect

Parameters

  rect | a pointer to an SDL_Rect.
  frect | a pointer filled in with the floating point representation of rect.

\threadsafety It is safe to call this function from any thread.

Since
  This function is available since SDL 3.2.0.

Definition at line 129 of file SDL_rect.h.

{
frect->x = SDL_static_cast(float, rect->x);
frect->y = SDL_static_cast(float, rect->y);
frect->w = SDL_static_cast(float, rect->w);
frect->h = SDL_static_cast(float, rect->h);
}
SDL_static_cast
#define SDL_static_cast(type, expression)
Definition SDL_stdinc.h:345

References SDL_FRect::h, SDL_Rect::h, SDL_FORCE_INLINE, SDL_static_cast, SDL_FRect::w, SDL_Rect::w, SDL_FRect::x, SDL_Rect::x, SDL_FRect::y, and SDL_Rect::y.