SDL_rect.h 12 KB

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