src/video/SDL_rect.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5294 4801b8bede31
child 5869 065c76282d68
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
slouken@1895
    21
#include "SDL_config.h"
slouken@1895
    22
slouken@5154
    23
#include "SDL_rect.h"
slouken@1895
    24
slouken@5294
    25
slouken@1895
    26
SDL_bool
slouken@1895
    27
SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
slouken@1895
    28
{
slouken@1895
    29
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    30
slouken@1895
    31
    /* Horizontal intersection */
slouken@1895
    32
    Amin = A->x;
slouken@1895
    33
    Amax = Amin + A->w;
slouken@1895
    34
    Bmin = B->x;
slouken@1895
    35
    Bmax = Bmin + B->w;
slouken@1895
    36
    if (Bmin > Amin)
slouken@1895
    37
        Amin = Bmin;
slouken@1895
    38
    if (Bmax < Amax)
slouken@1895
    39
        Amax = Bmax;
slouken@1895
    40
    if (Amax <= Amin)
slouken@1895
    41
        return SDL_FALSE;
slouken@1895
    42
slouken@1895
    43
    /* Vertical intersection */
slouken@1895
    44
    Amin = A->y;
slouken@1895
    45
    Amax = Amin + A->h;
slouken@1895
    46
    Bmin = B->y;
slouken@1895
    47
    Bmax = Bmin + B->h;
slouken@1895
    48
    if (Bmin > Amin)
slouken@1895
    49
        Amin = Bmin;
slouken@1895
    50
    if (Bmax < Amax)
slouken@1895
    51
        Amax = Bmax;
slouken@1895
    52
    if (Amax <= Amin)
slouken@1895
    53
        return SDL_FALSE;
slouken@1895
    54
slouken@1895
    55
    return SDL_TRUE;
slouken@1895
    56
}
slouken@1895
    57
slouken@1895
    58
SDL_bool
slouken@1895
    59
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    60
{
slouken@1895
    61
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    62
slouken@1895
    63
    /* Horizontal intersection */
slouken@1895
    64
    Amin = A->x;
slouken@1895
    65
    Amax = Amin + A->w;
slouken@1895
    66
    Bmin = B->x;
slouken@1895
    67
    Bmax = Bmin + B->w;
slouken@1895
    68
    if (Bmin > Amin)
slouken@1895
    69
        Amin = Bmin;
slouken@1895
    70
    result->x = Amin;
slouken@1895
    71
    if (Bmax < Amax)
slouken@1895
    72
        Amax = Bmax;
slouken@1895
    73
    result->w = Amax - Amin;
slouken@1895
    74
slouken@1895
    75
    /* Vertical intersection */
slouken@1895
    76
    Amin = A->y;
slouken@1895
    77
    Amax = Amin + A->h;
slouken@1895
    78
    Bmin = B->y;
slouken@1895
    79
    Bmax = Bmin + B->h;
slouken@1895
    80
    if (Bmin > Amin)
slouken@1895
    81
        Amin = Bmin;
slouken@1895
    82
    result->y = Amin;
slouken@1895
    83
    if (Bmax < Amax)
slouken@1895
    84
        Amax = Bmax;
slouken@1895
    85
    result->h = Amax - Amin;
slouken@1895
    86
slouken@1895
    87
    return !SDL_RectEmpty(result);
slouken@1895
    88
}
slouken@1895
    89
slouken@1895
    90
void
slouken@1895
    91
SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    92
{
slouken@1895
    93
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    94
slouken@1895
    95
    /* Horizontal union */
slouken@1895
    96
    Amin = A->x;
slouken@1895
    97
    Amax = Amin + A->w;
slouken@1895
    98
    Bmin = B->x;
slouken@1895
    99
    Bmax = Bmin + B->w;
slouken@1895
   100
    if (Bmin < Amin)
slouken@1895
   101
        Amin = Bmin;
slouken@1895
   102
    result->x = Amin;
slouken@1895
   103
    if (Bmax > Amax)
slouken@1895
   104
        Amax = Bmax;
slouken@1895
   105
    result->w = Amax - Amin;
slouken@1895
   106
slouken@1895
   107
    /* Vertical intersection */
slouken@1895
   108
    Amin = A->y;
slouken@1895
   109
    Amax = Amin + A->h;
slouken@1895
   110
    Bmin = B->y;
slouken@1895
   111
    Bmax = Bmin + B->h;
slouken@1895
   112
    if (Bmin < Amin)
slouken@1895
   113
        Amin = Bmin;
slouken@1895
   114
    result->y = Amin;
slouken@1895
   115
    if (Bmax > Amax)
slouken@1895
   116
        Amax = Bmax;
slouken@1895
   117
    result->h = Amax - Amin;
slouken@1895
   118
}
slouken@1895
   119
