Mercurial > sdl-ios-xcode
diff src/video/SDL_rect.c @ 2909:3da0bb421d83
Added line clipping
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 23 Dec 2008 02:23:18 +0000 |
parents | 99210400e8b9 |
children | cdb01906cb7e |
line wrap: on
line diff
--- a/src/video/SDL_rect.c Tue Dec 23 01:28:06 2008 +0000 +++ b/src/video/SDL_rect.c Tue Dec 23 02:23:18 2008 +0000 @@ -118,6 +118,75 @@ result->h = Amax - Amin; } +SDL_bool +SDL_IntersectRectAndLine(const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2) +{ + int x1, y1; + int x2, y2; + int rectx1; + int recty1; + int rectx2; + int recty2; + + if (!rect || !X1 || !Y1 || !X2 || !Y2) { + SDL_FALSE; + } + + x1 = *X1; + y1 = *Y1; + x2 = *X2; + y2 = *Y2; + rectx1 = rect->x; + recty1 = rect->y; + rectx2 = rect->x + rect->w - 1; + recty2 = rect->y + rect->h - 1; + + /* 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 SDL_TRUE; + } + + /* Check to see if entire line is outside rect */ + if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) || + (y1 < recty1 && y2 < recty2) || (y1 > recty2 && y2 > recty2)) { + return SDL_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 SDL_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 SDL_TRUE; + } + + /* FIXME: need code to clip diagonal line to rect */ + return SDL_FALSE; +} + void SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect) {