src/video/SDL_rect.c
changeset 3536 0267b8b1595c
parent 3046 47965eacde88
child 3541 0c429a5fda8a
     1.1 --- a/src/video/SDL_rect.c	Mon Dec 07 10:08:24 2009 +0000
     1.2 +++ b/src/video/SDL_rect.c	Wed Dec 09 15:56:56 2009 +0000
     1.3 @@ -119,6 +119,84 @@
     1.4  }
     1.5  
     1.6  SDL_bool
     1.7 +SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
     1.8 +                  SDL_Rect * result)
     1.9 +{
    1.10 +    int minx, miny;
    1.11 +    int maxx, maxy;
    1.12 +    int x, y, i;
    1.13 +
    1.14 +    if (count < 1) {
    1.15 +        return SDL_FALSE;
    1.16 +    }
    1.17 +
    1.18 +    if (clip) {
    1.19 +        SDL_bool added = SDL_FALSE;
    1.20 +        int clip_minx = clip->x;
    1.21 +        int clip_miny = clip->y;
    1.22 +        int clip_maxx = clip->x+clip->w-1;
    1.23 +        int clip_maxy = clip->y+clip->h-1;
    1.24 +
    1.25 +        for (i = 0; i < count; ++i) {
    1.26 +            x = points[i].x;
    1.27 +            y = points[i].y;
    1.28 +
    1.29 +            if (x < clip_minx || x > clip_maxx ||
    1.30 +                y < clip_miny || y > clip_maxy) {
    1.31 +                continue;
    1.32 +            }
    1.33 +            if (!added) {
    1.34 +                minx = maxx = x;
    1.35 +                miny = maxy = y;
    1.36 +                added = SDL_TRUE;
    1.37 +                continue;
    1.38 +            }
    1.39 +            if (x < minx) {
    1.40 +                minx = x;
    1.41 +            } else if (x > maxx) {
    1.42 +                maxx = x;
    1.43 +            }
    1.44 +            if (y < miny) {
    1.45 +                miny = y;
    1.46 +            } else if (y > maxy) {
    1.47 +                maxy = y;
    1.48 +            }
    1.49 +        }
    1.50 +        if (!added) {
    1.51 +            return SDL_FALSE;
    1.52 +        }
    1.53 +    } else {
    1.54 +        /* No clipping, always add the first point */
    1.55 +        minx = maxx = points[0].x;
    1.56 +        miny = maxy = points[0].y;
    1.57 +
    1.58 +        for (i = 1; i < count; ++i) {
    1.59 +            x = points[i].x;
    1.60 +            y = points[i].y;
    1.61 +
    1.62 +            if (x < minx) {
    1.63 +                minx = x;
    1.64 +            } else if (x > maxx) {
    1.65 +                maxx = x;
    1.66 +            }
    1.67 +            if (y < miny) {
    1.68 +                miny = y;
    1.69 +            } else if (y > maxy) {
    1.70 +                maxy = y;
    1.71 +            }
    1.72 +        }
    1.73 +    }
    1.74 +
    1.75 +    if (result) {
    1.76 +        result->x = minx;
    1.77 +        result->y = miny;
    1.78 +        result->w = (maxx-minx)+1;
    1.79 +        result->h = (maxy-miny)+1;
    1.80 +    }
    1.81 +    return SDL_TRUE;
    1.82 +}
    1.83 +
    1.84 +SDL_bool
    1.85  SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
    1.86                           int *Y2)
    1.87  {