| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- /*
- Simple DirectMedia Layer
- Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- */
- // This file is #included twice to support int and float versions with the same code.
- static bool SDL_RECT_CAN_OVERFLOW(const RECTTYPE *rect)
- {
- if (rect->x <= (SCALARTYPE)(SDL_MIN_SINT32 / 2) ||
- rect->x >= (SCALARTYPE)(SDL_MAX_SINT32 / 2) ||
- rect->y <= (SCALARTYPE)(SDL_MIN_SINT32 / 2) ||
- rect->y >= (SCALARTYPE)(SDL_MAX_SINT32 / 2) ||
- rect->w >= (SCALARTYPE)(SDL_MAX_SINT32 / 2) ||
- rect->h >= (SCALARTYPE)(SDL_MAX_SINT32 / 2)) {
- return true;
- }
- return false;
- }
- bool SDL_HASINTERSECTION(const RECTTYPE *A, const RECTTYPE *B)
- {
- SCALARTYPE Amin, Amax, Bmin, Bmax;
- CHECK_PARAM(!A) {
- SDL_InvalidParamError("A");
- return false;
- }
- CHECK_PARAM(!B) {
- SDL_InvalidParamError("B");
- return false;
- }
- CHECK_PARAM(SDL_RECT_CAN_OVERFLOW(A) || SDL_RECT_CAN_OVERFLOW(B)) {
- SDL_SetError("Potential rect math overflow");
- return false;
- }
- // Horizontal intersection
- Amin = A->x;
- Amax = Amin + A->w;
- Bmin = B->x;
- Bmax = Bmin + B->w;
- if (Bmin > Amin) {
- Amin = Bmin;
- }
- if (Bmax < Amax) {
- Amax = Bmax;
- }
- if ((Amax - ENCLOSEPOINTS_EPSILON) < Amin) {
- return false;
- }
- // Vertical intersection
- Amin = A->y;
- Amax = Amin + A->h;
- Bmin = B->y;
- Bmax = Bmin + B->h;
- if (Bmin > Amin) {
- Amin = Bmin;
- }
- if (Bmax < Amax) {
- Amax = Bmax;
- }
- if ((Amax - ENCLOSEPOINTS_EPSILON) < Amin) {
- return false;
- }
- return true;
- }
- bool SDL_INTERSECTRECT(const RECTTYPE *A, const RECTTYPE *B, RECTTYPE *result)
- {
- SCALARTYPE Amin, Amax, Bmin, Bmax;
- CHECK_PARAM(!A) {
- SDL_InvalidParamError("A");
- return false;
- }
- CHECK_PARAM(!B) {
- SDL_InvalidParamError("B");
- return false;
- }
- CHECK_PARAM(SDL_RECT_CAN_OVERFLOW(A) || SDL_RECT_CAN_OVERFLOW(B)) {
- SDL_SetError("Potential rect math overflow");
- return false;
- }
- CHECK_PARAM(!result) {
- SDL_InvalidParamError("result");
- return false;
- }
- // Horizontal intersection
- Amin = A->x;
- Amax = Amin + A->w;
- Bmin = B->x;
- Bmax = Bmin + B->w;
- if (Bmin > Amin) {
- Amin = Bmin;
- }
- result->x = Amin;
- if (Bmax < Amax) {
- Amax = Bmax;
- }
- result->w = Amax - Amin;
- // Vertical intersection
- Amin = A->y;
- Amax = Amin + A->h;
- Bmin = B->y;
- Bmax = Bmin + B->h;
- if (Bmin > Amin) {
- Amin = Bmin;
- }
- result->y = Amin;
- if (Bmax < Amax) {
- Amax = Bmax;
- }
- result->h = Amax - Amin;
- return !SDL_RECTEMPTY(result);
- }
- bool SDL_UNIONRECT(const RECTTYPE *A, const RECTTYPE *B, RECTTYPE *result)
- {
- SCALARTYPE Amin, Amax, Bmin, Bmax;
- CHECK_PARAM(!A) {
- return SDL_InvalidParamError("A");
- }
- CHECK_PARAM(!B) {
- return SDL_InvalidParamError("B");
- }
- CHECK_PARAM(SDL_RECT_CAN_OVERFLOW(A) || SDL_RECT_CAN_OVERFLOW(B)) {
- return SDL_SetError("Potential rect math overflow");
- }
- CHECK_PARAM(!result) {
- return SDL_InvalidParamError("result");
- }
- if (SDL_RECTEMPTY(A)) { // Special cases for empty Rects
- if (SDL_RECTEMPTY(B)) { // A and B empty
- SDL_zerop(result);
- } else { // A empty, B not empty
- *result = *B;
- }
- return true;
- } else if (SDL_RECTEMPTY(B)) { // A not empty, B empty
- *result = *A;
- return true;
- }
- // Horizontal union
- Amin = A->x;
- Amax = Amin + A->w;
- Bmin = B->x;
- Bmax = Bmin + B->w;
- if (Bmin < Amin) {
- Amin = Bmin;
- }
- result->x = Amin;
- if (Bmax > Amax) {
- Amax = Bmax;
- }
- result->w = Amax - Amin;
- // Vertical union
- Amin = A->y;
- Amax = Amin + A->h;
- Bmin = B->y;
- Bmax = Bmin + B->h;
- if (Bmin < Amin) {
- Amin = Bmin;
- }
- result->y = Amin;
- if (Bmax > Amax) {
- Amax = Bmax;
- }
- result->h = Amax - Amin;
- return true;
- }
- bool SDL_ENCLOSEPOINTS(const POINTTYPE *points, int count, const RECTTYPE *clip, RECTTYPE *result)
- {
- SCALARTYPE minx = 0;
- SCALARTYPE miny = 0;
- SCALARTYPE maxx = 0;
- SCALARTYPE maxy = 0;
- SCALARTYPE x, y;
- int i;
- CHECK_PARAM(!points) {
- SDL_InvalidParamError("points");
- return false;
- }
- CHECK_PARAM(count < 1) {
- SDL_InvalidParamError("count");
- return false;
- }
- if (clip) {
- bool added = false;
- const SCALARTYPE clip_minx = clip->x;
- const SCALARTYPE clip_miny = clip->y;
- const SCALARTYPE clip_maxx = clip->x + clip->w - ENCLOSEPOINTS_EPSILON;
- const SCALARTYPE clip_maxy = clip->y + clip->h - ENCLOSEPOINTS_EPSILON;
- // Special case for empty rectangle
- if (SDL_RECTEMPTY(clip)) {
- return false;
- }
- for (i = 0; i < count; ++i) {
- x = points[i].x;
- y = points[i].y;
- if (x < clip_minx || x > clip_maxx ||
- y < clip_miny || y > clip_maxy) {
- continue;
- }
- if (!added) {
- // Special case: if no result was requested, we are done
- if (!result) {
- return true;
- }
- // First point added
- minx = maxx = x;
- miny = maxy = y;
- added = true;
- continue;
- }
- if (x < minx) {
- minx = x;
- } else if (x > maxx) {
- maxx = x;
- }
- if (y < miny) {
- miny = y;
- } else if (y > maxy) {
- maxy = y;
- }
- }
- if (!added) {
- return false;
- }
- } else {
- // Special case: if no result was requested, we are done
- if (!result) {
- return true;
- }
- // No clipping, always add the first point
- minx = maxx = points[0].x;
- miny = maxy = points[0].y;
- for (i = 1; i < count; ++i) {
- x = points[i].x;
- y = points[i].y;
- if (x < minx) {
- minx = x;
- } else if (x > maxx) {
- maxx = x;
- }
- if (y < miny) {
- miny = y;
- } else if (y > maxy) {
- maxy = y;
- }
- }
- }
- if (result) {
- result->x = minx;
- result->y = miny;
- result->w = (maxx - minx) + ENCLOSEPOINTS_EPSILON;
- result->h = (maxy - miny) + ENCLOSEPOINTS_EPSILON;
- }
- return true;
- }
- // Use the Cohen-Sutherland algorithm for line clipping
- static int COMPUTEOUTCODE(const RECTTYPE *rect, SCALARTYPE x, SCALARTYPE y)
- {
- int code = 0;
- if (y < rect->y) {
- code |= CODE_TOP;
- } else if (y > (rect->y + rect->h - ENCLOSEPOINTS_EPSILON)) {
- code |= CODE_BOTTOM;
- }
- if (x < rect->x) {
- code |= CODE_LEFT;
- } else if (x > (rect->x + rect->w - ENCLOSEPOINTS_EPSILON)) {
- code |= CODE_RIGHT;
- }
- return code;
- }
- bool SDL_INTERSECTRECTANDLINE(const RECTTYPE *rect, SCALARTYPE *X1, SCALARTYPE *Y1, SCALARTYPE *X2, SCALARTYPE *Y2)
- {
- SCALARTYPE x = 0;
- SCALARTYPE y = 0;
- SCALARTYPE x1, y1;
- SCALARTYPE x2, y2;
- SCALARTYPE rectx1;
- SCALARTYPE recty1;
- SCALARTYPE rectx2;
- SCALARTYPE recty2;
- int outcode1, outcode2;
- CHECK_PARAM(!rect) {
- SDL_InvalidParamError("rect");
- return false;
- }
- CHECK_PARAM(SDL_RECT_CAN_OVERFLOW(rect)) {
- SDL_SetError("Potential rect math overflow");
- return false;
- }
- CHECK_PARAM(!X1) {
- SDL_InvalidParamError("X1");
- return false;
- }
- CHECK_PARAM(!Y1) {
- SDL_InvalidParamError("Y1");
- return false;
- }
- CHECK_PARAM(!X2) {
- SDL_InvalidParamError("X2");
- return false;
- }
- CHECK_PARAM(!Y2) {
- SDL_InvalidParamError("Y2");
- return false;
- }
- if (SDL_RECTEMPTY(rect)) {
- return false; // Special case for empty rect
- }
- x1 = *X1;
- y1 = *Y1;
- x2 = *X2;
- y2 = *Y2;
- rectx1 = rect->x;
- recty1 = rect->y;
- rectx2 = rect->x + rect->w - ENCLOSEPOINTS_EPSILON;
- recty2 = rect->y + rect->h - ENCLOSEPOINTS_EPSILON;
- // Check to see if entire line is inside rect
- if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
- y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
- return true;
- }
- // Check to see if entire line is to one side of rect
- if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
- (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
- return false;
- }
- if (y1 == y2) { // Horizontal line, easy to clip
- if (x1 < rectx1) {
- *X1 = rectx1;
- } else if (x1 > rectx2) {
- *X1 = rectx2;
- }
- if (x2 < rectx1) {
- *X2 = rectx1;
- } else if (x2 > rectx2) {
- *X2 = rectx2;
- }
- return true;
- }
- if (x1 == x2) { // Vertical line, easy to clip
- if (y1 < recty1) {
- *Y1 = recty1;
- } else if (y1 > recty2) {
- *Y1 = recty2;
- }
- if (y2 < recty1) {
- *Y2 = recty1;
- } else if (y2 > recty2) {
- *Y2 = recty2;
- }
- return true;
- }
- // More complicated Cohen-Sutherland algorithm
- outcode1 = COMPUTEOUTCODE(rect, x1, y1);
- outcode2 = COMPUTEOUTCODE(rect, x2, y2);
- while (outcode1 || outcode2) {
- if (outcode1 & outcode2) {
- return false;
- }
- if (outcode1) {
- if (outcode1 & CODE_TOP) {
- y = recty1;
- x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
- } else if (outcode1 & CODE_BOTTOM) {
- y = recty2;
- x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
- } else if (outcode1 & CODE_LEFT) {
- x = rectx1;
- y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
- } else if (outcode1 & CODE_RIGHT) {
- x = rectx2;
- y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
- }
- x1 = x;
- y1 = y;
- outcode1 = COMPUTEOUTCODE(rect, x, y);
- } else {
- if (outcode2 & CODE_TOP) {
- SDL_assert(y2 != y1); // if equal: division by zero.
- y = recty1;
- x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
- } else if (outcode2 & CODE_BOTTOM) {
- SDL_assert(y2 != y1); // if equal: division by zero.
- y = recty2;
- x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
- } else if (outcode2 & CODE_LEFT) {
- /* If this assertion ever fires, here's the static analysis that warned about it:
- http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-b0d01a.html#EndPath */
- SDL_assert(x2 != x1); // if equal: division by zero.
- x = rectx1;
- y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
- } else if (outcode2 & CODE_RIGHT) {
- /* If this assertion ever fires, here's the static analysis that warned about it:
- http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-39b114.html#EndPath */
- SDL_assert(x2 != x1); // if equal: division by zero.
- x = rectx2;
- y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
- }
- x2 = x;
- y2 = y;
- outcode2 = COMPUTEOUTCODE(rect, x, y);
- }
- }
- *X1 = x1;
- *Y1 = y1;
- *X2 = x2;
- *Y2 = y2;
- return true;
- }
- #undef RECTTYPE
- #undef POINTTYPE
- #undef SCALARTYPE
- #undef BIGSCALARTYPE
- #undef COMPUTEOUTCODE
- #undef ENCLOSEPOINTS_EPSILON
- #undef SDL_RECT_CAN_OVERFLOW
- #undef SDL_HASINTERSECTION
- #undef SDL_INTERSECTRECT
- #undef SDL_RECTEMPTY
- #undef SDL_UNIONRECT
- #undef SDL_ENCLOSEPOINTS
- #undef SDL_INTERSECTRECTANDLINE
|