SDL_rect_impl.h 13 KB

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