SDL  2.0
SDL_rect.h
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 /**
23  * \file SDL_rect.h
24  *
25  * Header file for SDL_rect definition and management functions.
26  */
27 
28 #ifndef SDL_rect_h_
29 #define SDL_rect_h_
30 
31 #include "SDL_stdinc.h"
32 #include "SDL_error.h"
33 #include "SDL_pixels.h"
34 #include "SDL_rwops.h"
35 
36 #include "begin_code.h"
37 /* Set up for C function definitions, even when using C++ */
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * The structure that defines a point (integer)
44  *
45  * \sa SDL_EnclosePoints
46  * \sa SDL_PointInRect
47  */
48 typedef struct SDL_Point
49 {
50  int x;
51  int y;
52 } SDL_Point;
53 
54 /**
55  * The structure that defines a point (floating point)
56  *
57  * \sa SDL_FRectEmpty
58  * \sa SDL_FRectEquals
59  * \sa SDL_HasIntersectionF
60  * \sa SDL_IntersectFRect
61  * \sa SDL_UnionFRect
62  * \sa SDL_EncloseFPoints
63  * \sa SDL_PointInFRect
64  */
65 typedef struct SDL_FPoint
66 {
67  float x;
68  float y;
69 } SDL_FPoint;
70 
71 
72 /**
73  * A rectangle, with the origin at the upper left (integer).
74  *
75  * \sa SDL_RectEmpty
76  * \sa SDL_RectEquals
77  * \sa SDL_HasIntersection
78  * \sa SDL_IntersectRect
79  * \sa SDL_UnionRect
80  * \sa SDL_EnclosePoints
81  */
82 typedef struct SDL_Rect
83 {
84  int x, y;
85  int w, h;
86 } SDL_Rect;
87 
88 
89 /**
90  * A rectangle, with the origin at the upper left (floating point).
91  */
92 typedef struct SDL_FRect
93 {
94  float x;
95  float y;
96  float w;
97  float h;
98 } SDL_FRect;
99 
100 
101 /**
102  * Returns true if point resides inside a rectangle.
103  */
105 {
106  return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
107  (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
108 }
109 
110 /**
111  * Returns true if the rectangle has no area.
112  */
114 {
115  return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
116 }
117 
118 /**
119  * Returns true if the two rectangles are equal.
120  */
122 {
123  return (a && b && (a->x == b->x) && (a->y == b->y) &&
124  (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
125 }
126 
127 /**
128  * Determine whether two rectangles intersect.
129  *
130  * If either pointer is NULL the function will return SDL_FALSE.
131  *
132  * \param A an SDL_Rect structure representing the first rectangle
133  * \param B an SDL_Rect structure representing the second rectangle
134  * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
135  *
136  * \since This function is available since SDL 2.0.0.
137  *
138  * \sa SDL_IntersectRect
139  */
140 extern DECLSPEC SDL_bool SDLCALL SDL_HasIntersection(const SDL_Rect * A,
141  const SDL_Rect * B);
142 
143 /**
144  * Calculate the intersection of two rectangles.
145  *
146  * If `result` is NULL then this function will return SDL_FALSE.
147  *
148  * \param A an SDL_Rect structure representing the first rectangle
149  * \param B an SDL_Rect structure representing the second rectangle
150  * \param result an SDL_Rect structure filled in with the intersection of
151  * rectangles `A` and `B`
152  * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
153  *
154  * \since This function is available since SDL 2.0.0.
155  *
156  * \sa SDL_HasIntersection
157  */
158 extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRect(const SDL_Rect * A,
159  const SDL_Rect * B,
160  SDL_Rect * result);
161 
162 /**
163  * Calculate the union of two rectangles.
164  *
165  * \param A an SDL_Rect structure representing the first rectangle
166  * \param B an SDL_Rect structure representing the second rectangle
167  * \param result an SDL_Rect structure filled in with the union of rectangles
168  * `A` and `B`
169  *
170  * \since This function is available since SDL 2.0.0.
171  */
172 extern DECLSPEC void SDLCALL SDL_UnionRect(const SDL_Rect * A,
173  const SDL_Rect * B,
174  SDL_Rect * result);
175 
176 /**
177  * Calculate a minimal rectangle enclosing a set of points.
178  *
179  * If `clip` is not NULL then only points inside of the clipping rectangle are
180  * considered.
181  *
182  * \param points an array of SDL_Point structures representing points to be
183  * enclosed
184  * \param count the number of structures in the `points` array
185  * \param clip an SDL_Rect used for clipping or NULL to enclose all points
186  * \param result an SDL_Rect structure filled in with the minimal enclosing
187  * rectangle
188  * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
189  * points were outside of the clipping rectangle.
190  *
191  * \since This function is available since SDL 2.0.0.
192  */
193 extern DECLSPEC SDL_bool SDLCALL SDL_EnclosePoints(const SDL_Point * points,
194  int count,
195  const SDL_Rect * clip,
196  SDL_Rect * result);
197 
198 /**
199  * Calculate the intersection of a rectangle and line segment.
200  *
201  * This function is used to clip a line segment to a rectangle. A line segment
202  * contained entirely within the rectangle or that does not intersect will
203  * remain unchanged. A line segment that crosses the rectangle at either or
204  * both ends will be clipped to the boundary of the rectangle and the new
205  * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
206  *
207  * \param rect an SDL_Rect structure representing the rectangle to intersect
208  * \param X1 a pointer to the starting X-coordinate of the line
209  * \param Y1 a pointer to the starting Y-coordinate of the line
210  * \param X2 a pointer to the ending X-coordinate of the line
211  * \param Y2 a pointer to the ending Y-coordinate of the line
212  * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
213  *
214  * \since This function is available since SDL 2.0.0.
215  */
216 extern DECLSPEC SDL_bool SDLCALL SDL_IntersectRectAndLine(const SDL_Rect *
217  rect, int *X1,
218  int *Y1, int *X2,
219  int *Y2);
220 
221 
222 /* SDL_FRect versions... */
223 
224 /**
225  * Returns true if point resides inside a rectangle.
226  */
228 {
229  return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
230  (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
231 }
232 
233 /**
234  * Returns true if the rectangle has no area.
235  */
237 {
238  return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
239 }
240 
241 /**
242  * Returns true if the two rectangles are equal.
243  */
245 {
246  return (a && b && (a->x == b->x) && (a->y == b->y) &&
247  (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
248 }
249 
250 /**
251  * Determine whether two rectangles intersect with float precision.
252  *
253  * If either pointer is NULL the function will return SDL_FALSE.
254  *
255  * \param A an SDL_FRect structure representing the first rectangle
256  * \param B an SDL_FRect structure representing the second rectangle
257  * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
258  *
259  * \since This function is available since SDL 2.0.22.
260  *
261  * \sa SDL_IntersectRect
262  */
263 extern DECLSPEC SDL_bool SDLCALL SDL_HasIntersectionF(const SDL_FRect * A,
264  const SDL_FRect * B);
265 
266 /**
267  * Calculate the intersection of two rectangles with float precision.
268  *
269  * If `result` is NULL then this function will return SDL_FALSE.
270  *
271  * \param A an SDL_FRect structure representing the first rectangle
272  * \param B an SDL_FRect structure representing the second rectangle
273  * \param result an SDL_FRect structure filled in with the intersection of
274  * rectangles `A` and `B`
275  * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
276  *
277  * \since This function is available since SDL 2.0.22.
278  *
279  * \sa SDL_HasIntersectionF
280  */
281 extern DECLSPEC SDL_bool SDLCALL SDL_IntersectFRect(const SDL_FRect * A,
282  const SDL_FRect * B,
283  SDL_FRect * result);
284 
285 /**
286  * Calculate the union of two rectangles with float precision.
287  *
288  * \param A an SDL_FRect structure representing the first rectangle
289  * \param B an SDL_FRect structure representing the second rectangle
290  * \param result an SDL_FRect structure filled in with the union of rectangles
291  * `A` and `B`
292  *
293  * \since This function is available since SDL 2.0.22.
294  */
295 extern DECLSPEC void SDLCALL SDL_UnionFRect(const SDL_FRect * A,
296  const SDL_FRect * B,
297  SDL_FRect * result);
298 
299 /**
300  * Calculate a minimal rectangle enclosing a set of points with float
301  * precision.
302  *
303  * If `clip` is not NULL then only points inside of the clipping rectangle are
304  * considered.
305  *
306  * \param points an array of SDL_FPoint structures representing points to be
307  * enclosed
308  * \param count the number of structures in the `points` array
309  * \param clip an SDL_FRect used for clipping or NULL to enclose all points
310  * \param result an SDL_FRect structure filled in with the minimal enclosing
311  * rectangle
312  * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
313  * points were outside of the clipping rectangle.
314  *
315  * \since This function is available since SDL 2.0.22.
316  */
317 extern DECLSPEC SDL_bool SDLCALL SDL_EncloseFPoints(const SDL_FPoint * points,
318  int count,
319  const SDL_FRect * clip,
320  SDL_FRect * result);
321 
322 /**
323  * Calculate the intersection of a rectangle and line segment with float
324  * precision.
325  *
326  * This function is used to clip a line segment to a rectangle. A line segment
327  * contained entirely within the rectangle or that does not intersect will
328  * remain unchanged. A line segment that crosses the rectangle at either or
329  * both ends will be clipped to the boundary of the rectangle and the new
330  * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
331  *
332  * \param rect an SDL_FRect structure representing the rectangle to intersect
333  * \param X1 a pointer to the starting X-coordinate of the line
334  * \param Y1 a pointer to the starting Y-coordinate of the line
335  * \param X2 a pointer to the ending X-coordinate of the line
336  * \param Y2 a pointer to the ending Y-coordinate of the line
337  * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
338  *
339  * \since This function is available since SDL 2.0.22.
340  */
341 extern DECLSPEC SDL_bool SDLCALL SDL_IntersectFRectAndLine(const SDL_FRect *
342  rect, float *X1,
343  float *Y1, float *X2,
344  float *Y2);
345 
346 /* Ends C function definitions when using C++ */
347 #ifdef __cplusplus
348 }
349 #endif
350 #include "close_code.h"
351 
352 #endif /* SDL_rect_h_ */
353 
354 /* vi: set ts=4 sw=4 expandtab: */
SDL_bool SDL_EncloseFPoints(const SDL_FPoint *points, int count, const SDL_FRect *clip, SDL_FRect *result)
SDL_FORCE_INLINE SDL_bool SDL_RectEquals(const SDL_Rect *a, const SDL_Rect *b)
Definition: SDL_rect.h:121
SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
Definition: SDL_rect.h:104
void SDL_UnionFRect(const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result)
SDL_bool SDL_IntersectRect(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result)
SDL_bool SDL_EnclosePoints(const SDL_Point *points, int count, const SDL_Rect *clip, SDL_Rect *result)
SDL_bool SDL_HasIntersection(const SDL_Rect *A, const SDL_Rect *B)
void SDL_UnionRect(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result)
SDL_bool SDL_IntersectRectAndLine(const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2)
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Definition: SDL_rect.h:113
SDL_FORCE_INLINE SDL_bool SDL_FRectEquals(const SDL_FRect *a, const SDL_FRect *b)
Definition: SDL_rect.h:244
SDL_bool SDL_IntersectFRect(const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result)
SDL_FORCE_INLINE SDL_bool SDL_PointInFRect(const SDL_FPoint *p, const SDL_FRect *r)
Definition: SDL_rect.h:227
SDL_bool SDL_HasIntersectionF(const SDL_FRect *A, const SDL_FRect *B)
SDL_FORCE_INLINE SDL_bool SDL_FRectEmpty(const SDL_FRect *r)
Definition: SDL_rect.h:236
SDL_bool SDL_IntersectFRectAndLine(const SDL_FRect *rect, float *X1, float *Y1, float *X2, float *Y2)
SDL_bool
Definition: SDL_stdinc.h:179
@ SDL_TRUE
Definition: SDL_stdinc.h:181
@ SDL_FALSE
Definition: SDL_stdinc.h:180
#define SDL_FORCE_INLINE
Definition: begin_code.h:143
float x
Definition: SDL_rect.h:67
float y
Definition: SDL_rect.h:68
float h
Definition: SDL_rect.h:97
float x
Definition: SDL_rect.h:94
float w
Definition: SDL_rect.h:96
float y
Definition: SDL_rect.h:95
int x
Definition: SDL_rect.h:50
int y
Definition: SDL_rect.h:51
int h
Definition: SDL_rect.h:85
int w
Definition: SDL_rect.h:85
int y
Definition: SDL_rect.h:84
int x
Definition: SDL_rect.h:84