SDL_rect.h 19 KB

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