SDL_rect_impl.h 12 KB

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