SDL_rect.h 13 KB

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