SDL_rect.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. * \file SDL_rect.h
  20. *
  21. * Header file for SDL_rect definition and management functions.
  22. */
  23. #ifndef SDL_rect_h_
  24. #define SDL_rect_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_begin_code.h>
  28. /* Set up for C function definitions, even when using C++ */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /**
  33. * The structure that defines a point (using integers).
  34. *
  35. * \since This struct is available since SDL 3.0.0.
  36. *
  37. * \sa SDL_GetRectEnclosingPoints
  38. * \sa SDL_PointInRect
  39. */
  40. typedef struct SDL_Point
  41. {
  42. int x;
  43. int y;
  44. } SDL_Point;
  45. /**
  46. * The structure that defines a point (using floating point values).
  47. *
  48. * \since This struct is available since SDL 3.0.0.
  49. *
  50. * \sa SDL_GetRectEnclosingPointsFloat
  51. * \sa SDL_PointInRectFloat
  52. */
  53. typedef struct SDL_FPoint
  54. {
  55. float x;
  56. float y;
  57. } SDL_FPoint;
  58. /**
  59. * A rectangle, with the origin at the upper left (using integers).
  60. *
  61. * \since This struct is available since SDL 3.0.0.
  62. *
  63. * \sa SDL_RectEmpty
  64. * \sa SDL_RectsEqual
  65. * \sa SDL_HasRectIntersection
  66. * \sa SDL_GetRectIntersection
  67. * \sa SDL_GetRectAndLineIntersection
  68. * \sa SDL_GetRectUnion
  69. * \sa SDL_GetRectEnclosingPoints
  70. */
  71. typedef struct SDL_Rect
  72. {
  73. int x, y;
  74. int w, h;
  75. } SDL_Rect;
  76. /**
  77. * A rectangle, with the origin at the upper left (using floating point
  78. * values).
  79. *
  80. * \since This struct is available since SDL 3.0.0.
  81. *
  82. * \sa SDL_RectEmptyFloat
  83. * \sa SDL_RectsEqualFloat
  84. * \sa SDL_RectsEqualEpsilon
  85. * \sa SDL_HasRectIntersectionFloat
  86. * \sa SDL_GetRectIntersectionFloat
  87. * \sa SDL_GetRectAndLineIntersectionFloat
  88. * \sa SDL_GetRectUnionFloat
  89. * \sa SDL_GetRectEnclosingPointsFloat
  90. * \sa SDL_PointInRectFloat
  91. */
  92. typedef struct SDL_FRect
  93. {
  94. float x;
  95. float y;
  96. float w;
  97. float h;
  98. } SDL_FRect;
  99. /**
  100. * Returns true if point resides inside a rectangle.
  101. */
  102. SDL_FORCE_INLINE SDL_bool SDL_PointInRect(const SDL_Point *p, const SDL_Rect *r)
  103. {
  104. return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  105. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  106. }
  107. /**
  108. * Returns true if the rectangle has no area.
  109. */
  110. SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
  111. {
  112. return ((!r) || (r->w <= 0) || (r->h <= 0)) ? SDL_TRUE : SDL_FALSE;
  113. }
  114. /**
  115. * Returns true if the two rectangles are equal.
  116. */
  117. SDL_FORCE_INLINE SDL_bool SDL_RectsEqual(const SDL_Rect *a, const SDL_Rect *b)
  118. {
  119. return (a && b && (a->x == b->x) && (a->y == b->y) &&
  120. (a->w == b->w) && (a->h == b->h)) ? SDL_TRUE : SDL_FALSE;
  121. }
  122. /**
  123. * Determine whether two rectangles intersect.
  124. *
  125. * If either pointer is NULL the function will return SDL_FALSE.
  126. *
  127. * \param A an SDL_Rect structure representing the first rectangle
  128. * \param B an SDL_Rect structure representing the second rectangle
  129. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  130. *
  131. * \since This function is available since SDL 3.0.0.
  132. *
  133. * \sa SDL_GetRectIntersection
  134. */
  135. extern DECLSPEC SDL_bool SDLCALL SDL_HasRectIntersection(const SDL_Rect * A,
  136. const SDL_Rect * B);
  137. /**
  138. * Calculate the intersection of two rectangles.
  139. *
  140. * If `result` is NULL then this function will return SDL_FALSE.
  141. *
  142. * \param A an SDL_Rect structure representing the first rectangle
  143. * \param B an SDL_Rect structure representing the second rectangle
  144. * \param result an SDL_Rect structure filled in with the intersection of
  145. * rectangles `A` and `B`
  146. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  147. *
  148. * \since This function is available since SDL 3.0.0.
  149. *
  150. * \sa SDL_HasRectIntersection
  151. */
  152. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectIntersection(const SDL_Rect * A,
  153. const SDL_Rect * B,
  154. SDL_Rect * result);
  155. /**
  156. * Calculate the union of two rectangles.
  157. *
  158. * \param A an SDL_Rect structure representing the first rectangle
  159. * \param B an SDL_Rect structure representing the second rectangle
  160. * \param result an SDL_Rect structure filled in with the union of rectangles
  161. * `A` and `B`
  162. * \returns 0 on success or a negative error code on failure; call
  163. * SDL_GetError() for more information.
  164. *
  165. * \since This function is available since SDL 3.0.0.
  166. */
  167. extern DECLSPEC int SDLCALL SDL_GetRectUnion(const SDL_Rect * A,
  168. const SDL_Rect * B,
  169. SDL_Rect * result);
  170. /**
  171. * Calculate a minimal rectangle enclosing a set of points.
  172. *
  173. * If `clip` is not NULL then only points inside of the clipping rectangle are
  174. * considered.
  175. *
  176. * \param points an array of SDL_Point structures representing points to be
  177. * enclosed
  178. * \param count the number of structures in the `points` array
  179. * \param clip an SDL_Rect used for clipping or NULL to enclose all points
  180. * \param result an SDL_Rect structure filled in with the minimal enclosing
  181. * rectangle
  182. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  183. * points were outside of the clipping rectangle.
  184. *
  185. * \since This function is available since SDL 3.0.0.
  186. */
  187. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectEnclosingPoints(const SDL_Point * points,
  188. int count,
  189. const SDL_Rect * clip,
  190. SDL_Rect * result);
  191. /**
  192. * Calculate the intersection of a rectangle and line segment.
  193. *
  194. * This function is used to clip a line segment to a rectangle. A line segment
  195. * contained entirely within the rectangle or that does not intersect will
  196. * remain unchanged. A line segment that crosses the rectangle at either or
  197. * both ends will be clipped to the boundary of the rectangle and the new
  198. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  199. *
  200. * \param rect an SDL_Rect structure representing the rectangle to intersect
  201. * \param X1 a pointer to the starting X-coordinate of the line
  202. * \param Y1 a pointer to the starting Y-coordinate of the line
  203. * \param X2 a pointer to the ending X-coordinate of the line
  204. * \param Y2 a pointer to the ending Y-coordinate of the line
  205. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  206. *
  207. * \since This function is available since SDL 3.0.0.
  208. */
  209. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectAndLineIntersection(const SDL_Rect *
  210. rect, int *X1,
  211. int *Y1, int *X2,
  212. int *Y2);
  213. /* SDL_FRect versions... */
  214. /**
  215. * Returns true if point resides inside a rectangle.
  216. */
  217. SDL_FORCE_INLINE SDL_bool SDL_PointInRectFloat(const SDL_FPoint *p, const SDL_FRect *r)
  218. {
  219. return ( (p->x >= r->x) && (p->x < (r->x + r->w)) &&
  220. (p->y >= r->y) && (p->y < (r->y + r->h)) ) ? SDL_TRUE : SDL_FALSE;
  221. }
  222. /**
  223. * Returns true if the rectangle has no area.
  224. *
  225. * \since This function is available since SDL 3.0.0.
  226. */
  227. SDL_FORCE_INLINE SDL_bool SDL_RectEmptyFloat(const SDL_FRect *r)
  228. {
  229. return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
  230. }
  231. /**
  232. * Returns true if the two rectangles are equal, within some given epsilon.
  233. *
  234. * \since This function is available since SDL 3.0.0.
  235. */
  236. SDL_FORCE_INLINE SDL_bool SDL_RectsEqualEpsilon(const SDL_FRect *a, const SDL_FRect *b, const float epsilon)
  237. {
  238. return (a && b && ((a == b) ||
  239. ((SDL_fabsf(a->x - b->x) <= epsilon) &&
  240. (SDL_fabsf(a->y - b->y) <= epsilon) &&
  241. (SDL_fabsf(a->w - b->w) <= epsilon) &&
  242. (SDL_fabsf(a->h - b->h) <= epsilon))))
  243. ? SDL_TRUE : SDL_FALSE;
  244. }
  245. /**
  246. * Returns true if the two rectangles are equal, using a default epsilon.
  247. *
  248. * \since This function is available since SDL 3.0.0.
  249. */
  250. SDL_FORCE_INLINE SDL_bool SDL_RectsEqualFloat(const SDL_FRect *a, const SDL_FRect *b)
  251. {
  252. return SDL_RectsEqualEpsilon(a, b, SDL_FLT_EPSILON);
  253. }
  254. /**
  255. * Determine whether two rectangles intersect with float precision.
  256. *
  257. * If either pointer is NULL the function will return SDL_FALSE.
  258. *
  259. * \param A an SDL_FRect structure representing the first rectangle
  260. * \param B an SDL_FRect structure representing the second rectangle
  261. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  262. *
  263. * \since This function is available since SDL 3.0.0.
  264. *
  265. * \sa SDL_GetRectIntersection
  266. */
  267. extern DECLSPEC SDL_bool SDLCALL SDL_HasRectIntersectionFloat(const SDL_FRect * A,
  268. const SDL_FRect * B);
  269. /**
  270. * Calculate the intersection of two rectangles with float precision.
  271. *
  272. * If `result` is NULL then this function will return SDL_FALSE.
  273. *
  274. * \param A an SDL_FRect structure representing the first rectangle
  275. * \param B an SDL_FRect structure representing the second rectangle
  276. * \param result an SDL_FRect structure filled in with the intersection of
  277. * rectangles `A` and `B`
  278. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  279. *
  280. * \since This function is available since SDL 3.0.0.
  281. *
  282. * \sa SDL_HasRectIntersectionFloat
  283. */
  284. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectIntersectionFloat(const SDL_FRect * A,
  285. const SDL_FRect * B,
  286. SDL_FRect * result);
  287. /**
  288. * Calculate the union of two rectangles with float precision.
  289. *
  290. * \param A an SDL_FRect structure representing the first rectangle
  291. * \param B an SDL_FRect structure representing the second rectangle
  292. * \param result an SDL_FRect structure filled in with the union of rectangles
  293. * `A` and `B`
  294. * \returns 0 on success or a negative error code on failure; call
  295. * SDL_GetError() for more information.
  296. *
  297. * \since This function is available since SDL 3.0.0.
  298. */
  299. extern DECLSPEC int SDLCALL SDL_GetRectUnionFloat(const SDL_FRect * A,
  300. const SDL_FRect * B,
  301. SDL_FRect * result);
  302. /**
  303. * Calculate a minimal rectangle enclosing a set of points with float
  304. * precision.
  305. *
  306. * If `clip` is not NULL then only points inside of the clipping rectangle are
  307. * considered.
  308. *
  309. * \param points an array of SDL_FPoint structures representing points to be
  310. * enclosed
  311. * \param count the number of structures in the `points` array
  312. * \param clip an SDL_FRect used for clipping or NULL to enclose all points
  313. * \param result an SDL_FRect structure filled in with the minimal enclosing
  314. * rectangle
  315. * \returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the
  316. * points were outside of the clipping rectangle.
  317. *
  318. * \since This function is available since SDL 3.0.0.
  319. */
  320. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectEnclosingPointsFloat(const SDL_FPoint * points,
  321. int count,
  322. const SDL_FRect * clip,
  323. SDL_FRect * result);
  324. /**
  325. * Calculate the intersection of a rectangle and line segment with float
  326. * precision.
  327. *
  328. * This function is used to clip a line segment to a rectangle. A line segment
  329. * contained entirely within the rectangle or that does not intersect will
  330. * remain unchanged. A line segment that crosses the rectangle at either or
  331. * both ends will be clipped to the boundary of the rectangle and the new
  332. * coordinates saved in `X1`, `Y1`, `X2`, and/or `Y2` as necessary.
  333. *
  334. * \param rect an SDL_FRect structure representing the rectangle to intersect
  335. * \param X1 a pointer to the starting X-coordinate of the line
  336. * \param Y1 a pointer to the starting Y-coordinate of the line
  337. * \param X2 a pointer to the ending X-coordinate of the line
  338. * \param Y2 a pointer to the ending Y-coordinate of the line
  339. * \returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
  340. *
  341. * \since This function is available since SDL 3.0.0.
  342. */
  343. extern DECLSPEC SDL_bool SDLCALL SDL_GetRectAndLineIntersectionFloat(const SDL_FRect *
  344. rect, float *X1,
  345. float *Y1, float *X2,
  346. float *Y2);
  347. /* Ends C function definitions when using C++ */
  348. #ifdef __cplusplus
  349. }
  350. #endif
  351. #include <SDL3/SDL_close_code.h>
  352. #endif /* SDL_rect_h_ */