1
0

SDL_rect.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryRect
  20. *
  21. * Some helper functions for managing rectangles and 2D points, in both
  22. * integer and floating point versions.
  23. */
  24. #ifndef SDL_rect_h_
  25. #define SDL_rect_h_
  26. #include <SDL3/SDL_stdinc.h>
  27. #include <SDL3/SDL_error.h>
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /**
  34. * The structure that defines a point (using integers).
  35. *
  36. * \since This struct is available since SDL 3.2.0.
  37. *
  38. * \sa SDL_GetRectEnclosingPoints
  39. * \sa SDL_PointInRect
  40. */
  41. typedef struct SDL_Point
  42. {
  43. int x;
  44. int y;
  45. } SDL_Point;
  46. /**
  47. * The structure that defines a point (using floating point values).
  48. *
  49. * \since This struct is available since SDL 3.2.0.
  50. *
  51. * \sa SDL_GetRectEnclosingPointsFloat
  52. * \sa SDL_PointInRectFloat
  53. */
  54. typedef struct SDL_FPoint
  55. {
  56. float x;
  57. float y;
  58. } SDL_FPoint;
  59. /**
  60. * A rectangle, with the origin at the upper left (using integers).
  61. *
  62. * \since This struct is available since SDL 3.2.0.
  63. *
  64. * \sa SDL_RectEmpty
  65. * \sa SDL_RectsEqual
  66. * \sa SDL_HasRectIntersection
  67. * \sa SDL_GetRectIntersection
  68. * \sa SDL_GetRectAndLineIntersection
  69. * \sa SDL_GetRectUnion
  70. * \sa SDL_GetRectEnclosingPoints
  71. */
  72. typedef struct SDL_Rect
  73. {
  74. int x, y;
  75. int w, h;
  76. } SDL_Rect;
  77. /**
  78. * A rectangle, with the origin at the upper left (using floating point
  79. * values).
  80. *
  81. * \since This struct is available since SDL 3.2.0.
  82. *
  83. * \sa SDL_RectEmptyFloat
  84. * \sa SDL_RectsEqualFloat
  85. * \sa SDL_RectsEqualEpsilon
  86. * \sa SDL_HasRectIntersectionFloat
  87. * \sa SDL_GetRectIntersectionFloat
  88. * \sa SDL_GetRectAndLineIntersectionFloat
  89. * \sa SDL_GetRectUnionFloat
  90. * \sa SDL_GetRectEnclosingPointsFloat
  91. * \sa SDL_PointInRectFloat
  92. */
  93. typedef struct SDL_FRect
  94. {
  95. float x;
  96. float y;
  97. float w;
  98. float h;
  99. } SDL_FRect;
  100. /**
  101. * Convert an SDL_Rect to SDL_FRect
  102. *
  103. * \param rect a pointer to an SDL_Rect.
  104. * \param frect a pointer filled in with the floating point representation of
  105. * `rect`.
  106. *
  107. * \threadsafety It is safe to call this function from any thread.
  108. *
  109. * \since This function is available since SDL 3.2.0.
  110. */
  111. SDL_FORCE_INLINE void SDL_RectToFRect(const SDL_Rect *rect, SDL_FRect *frect)
  112. {
  113. frect->x = (float)rect->x;
  114. frect->y = (float)rect->y;
  115. frect->w = (float)rect->w;
  116. frect->h = (float)rect->h;
  117. }
  118. /**
  119. * Determine whether a point resides inside a rectangle.
  120. *
  121. * A point is considered part of a rectangle if both `p` and `r` are not NULL,
  122. * and `p`'s x and y coordinates are >= to the rectangle's top left corner,
  123. * and < the rectangle's x+w and y+h. So a 1x1 rectangle considers point (0,0)
  124. * as "inside" and (0,1) as not.
  125. *
  126. * Note that this is a forced-inline function in a header, and not a public
  127. * API function available in the SDL library (which is to say, the code is
  128. * embedded in the calling program and the linker and dynamic loader will not
  129. * be able to find this function inside SDL itself).
  130. *
  131. * \param p the point to test.
  132. * \param r the rectangle to test.
  133. * \returns true if `p` is contained by `r`, false otherwise.
  134. *
  135. * \threadsafety It is safe to call this function from any thread.
  136. *
  137. * \since This function is available since SDL 3.2.0.
  138. */
  139. SDL_FORCE_INLINE bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
  140. {
  141. return ( p && r && (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  142. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? true : false;
  143. }
  144. /**
  145. * Determine whether a rectangle has no area.
  146. *
  147. * A rectangle is considered "empty" for this function if `r` is NULL, or if
  148. * `r`'s width and/or height are <= 0.
  149. *
  150. * Note that this is a forced-inline function in a header, and not a public
  151. * API function available in the SDL library (which is to say, the code is
  152. * embedded in the calling program and the linker and dynamic loader will not
  153. * be able to find this function inside SDL itself).
  154. *
  155. * \param r the rectangle to test.
  156. * \returns true if the rectangle is "empty", false otherwise.
  157. *
  158. * \threadsafety It is safe to call this function from any thread.
  159. *
  160. * \since This function is available since SDL 3.2.0.
  161. */
  162. SDL_FORCE_INLINE bool SDL_RectEmpty(const SDL_Rect *r)
  163. {
  164. return ((!r) || (r->w <= 0) || (r->h <= 0)) ? true : false;
  165. }
  166. /**
  167. * Determine whether two rectangles are equal.
  168. *
  169. * Rectangles are considered equal if both are not NULL and each of their x,
  170. * y, width and height match.
  171. *
  172. * Note that this is a forced-inline function in a header, and not a public
  173. * API function available in the SDL library (which is to say, the code is
  174. * embedded in the calling program and the linker and dynamic loader will not
  175. * be able to find this function inside SDL itself).
  176. *
  177. * \param a the first rectangle to test.
  178. * \param b the second rectangle to test.
  179. * \returns true if the rectangles are equal, false otherwise.
  180. *
  181. * \threadsafety It is safe to call this function from any thread.
  182. *
  183. * \since This function is available since SDL 3.2.0.
  184. */
  185. SDL_FORCE_INLINE bool SDL_RectsEqual(const SDL_Rect *a, const SDL_Rect *b)
  186. {
  187. return (a && b && (a->x == b->x) && (a->y == b->y) &&
  188. (a->w == b->w) && (a->h == b->h)) ? true : false;
  189. }
  190. /**
  191. * Determine whether two rectangles intersect.
  192. *
  193. * If either pointer is NULL the function will return false.
  194. *
  195. * \param A an SDL_Rect structure representing the first rectangle.
  196. * \param B an SDL_Rect structure representing the second rectangle.
  197. * \returns true if there is an intersection, false otherwise.
  198. *
  199. * \threadsafety It is safe to call this function from any thread.
  200. *
  201. * \since This function is available since SDL 3.2.0.
  202. *
  203. * \sa SDL_GetRectIntersection
  204. */
  205. extern SDL_DECLSPEC bool SDLCALL SDL_HasRectIntersection(const SDL_Rect *A, const SDL_Rect *B);
  206. /**
  207. * Calculate the intersection of two rectangles.
  208. *
  209. * If `result` is NULL then this function will return false.
  210. *
  211. * \param A an SDL_Rect structure representing the first rectangle.
  212. * \param B an SDL_Rect structure representing the second rectangle.
  213. * \param result an SDL_Rect structure filled in with the intersection of
  214. * rectangles `A` and `B`.
  215. * \returns true if there is an intersection, false otherwise.
  216. *
  217. * \since This function is available since SDL 3.2.0.
  218. *
  219. * \sa SDL_HasRectIntersection
  220. */
  221. extern SDL_DECLSPEC bool SDLCALL SDL_GetRectIntersection(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result);
  222. /**
  223. * Calculate the union of two rectangles.
  224. *
  225. * \param A an SDL_Rect structure representing the first rectangle.
  226. * \param B an SDL_Rect structure representing the second rectangle.
  227. * \param result an SDL_Rect structure filled in with the union of rectangles
  228. * `A` and `B`.
  229. * \returns true on success or false on failure; call SDL_GetError() for more
  230. * information.
  231. *
  232. * \since This function is available since SDL 3.2.0.
  233. */
  234. extern SDL_DECLSPEC bool SDLCALL SDL_GetRectUnion(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *result);
  235. /**
  236. * Calculate a minimal rectangle enclosing a set of points.
  237. *
  238. * If `clip` is not NULL then only points inside of the clipping rectangle are
  239. * considered.
  240. *
  241. * \param points an array of SDL_Point structures representing points to be
  242. * enclosed.
  243. * \param count the number of structures in the `points` array.
  244. * \param clip an SDL_Rect used for clipping or NULL to enclose all points.
  245. * \param result an SDL_Rect structure filled in with the minimal enclosing
  246. * rectangle.
  247. * \returns true if any points were enclosed or false if all the points were
  248. * outside of the clipping rectangle.
  249. *
  250. * \since This function is available since SDL 3.2.0.
  251. */
  252. extern SDL_DECLSPEC bool SDLCALL SDL_GetRectEnclosingPoints(const SDL_Point *points, int count, const SDL_Rect *clip, SDL_Rect *result);
  253. /**
  254. * Calculate the intersection of a rectangle and line segment.
  255. *
  256. * This function is used to clip a line segment to a rectangle. A line segment
  257. * contained entirely within the rectangle or that does not intersect will
  258. * remain unchanged. A line segment that crosses the rectangle at either or
  259. * both ends will be clipped to the boundary of the rectangle and the new
  260. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  261. *
  262. * \param rect an SDL_Rect structure representing the rectangle to intersect.
  263. * \param X1 a pointer to the starting X-coordinate of the line.
  264. * \param Y1 a pointer to the starting Y-coordinate of the line.
  265. * \param X2 a pointer to the ending X-coordinate of the line.
  266. * \param Y2 a pointer to the ending Y-coordinate of the line.
  267. * \returns true if there is an intersection, false otherwise.
  268. *
  269. * \since This function is available since SDL 3.2.0.
  270. */
  271. extern SDL_DECLSPEC bool SDLCALL SDL_GetRectAndLineIntersection(const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2);
  272. /* SDL_FRect versions... */
  273. /**
  274. * Determine whether a point resides inside a floating point rectangle.
  275. *
  276. * A point is considered part of a rectangle if both `p` and `r` are not NULL,
  277. * and `p`'s x and y coordinates are >= to the rectangle's top left corner,
  278. * and <= the rectangle's x+w and y+h. So a 1x1 rectangle considers point
  279. * (0,0) and (0,1) as "inside" and (0,2) as not.
  280. *
  281. * Note that this is a forced-inline function in a header, and not a public
  282. * API function available in the SDL library (which is to say, the code is
  283. * embedded in the calling program and the linker and dynamic loader will not
  284. * be able to find this function inside SDL itself).
  285. *
  286. * \param p the point to test.
  287. * \param r the rectangle to test.
  288. * \returns true if `p` is contained by `r`, false otherwise.
  289. *
  290. * \threadsafety It is safe to call this function from any thread.
  291. *
  292. * \since This function is available since SDL 3.2.0.
  293. */
  294. SDL_FORCE_INLINE bool SDL_PointInRectFloat(const SDL_FPoint *p, const SDL_FRect *r)
  295. {
  296. return ( p && r && (p->x >= r->x) && (p->x <= (r->x + r->w)) &&
  297. (p->y >= r->y) && (p->y <= (r->y + r->h)) ) ? true : false;
  298. }
  299. /**
  300. * Determine whether a floating point rectangle can contain any point.
  301. *
  302. * A rectangle is considered "empty" for this function if `r` is NULL, or if
  303. * `r`'s width and/or height are < 0.0f.
  304. *
  305. * Note that this is a forced-inline function in a header, and not a public
  306. * API function available in the SDL library (which is to say, the code is
  307. * embedded in the calling program and the linker and dynamic loader will not
  308. * be able to find this function inside SDL itself).
  309. *
  310. * \param r the rectangle to test.
  311. * \returns true if the rectangle is "empty", false otherwise.
  312. *
  313. * \threadsafety It is safe to call this function from any thread.
  314. *
  315. * \since This function is available since SDL 3.2.0.
  316. */
  317. SDL_FORCE_INLINE bool SDL_RectEmptyFloat(const SDL_FRect *r)
  318. {
  319. return ((!r) || (r->w < 0.0f) || (r->h < 0.0f)) ? true : false;
  320. }
  321. /**
  322. * Determine whether two floating point rectangles are equal, within some
  323. * given epsilon.
  324. *
  325. * Rectangles are considered equal if both are not NULL and each of their x,
  326. * y, width and height are within `epsilon` of each other. If you don't know
  327. * what value to use for `epsilon`, you should call the SDL_RectsEqualFloat
  328. * function instead.
  329. *
  330. * Note that this is a forced-inline function in a header, and not a public
  331. * API function available in the SDL library (which is to say, the code is
  332. * embedded in the calling program and the linker and dynamic loader will not
  333. * be able to find this function inside SDL itself).
  334. *
  335. * \param a the first rectangle to test.
  336. * \param b the second rectangle to test.
  337. * \param epsilon the epsilon value for comparison.
  338. * \returns true if the rectangles are equal, false otherwise.
  339. *
  340. * \threadsafety It is safe to call this function from any thread.
  341. *
  342. * \since This function is available since SDL 3.2.0.
  343. *
  344. * \sa SDL_RectsEqualFloat
  345. */
  346. SDL_FORCE_INLINE bool SDL_RectsEqualEpsilon(const SDL_FRect *a, const SDL_FRect *b, float epsilon)
  347. {
  348. return (a && b && ((a == b) ||
  349. ((SDL_fabsf(a->x - b->x) <= epsilon) &&
  350. (SDL_fabsf(a->y - b->y) <= epsilon) &&
  351. (SDL_fabsf(a->w - b->w) <= epsilon) &&
  352. (SDL_fabsf(a->h - b->h) <= epsilon))))
  353. ? true : false;
  354. }
  355. /**
  356. * Determine whether two floating point rectangles are equal, within a default
  357. * epsilon.
  358. *
  359. * Rectangles are considered equal if both are not NULL and each of their x,
  360. * y, width and height are within SDL_FLT_EPSILON of each other. This is often
  361. * a reasonable way to compare two floating point rectangles and deal with the
  362. * slight precision variations in floating point calculations that tend to pop
  363. * up.
  364. *
  365. * Note that this is a forced-inline function in a header, and not a public
  366. * API function available in the SDL library (which is to say, the code is
  367. * embedded in the calling program and the linker and dynamic loader will not
  368. * be able to find this function inside SDL itself).
  369. *
  370. * \param a the first rectangle to test.
  371. * \param b the second rectangle to test.
  372. * \returns true if the rectangles are equal, false otherwise.
  373. *
  374. * \threadsafety It is safe to call this function from any thread.
  375. *
  376. * \since This function is available since SDL 3.2.0.
  377. *
  378. * \sa SDL_RectsEqualEpsilon
  379. */
  380. SDL_FORCE_INLINE bool SDL_RectsEqualFloat(const SDL_FRect *a, const SDL_FRect *b)
  381. {
  382. return SDL_RectsEqualEpsilon(a, b, SDL_FLT_EPSILON);
  383. }
  384. /**
  385. * Determine whether two rectangles intersect with float precision.
  386. *
  387. * If either pointer is NULL the function will return false.
  388. *
  389. * \param A an SDL_FRect structure representing the first rectangle.
  390. * \param B an SDL_FRect structure representing the second rectangle.
  391. * \returns true if there is an intersection, false otherwise.
  392. *
  393. * \since This function is available since SDL 3.2.0.
  394. *
  395. * \sa SDL_GetRectIntersection
  396. */
  397. extern SDL_DECLSPEC bool SDLCALL SDL_HasRectIntersectionFloat(const SDL_FRect *A, const SDL_FRect *B);
  398. /**
  399. * Calculate the intersection of two rectangles with float precision.
  400. *
  401. * If `result` is NULL then this function will return false.
  402. *
  403. * \param A an SDL_FRect structure representing the first rectangle.
  404. * \param B an SDL_FRect structure representing the second rectangle.
  405. * \param result an SDL_FRect structure filled in with the intersection of
  406. * rectangles `A` and `B`.
  407. * \returns true if there is an intersection, false otherwise.
  408. *
  409. * \since This function is available since SDL 3.2.0.
  410. *
  411. * \sa SDL_HasRectIntersectionFloat
  412. */
  413. extern SDL_DECLSPEC bool SDLCALL SDL_GetRectIntersectionFloat(const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result);
  414. /**
  415. * Calculate the union of two rectangles with float precision.
  416. *
  417. * \param A an SDL_FRect structure representing the first rectangle.
  418. * \param B an SDL_FRect structure representing the second rectangle.
  419. * \param result an SDL_FRect structure filled in with the union of rectangles
  420. * `A` and `B`.
  421. * \returns true on success or false on failure; call SDL_GetError() for more
  422. * information.
  423. *
  424. * \since This function is available since SDL 3.2.0.
  425. */
  426. extern SDL_DECLSPEC bool SDLCALL SDL_GetRectUnionFloat(const SDL_FRect *A, const SDL_FRect *B, SDL_FRect *result);
  427. /**
  428. * Calculate a minimal rectangle enclosing a set of points with float
  429. * precision.
  430. *
  431. * If `clip` is not NULL then only points inside of the clipping rectangle are
  432. * considered.
  433. *
  434. * \param points an array of SDL_FPoint structures representing points to be
  435. * enclosed.
  436. * \param count the number of structures in the `points` array.
  437. * \param clip an SDL_FRect used for clipping or NULL to enclose all points.
  438. * \param result an SDL_FRect structure filled in with the minimal enclosing
  439. * rectangle.
  440. * \returns true if any points were enclosed or false if all the points were
  441. * outside of the clipping rectangle.
  442. *
  443. * \since This function is available since SDL 3.2.0.
  444. */
  445. extern SDL_DECLSPEC bool SDLCALL SDL_GetRectEnclosingPointsFloat(const SDL_FPoint *points, int count, const SDL_FRect *clip, SDL_FRect *result);
  446. /**
  447. * Calculate the intersection of a rectangle and line segment with float
  448. * precision.
  449. *
  450. * This function is used to clip a line segment to a rectangle. A line segment
  451. * contained entirely within the rectangle or that does not intersect will
  452. * remain unchanged. A line segment that crosses the rectangle at either or
  453. * both ends will be clipped to the boundary of the rectangle and the new
  454. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  455. *
  456. * \param rect an SDL_FRect structure representing the rectangle to intersect.
  457. * \param X1 a pointer to the starting X-coordinate of the line.
  458. * \param Y1 a pointer to the starting Y-coordinate of the line.
  459. * \param X2 a pointer to the ending X-coordinate of the line.
  460. * \param Y2 a pointer to the ending Y-coordinate of the line.
  461. * \returns true if there is an intersection, false otherwise.
  462. *
  463. * \since This function is available since SDL 3.2.0.
  464. */
  465. extern SDL_DECLSPEC bool SDLCALL SDL_GetRectAndLineIntersectionFloat(const SDL_FRect *rect, float *X1, float *Y1, float *X2, float *Y2);
  466. /* Ends C function definitions when using C++ */
  467. #ifdef __cplusplus
  468. }
  469. #endif
  470. #include <SDL3/SDL_close_code.h>
  471. #endif /* SDL_rect_h_ */