SDL_rect_impl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. // This file is #included twice to support int and float versions with the same code.
  19. static bool SDL_RECT_CAN_OVERFLOW(const RECTTYPE *rect)
  20. {
  21. if (rect->x <= (SCALARTYPE)(SDL_MIN_SINT32 / 2) ||
  22. rect->x >= (SCALARTYPE)(SDL_MAX_SINT32 / 2) ||
  23. rect->y <= (SCALARTYPE)(SDL_MIN_SINT32 / 2) ||
  24. rect->y >= (SCALARTYPE)(SDL_MAX_SINT32 / 2) ||
  25. rect->w >= (SCALARTYPE)(SDL_MAX_SINT32 / 2) ||
  26. rect->h >= (SCALARTYPE)(SDL_MAX_SINT32 / 2)) {
  27. return true;
  28. }
  29. return false;
  30. }
  31. bool SDL_HASINTERSECTION(const RECTTYPE *A, const RECTTYPE *B)
  32. {
  33. SCALARTYPE Amin, Amax, Bmin, Bmax;
  34. if (!A) {
  35. SDL_InvalidParamError("A");
  36. return false;
  37. } else if (!B) {
  38. SDL_InvalidParamError("B");
  39. return false;
  40. } else if (SDL_RECT_CAN_OVERFLOW(A) ||
  41. SDL_RECT_CAN_OVERFLOW(B)) {
  42. SDL_SetError("Potential rect math overflow");
  43. return false;
  44. } else if (SDL_RECTEMPTY(A) || SDL_RECTEMPTY(B)) {
  45. return false; // Special cases for empty rects
  46. }
  47. // Horizontal intersection
  48. Amin = A->x;
  49. Amax = Amin + A->w;
  50. Bmin = B->x;
  51. Bmax = Bmin + B->w;
  52. if (Bmin > Amin) {
  53. Amin = Bmin;
  54. }
  55. if (Bmax < Amax) {
  56. Amax = Bmax;
  57. }
  58. if ((Amax - ENCLOSEPOINTS_EPSILON) < Amin) {
  59. return false;
  60. }
  61. // Vertical intersection
  62. Amin = A->y;
  63. Amax = Amin + A->h;
  64. Bmin = B->y;
  65. Bmax = Bmin + B->h;
  66. if (Bmin > Amin) {
  67. Amin = Bmin;
  68. }
  69. if (Bmax < Amax) {
  70. Amax = Bmax;
  71. }
  72. if ((Amax - ENCLOSEPOINTS_EPSILON) < Amin) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool SDL_INTERSECTRECT(const RECTTYPE *A, const RECTTYPE *B, RECTTYPE *result)
  78. {
  79. SCALARTYPE Amin, Amax, Bmin, Bmax;
  80. if (!A) {
  81. SDL_InvalidParamError("A");
  82. return false;
  83. } else if (!B) {
  84. SDL_InvalidParamError("B");
  85. return false;
  86. } else if (SDL_RECT_CAN_OVERFLOW(A) ||
  87. SDL_RECT_CAN_OVERFLOW(B)) {
  88. SDL_SetError("Potential rect math overflow");
  89. return false;
  90. } else if (!result) {
  91. SDL_InvalidParamError("result");
  92. return false;
  93. } else if (SDL_RECTEMPTY(A) || SDL_RECTEMPTY(B)) { // Special cases for empty rects
  94. result->w = 0;
  95. result->h = 0;
  96. return false;
  97. }
  98. // Horizontal intersection
  99. Amin = A->x;
  100. Amax = Amin + A->w;
  101. Bmin = B->x;
  102. Bmax = Bmin + B->w;
  103. if (Bmin > Amin) {
  104. Amin = Bmin;
  105. }
  106. result->x = Amin;
  107. if (Bmax < Amax) {
  108. Amax = Bmax;
  109. }
  110. result->w = Amax - Amin;
  111. // Vertical intersection
  112. Amin = A->y;
  113. Amax = Amin + A->h;
  114. Bmin = B->y;
  115. Bmax = Bmin + B->h;
  116. if (Bmin > Amin) {
  117. Amin = Bmin;
  118. }
  119. result->y = Amin;
  120. if (Bmax < Amax) {
  121. Amax = Bmax;
  122. }
  123. result->h = Amax - Amin;
  124. return !SDL_RECTEMPTY(result);
  125. }
  126. bool SDL_UNIONRECT(const RECTTYPE *A, const RECTTYPE *B, RECTTYPE *result)
  127. {
  128. SCALARTYPE Amin, Amax, Bmin, Bmax;
  129. if (!A) {
  130. return SDL_InvalidParamError("A");
  131. } else if (!B) {
  132. return SDL_InvalidParamError("B");
  133. } else if (SDL_RECT_CAN_OVERFLOW(A) ||
  134. SDL_RECT_CAN_OVERFLOW(B)) {
  135. return SDL_SetError("Potential rect math overflow");
  136. } else if (!result) {
  137. return SDL_InvalidParamError("result");
  138. } else if (SDL_RECTEMPTY(A)) { // Special cases for empty Rects
  139. if (SDL_RECTEMPTY(B)) { // A and B empty
  140. SDL_zerop(result);
  141. } else { // A empty, B not empty
  142. *result = *B;
  143. }
  144. return true;
  145. } else if (SDL_RECTEMPTY(B)) { // A not empty, B empty
  146. *result = *A;
  147. return true;
  148. }
  149. // Horizontal union
  150. Amin = A->x;
  151. Amax = Amin + A->w;
  152. Bmin = B->x;
  153. Bmax = Bmin + B->w;
  154. if (Bmin < Amin) {
  155. Amin = Bmin;
  156. }
  157. result->x = Amin;
  158. if (Bmax > Amax) {
  159. Amax = Bmax;
  160. }
  161. result->w = Amax - Amin;
  162. // Vertical union
  163. Amin = A->y;
  164. Amax = Amin + A->h;
  165. Bmin = B->y;
  166. Bmax = Bmin + B->h;
  167. if (Bmin < Amin) {
  168. Amin = Bmin;
  169. }
  170. result->y = Amin;
  171. if (Bmax > Amax) {
  172. Amax = Bmax;
  173. }
  174. result->h = Amax - Amin;
  175. return true;
  176. }
  177. bool SDL_ENCLOSEPOINTS(const POINTTYPE *points, int count, const RECTTYPE *clip, RECTTYPE *result)
  178. {
  179. SCALARTYPE minx = 0;
  180. SCALARTYPE miny = 0;
  181. SCALARTYPE maxx = 0;
  182. SCALARTYPE maxy = 0;
  183. SCALARTYPE x, y;
  184. int i;
  185. if (!points) {
  186. SDL_InvalidParamError("points");
  187. return false;
  188. } else if (count < 1) {
  189. SDL_InvalidParamError("count");
  190. return false;
  191. }
  192. if (clip) {
  193. bool added = false;
  194. const SCALARTYPE clip_minx = clip->x;
  195. const SCALARTYPE clip_miny = clip->y;
  196. const SCALARTYPE clip_maxx = clip->x + clip->w - ENCLOSEPOINTS_EPSILON;
  197. const SCALARTYPE clip_maxy = clip->y + clip->h - ENCLOSEPOINTS_EPSILON;
  198. // Special case for empty rectangle
  199. if (SDL_RECTEMPTY(clip)) {
  200. return false;
  201. }
  202. for (i = 0; i < count; ++i) {
  203. x = points[i].x;
  204. y = points[i].y;
  205. if (x < clip_minx || x > clip_maxx ||
  206. y < clip_miny || y > clip_maxy) {
  207. continue;
  208. }
  209. if (!added) {
  210. // Special case: if no result was requested, we are done
  211. if (!result) {
  212. return true;
  213. }
  214. // First point added
  215. minx = maxx = x;
  216. miny = maxy = y;
  217. added = true;
  218. continue;
  219. }
  220. if (x < minx) {
  221. minx = x;
  222. } else if (x > maxx) {
  223. maxx = x;
  224. }
  225. if (y < miny) {
  226. miny = y;
  227. } else if (y > maxy) {
  228. maxy = y;
  229. }
  230. }
  231. if (!added) {
  232. return false;
  233. }
  234. } else {
  235. // Special case: if no result was requested, we are done
  236. if (!result) {
  237. return true;
  238. }
  239. // No clipping, always add the first point
  240. minx = maxx = points[0].x;
  241. miny = maxy = points[0].y;
  242. for (i = 1; i < count; ++i) {
  243. x = points[i].x;
  244. y = points[i].y;
  245. if (x < minx) {
  246. minx = x;
  247. } else if (x > maxx) {
  248. maxx = x;
  249. }
  250. if (y < miny) {
  251. miny = y;
  252. } else if (y > maxy) {
  253. maxy = y;
  254. }
  255. }
  256. }
  257. if (result) {
  258. result->x = minx;
  259. result->y = miny;
  260. result->w = (maxx - minx) + ENCLOSEPOINTS_EPSILON;
  261. result->h = (maxy - miny) + ENCLOSEPOINTS_EPSILON;
  262. }
  263. return true;
  264. }
  265. // Use the Cohen-Sutherland algorithm for line clipping
  266. static int COMPUTEOUTCODE(const RECTTYPE *rect, SCALARTYPE x, SCALARTYPE y)
  267. {
  268. int code = 0;
  269. if (y < rect->y) {
  270. code |= CODE_TOP;
  271. } else if (y > (rect->y + rect->h - ENCLOSEPOINTS_EPSILON)) {
  272. code |= CODE_BOTTOM;
  273. }
  274. if (x < rect->x) {
  275. code |= CODE_LEFT;
  276. } else if (x > (rect->x + rect->w - ENCLOSEPOINTS_EPSILON)) {
  277. code |= CODE_RIGHT;
  278. }
  279. return code;
  280. }
  281. bool SDL_INTERSECTRECTANDLINE(const RECTTYPE *rect, SCALARTYPE *X1, SCALARTYPE *Y1, SCALARTYPE *X2, SCALARTYPE *Y2)
  282. {
  283. SCALARTYPE x = 0;
  284. SCALARTYPE y = 0;
  285. SCALARTYPE x1, y1;
  286. SCALARTYPE x2, y2;
  287. SCALARTYPE rectx1;
  288. SCALARTYPE recty1;
  289. SCALARTYPE rectx2;
  290. SCALARTYPE recty2;
  291. int outcode1, outcode2;
  292. if (!rect) {
  293. SDL_InvalidParamError("rect");
  294. return false;
  295. } else if (SDL_RECT_CAN_OVERFLOW(rect)) {
  296. SDL_SetError("Potential rect math overflow");
  297. return false;
  298. } else if (!X1) {
  299. SDL_InvalidParamError("X1");
  300. return false;
  301. } else if (!Y1) {
  302. SDL_InvalidParamError("Y1");
  303. return false;
  304. } else if (!X2) {
  305. SDL_InvalidParamError("X2");
  306. return false;
  307. } else if (!Y2) {
  308. SDL_InvalidParamError("Y2");
  309. return false;
  310. } else if (SDL_RECTEMPTY(rect)) {
  311. return false; // Special case for empty rect
  312. }
  313. x1 = *X1;
  314. y1 = *Y1;
  315. x2 = *X2;
  316. y2 = *Y2;
  317. rectx1 = rect->x;
  318. recty1 = rect->y;
  319. rectx2 = rect->x + rect->w - ENCLOSEPOINTS_EPSILON;
  320. recty2 = rect->y + rect->h - ENCLOSEPOINTS_EPSILON;
  321. // Check to see if entire line is inside rect
  322. if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
  323. y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
  324. return true;
  325. }
  326. // Check to see if entire line is to one side of rect
  327. if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
  328. (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
  329. return false;
  330. }
  331. if (y1 == y2) { // Horizontal line, easy to clip
  332. if (x1 < rectx1) {
  333. *X1 = rectx1;
  334. } else if (x1 > rectx2) {
  335. *X1 = rectx2;
  336. }
  337. if (x2 < rectx1) {
  338. *X2 = rectx1;
  339. } else if (x2 > rectx2) {
  340. *X2 = rectx2;
  341. }
  342. return true;
  343. }
  344. if (x1 == x2) { // Vertical line, easy to clip
  345. if (y1 < recty1) {
  346. *Y1 = recty1;
  347. } else if (y1 > recty2) {
  348. *Y1 = recty2;
  349. }
  350. if (y2 < recty1) {
  351. *Y2 = recty1;
  352. } else if (y2 > recty2) {
  353. *Y2 = recty2;
  354. }
  355. return true;
  356. }
  357. // More complicated Cohen-Sutherland algorithm
  358. outcode1 = COMPUTEOUTCODE(rect, x1, y1);
  359. outcode2 = COMPUTEOUTCODE(rect, x2, y2);
  360. while (outcode1 || outcode2) {
  361. if (outcode1 & outcode2) {
  362. return false;
  363. }
  364. if (outcode1) {
  365. if (outcode1 & CODE_TOP) {
  366. y = recty1;
  367. x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
  368. } else if (outcode1 & CODE_BOTTOM) {
  369. y = recty2;
  370. x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
  371. } else if (outcode1 & CODE_LEFT) {
  372. x = rectx1;
  373. y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
  374. } else if (outcode1 & CODE_RIGHT) {
  375. x = rectx2;
  376. y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
  377. }
  378. x1 = x;
  379. y1 = y;
  380. outcode1 = COMPUTEOUTCODE(rect, x, y);
  381. } else {
  382. if (outcode2 & CODE_TOP) {
  383. SDL_assert(y2 != y1); // if equal: division by zero.
  384. y = recty1;
  385. x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
  386. } else if (outcode2 & CODE_BOTTOM) {
  387. SDL_assert(y2 != y1); // if equal: division by zero.
  388. y = recty2;
  389. x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
  390. } else if (outcode2 & CODE_LEFT) {
  391. /* If this assertion ever fires, here's the static analysis that warned about it:
  392. http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-b0d01a.html#EndPath */
  393. SDL_assert(x2 != x1); // if equal: division by zero.
  394. x = rectx1;
  395. y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
  396. } else if (outcode2 & CODE_RIGHT) {
  397. /* If this assertion ever fires, here's the static analysis that warned about it:
  398. http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-39b114.html#EndPath */
  399. SDL_assert(x2 != x1); // if equal: division by zero.
  400. x = rectx2;
  401. y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
  402. }
  403. x2 = x;
  404. y2 = y;
  405. outcode2 = COMPUTEOUTCODE(rect, x, y);
  406. }
  407. }
  408. *X1 = x1;
  409. *Y1 = y1;
  410. *X2 = x2;
  411. *Y2 = y2;
  412. return true;
  413. }
  414. #undef RECTTYPE
  415. #undef POINTTYPE
  416. #undef SCALARTYPE
  417. #undef BIGSCALARTYPE
  418. #undef COMPUTEOUTCODE
  419. #undef ENCLOSEPOINTS_EPSILON
  420. #undef SDL_RECT_CAN_OVERFLOW
  421. #undef SDL_HASINTERSECTION
  422. #undef SDL_INTERSECTRECT
  423. #undef SDL_RECTEMPTY
  424. #undef SDL_UNIONRECT
  425. #undef SDL_ENCLOSEPOINTS
  426. #undef SDL_INTERSECTRECTANDLINE