SDL_rect.h 18 KB

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