SDL_rect.h 18 KB

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