slouken@2909
   120
SDL_bool
slouken@3536
   121
SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
slouken@3536
   122
                  SDL_Rect * result)
slouken@3536
   123
{
slouken@4456
   124
    int minx = 0;
slouken@4456
   125
    int miny = 0;
slouken@4456
   126
    int maxx = 0;
slouken@4456
   127
    int maxy = 0;
slouken@3536
   128
    int x, y, i;
slouken@3536
   129
slouken@3536
   130
    if (count < 1) {
slouken@3536
   131
        return SDL_FALSE;
slouken@3536
   132
    }
slouken@3536
   133
slouken@3536
   134
    if (clip) {
slouken@3536
   135
        SDL_bool added = SDL_FALSE;
slouken@3536
   136
        int clip_minx = clip->x;
slouken@3536
   137
        int clip_miny = clip->y;
slouken@3536
   138
        int clip_maxx = clip->x+clip->w-1;
slouken@3536
   139
        int clip_maxy = clip->y+clip->h-1;
slouken@3536
   140
slouken@3536
   141
        for (i = 0; i < count; ++i) {
slouken@3536
   142
            x = points[i].x;
slouken@3536
   143
            y = points[i].y;
slouken@3536
   144
slouken@3536
   145
            if (x < clip_minx || x > clip_maxx ||
slouken@3536
   146
                y < clip_miny || y > clip_maxy) {
slouken@3536
   147
                continue;
slouken@3536
   148
            }
slouken@3536
   149
            if (!added) {
slouken@3536
   150
                minx = maxx = x;
slouken@3536
   151
                miny = maxy = y;
slouken@3536
   152
                added = SDL_TRUE;
slouken@3536
   153
                continue;
slouken@3536
   154
            }
slouken@3536
   155
            if (x < minx) {
slouken@3536
   156
                minx = x;
slouken@3536
   157
            } else if (x > maxx) {
slouken@3536
   158
                maxx = x;
slouken@3536
   159
            }
slouken@3536
   160
            if (y < miny) {
slouken@3536
   161
                miny = y;
slouken@3536
   162
            } else if (y > maxy) {
slouken@3536
   163
                maxy = y;
slouken@3536
   164
            }
slouken@3536
   165
        }
slouken@3536
   166
        if (!added) {
slouken@3536
   167
            return SDL_FALSE;
slouken@3536
   168
        }
slouken@3536
   169
    } else {
slouken@3536
   170
        /* No clipping, always add the first point */
slouken@3536
   171
        minx = maxx = points[0].x;
slouken@3536
   172
        miny = maxy = points[0].y;
slouken@3536
   173
slouken@3536
   174
        for (i = 1; i < count; ++i) {
slouken@3536
   175
            x = points[i].x;
slouken@3536
   176
            y = points[i].y;
slouken@3536
   177
slouken@3536
   178
            if (x < minx) {
slouken@3536
   179
                minx = x;
slouken@3536
   180
            } else if (x > maxx) {
slouken@3536
   181
                maxx = x;
slouken@3536
   182
            }
slouken@3536
   183
            if (y < miny) {
slouken@3536
   184
                miny = y;
slouken@3536
   185
            } else if (y > maxy) {
slouken@3536
   186
                maxy = y;
slouken@3536
   187
            }
slouken@3536
   188
        }
slouken@3536
   189
    }
slouken@3536
   190
slouken@3536
   191
    if (result) {
slouken@3536
   192
        result->x = minx;
slouken@3536
   193
        result->y = miny;
slouken@3536
   194
        result->w = (maxx-minx)+1;
slouken@3536
   195
        result->h = (maxy-miny)+1;
slouken@3536
   196
    }
slouken@3536
   197
    return SDL_TRUE;
slouken@3536
   198
}
slouken@3536
   199
slouken@3541
   200
/* Use the Cohen-Sutherland algorithm for line clipping */
slouken@3541
   201
#define CODE_BOTTOM 1
slouken@3541
   202
#define CODE_TOP    2
slouken@3541
   203
#define CODE_LEFT   4
slouken@3541
   204
#define CODE_RIGHT  8
slouken@3541
   205
slouken@3541
   206
static int ComputeOutCode(const SDL_Rect * rect, int x, int y)
slouken@3541
   207
{
slouken@3541
   208
    int code = 0;
slouken@3541
   209
    if (y < 0) {
slouken@3541
   210
        code |= CODE_TOP;
slouken@3541
   211
    } else if (y >= rect->y + rect->h) {
slouken@3541
   212
        code |= CODE_BOTTOM;
slouken@3541
   213
    }
slouken@3541
   214
    if (x < 0) {
slouken@3541
   215
        code |= CODE_LEFT;
slouken@3541
   216
    } else if (x >= rect->x + rect->w) {
slouken@3541
   217
        code |= CODE_RIGHT;
slouken@3541
   218
    }
slouken@3541
   219
    return code;
slouken@3541
   220
}
slouken@3541
   221
slouken@3536
   222
SDL_bool
slouken@2920
   223
SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
slouken@2920
   224
                         int *Y2)
slouken@2909
   225
{
slouken@4456
   226
    int x = 0;
slouken@4456
   227
    int y = 0;
slouken@2909
   228
    int x1, y1;
slouken@2909
   229
    int x2, y2;
slouken@2909
   230
    int rectx1;
slouken@2909
   231
    int recty1;
slouken@2909
   232
    int rectx2;
slouken@2909
   233
    int recty2;
slouken@3541
   234
    int outcode1, outcode2;
slouken@2909
   235
slouken@2909
   236
    if (!rect || !X1 || !Y1 || !X2 || !Y2) {
slouken@3046
   237
        return SDL_FALSE;
slouken@2909
   238
    }
slouken@2909
   239
slouken@2909
   240
    x1 = *X1;
slouken@2909
   241
    y1 = *Y1;
slouken@2909
   242
    x2 = *X2;
slouken@2909
   243
    y2 = *Y2;
slouken@2909
   244
    rectx1 = rect->x;
slouken@2909
   245
    recty1 = rect->y;
slouken@2909
   246
    rectx2 = rect->x + rect->w - 1;
slouken@2909
   247
    recty2 = rect->y + rect->h - 1;
slouken@2909
   248
slouken@2909
   249
    /* Check to see if entire line is inside rect */
slouken@2909
   250
    if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
slouken@2909
   251
        y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
slouken@2909
   252
        return SDL_TRUE;
slouken@2909
   253
    }
slouken@2909
   254
slouken@2994
   255
    /* Check to see if entire line is to one side of rect */
slouken@2909
   256
    if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
slouken@3004
   257
        (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
slouken@2909
   258
        return SDL_FALSE;
slouken@2909
   259
    }
slouken@2909
   260
slouken@2994
   261
    if (y1 == y2) {
slouken@2909
   262
        /* Horizontal line, easy to clip */
slouken@2909
   263
        if (x1 < rectx1) {
slouken@2909
   264
            *X1 = rectx1;
slouken@2909
   265
        } else if (x1 > rectx2) {
slouken@2909
   266
            *X1 = rectx2;
slouken@2909
   267
        }
slouken@2909
   268
        if (x2 < rectx1) {
slouken@2909
   269
            *X2 = rectx1;
slouken@2909
   270
        } else if (x2 > rectx2) {
slouken@2909
   271
            *X2 = rectx2;
slouken@2909
   272
        }
slouken@2909
   273
        return SDL_TRUE;
slouken@2909
   274
    }
slouken@2909
   275
slouken@2909
   276
    if (x1 == x2) {
slouken@2909
   277
        /* Vertical line, easy to clip */
slouken@2909
   278
        if (y1 < recty1) {
slouken@2909
   279
            *Y1 = recty1;
slouken@2909
   280
        } else if (y1 > recty2) {
slouken@2909
   281
            *Y1 = recty2;
slouken@2909
   282
        }
slouken@2909
   283
        if (y2 < recty1) {
slouken@2909
   284
            *Y2 = recty1;
slouken@2909
   285
        } else if (y2 > recty2) {
slouken@2909
   286
            *Y2 = recty2;
slouken@2909
   287
        }
slouken@2909
   288
        return SDL_TRUE;
slouken@2909
   289
    }
slouken@2909
   290
slouken@3541
   291
    /* More complicated Cohen-Sutherland algorithm */
slouken@3541
   292
    outcode1 = ComputeOutCode(rect, x1, y1);
slouken@3541
   293
    outcode2 = ComputeOutCode(rect, x2, y2);
slouken@3541
   294
    while (outcode1 || outcode2) {
slouken@3541
   295
        if (outcode1 & outcode2) {
slouken@3541
   296
            return SDL_FALSE;
slouken@2994
   297
        }
slouken@2994
   298
slouken@3541
   299
        if (outcode1) {
slouken@3541
   300
            if (outcode1 & CODE_TOP) {
slouken@3541
   301
                y = recty1;
slouken@3541
   302
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   303
            } else if (outcode1 & CODE_BOTTOM) {
slouken@3541
   304
                y = recty2;
slouken@3541
   305
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   306
            } else if (outcode1 & CODE_LEFT) {
slouken@3541
   307
                x = rectx1;
slouken@3541
   308
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   309
            } else if (outcode1 & CODE_RIGHT) {
slouken@3541
   310
                x = rectx2;
slouken@3541
   311
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   312
            }
slouken@3541
   313
            x1 = x;
slouken@3541
   314
            y1 = y;
slouken@3541
   315
            outcode1 = ComputeOutCode(rect, x, y);
slouken@3542
   316
        } else {
slouken@3541
   317
            if (outcode2 & CODE_TOP) {
slouken@3541
   318
                y = recty1;
slouken@3541
   319
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   320
            } else if (outcode2 & CODE_BOTTOM) {
slouken@3541
   321
                y = recty2;
slouken@3541
   322
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   323
            } else if (outcode2 & CODE_LEFT) {
slouken@3541
   324
                x = rectx1;
slouken@3541
   325
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   326
            } else if (outcode2 & CODE_RIGHT) {
slouken@3541
   327
                x = rectx2;
slouken@3541
   328
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   329
            }
slouken@3541
   330
            x2 = x;
slouken@3541
   331
            y2 = y;
slouken@3541
   332
            outcode2 = ComputeOutCode(rect, x, y);
slouken@2994
   333
        }
slouken@2994
   334
    }
slouken@3541
   335
    *X1 = x1;
slouken@3541
   336
    *Y1 = y1;
slouken@3541
   337
    *X2 = x2;
slouken@3541
   338
    *Y2 = y2;
slouken@3541
   339
    return SDL_TRUE;
slouken@2909
   340
}
slouken@2909
   341
slouken@5294
   342
SDL_bool
slouken@5294
   343
SDL_GetSpanEnclosingRect(int width, int height,
slouken@5294
   344
                         int numrects, SDL_Rect * rects, SDL_Rect *span)
slouken@5294
   345
{
slouken@5294
   346
    int i;
slouken@5294
   347
    int span_y1, span_y2;
slouken@5294
   348
    int rect_y1, rect_y2;
slouken@5294
   349
slouken@5294
   350
    /* Initialize to empty rect */
slouken@5294
   351
    span_y1 = height;
slouken@5294
   352
    span_y2 = 0;
slouken@5294
   353
slouken@5294
   354
    for (i = 0; i < numrects; ++i) {
slouken@5294
   355
        rect_y1 = rects[i].y;
slouken@5294
   356
        rect_y2 = rect_y1 + rects[i].h;
slouken@5294
   357
slouken@5294
   358
        /* Clip out of bounds rectangles, and expand span rect */
slouken@5294
   359
        if (rect_y1 < 0) {
slouken@5294
   360
            span_y1 = 0;
slouken@5294
   361
        } else if (rect_y1 < span_y1) {
slouken@5294
   362
            span_y1 = rect_y1;
slouken@5294
   363
        }
slouken@5294
   364
        if (rect_y2 > height) {
slouken@5294
   365
            span_y2 = height;
slouken@5294
   366
        } else if (rect_y2 > span_y2) {
slouken@5294
   367
            span_y2 = rect_y2;
slouken@5294
   368
        }
slouken@5294
   369
    }
slouken@5294
   370
    if (span_y2 > span_y1) {
slouken@5294
   371
        span->x = 0;
slouken@5294
   372
        span->y = span_y1;
slouken@5294
   373
        span->w = width;
slouken@5294
   374
        span->h = (span_y2 - span_y1);
slouken@5294
   375
        return SDL_TRUE;
slouken@5294
   376
    }
slouken@5294
   377
    return SDL_FALSE;
slouken@5294
   378
}
slouken@5294
   379
slouken@1895
   380
/* vi: set ts=4 sw=4 expandtab: */