src/video/SDL_rect.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 12 Sep 2011 09:00:01 -0700
changeset 5902 d3d839b15c2f
parent 5869 065c76282d68
child 5950 36fa6d12784b
permissions -rw-r--r--
Fix regression introducted by added parameter check in SDL_EnclosePoints. Add special case to speedup when no result was requested.
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
aschiffler@5869
    31
    if (!A || !B) {
aschiffler@5869
    32
        // TODO error message
aschiffler@5869
    33
        return SDL_FALSE;
aschiffler@5869
    34
    }
aschiffler@5869
    35
slouken@1895
    36
    /* Horizontal intersection */
slouken@1895
    37
    Amin = A->x;
slouken@1895
    38
    Amax = Amin + A->w;
slouken@1895
    39
    Bmin = B->x;
slouken@1895
    40
    Bmax = Bmin + B->w;
slouken@1895
    41
    if (Bmin > Amin)
slouken@1895
    42
        Amin = Bmin;
slouken@1895
    43
    if (Bmax < Amax)
slouken@1895
    44
        Amax = Bmax;
slouken@1895
    45
    if (Amax <= Amin)
slouken@1895
    46
        return SDL_FALSE;
slouken@1895
    47
slouken@1895
    48
    /* Vertical intersection */
slouken@1895
    49
    Amin = A->y;
slouken@1895
    50
    Amax = Amin + A->h;
slouken@1895
    51
    Bmin = B->y;
slouken@1895
    52
    Bmax = Bmin + B->h;
slouken@1895
    53
    if (Bmin > Amin)
slouken@1895
    54
        Amin = Bmin;
slouken@1895
    55
    if (Bmax < Amax)
slouken@1895
    56
        Amax = Bmax;
slouken@1895
    57
    if (Amax <= Amin)
slouken@1895
    58
        return SDL_FALSE;
slouken@1895
    59
slouken@1895
    60
    return SDL_TRUE;
slouken@1895
    61
}
slouken@1895
    62
slouken@1895
    63
SDL_bool
slouken@1895
    64
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    65
{
slouken@1895
    66
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    67
aschiffler@5869
    68
    if (!A || !B || !result) {
aschiffler@5869
    69
        // TODO error message
aschiffler@5869
    70
        return SDL_FALSE;
aschiffler@5869
    71
    }
aschiffler@5869
    72
slouken@1895
    73
    /* Horizontal intersection */
slouken@1895
    74
    Amin = A->x;
slouken@1895
    75
    Amax = Amin + A->w;
slouken@1895
    76
    Bmin = B->x;
slouken@1895
    77
    Bmax = Bmin + B->w;
slouken@1895
    78
    if (Bmin > Amin)
slouken@1895
    79
        Amin = Bmin;
slouken@1895
    80
    result->x = Amin;
slouken@1895
    81
    if (Bmax < Amax)
slouken@1895
    82
        Amax = Bmax;
slouken@1895
    83
    result->w = Amax - Amin;
slouken@1895
    84
slouken@1895
    85
    /* Vertical intersection */
slouken@1895
    86
    Amin = A->y;
slouken@1895
    87
    Amax = Amin + A->h;
slouken@1895
    88
    Bmin = B->y;
slouken@1895
    89
    Bmax = Bmin + B->h;
slouken@1895
    90
    if (Bmin > Amin)
slouken@1895
    91
        Amin = Bmin;
slouken@1895
    92
    result->y = Amin;
slouken@1895
    93
    if (Bmax < Amax)
slouken@1895
    94
        Amax = Bmax;
slouken@1895
    95
    result->h = Amax - Amin;
slouken@1895
    96
slouken@1895
    97
    return !SDL_RectEmpty(result);
slouken@1895
    98
}
slouken@1895
    99
slouken@1895
   100
void
slouken@1895
   101
SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
   102
{
slouken@1895
   103
    int Amin, Amax, Bmin, Bmax;
slouken@1895
   104
aschiffler@5869
   105
    if (!A || !B || !result) {
aschiffler@5869
   106
        return;
aschiffler@5869
   107
    }
aschiffler@5869
   108
slouken@1895
   109
    /* Horizontal union */
slouken@1895
   110
    Amin = A->x;
slouken@1895
   111
    Amax = Amin + A->w;
slouken@1895
   112
    Bmin = B->x;
slouken@1895
   113
    Bmax = Bmin + B->w;
slouken@1895
   114
    if (Bmin < Amin)
slouken@1895
   115
        Amin = Bmin;
slouken@1895
   116
    result->x = Amin;
slouken@1895
   117
    if (Bmax > Amax)
slouken@1895
   118
        Amax = Bmax;
slouken@1895
   119
    result->w = Amax - Amin;
slouken@1895
   120
slouken@1895
   121
    /* Vertical intersection */
slouken@1895
   122
    Amin = A->y;
slouken@1895
   123
    Amax = Amin + A->h;
slouken@1895
   124
    Bmin = B->y;
slouken@1895
   125
    Bmax = Bmin + B->h;
slouken@1895
   126
    if (Bmin < Amin)
slouken@1895
   127
        Amin = Bmin;
slouken@1895
   128
    result->y = Amin;
slouken@1895
   129
    if (Bmax > Amax)
slouken@1895
   130
        Amax = Bmax;
slouken@1895
   131
    result->h = Amax - Amin;
slouken@1895
   132
}
slouken@1895
   133
slouken@2909
   134
SDL_bool
slouken@3536
   135
SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
slouken@3536
   136
                  SDL_Rect * result)
slouken@3536
   137
{
slouken@4456
   138
    int minx = 0;
slouken@4456
   139
    int miny = 0;
slouken@4456
   140
    int maxx = 0;
slouken@4456
   141
    int maxy = 0;
slouken@3536
   142
    int x, y, i;
slouken@3536
   143
aschiffler@5902
   144
    if (!points) {
aschiffler@5869
   145
        // TODO error message
aschiffler@5869
   146
        return SDL_FALSE;
aschiffler@5869
   147
    }
aschiffler@5869
   148
slouken@3536
   149
    if (count < 1) {
aschiffler@5869
   150
        // TODO error message
slouken@3536
   151
        return SDL_FALSE;
slouken@3536
   152
    }
slouken@3536
   153
slouken@3536
   154
    if (clip) {
slouken@3536
   155
        SDL_bool added = SDL_FALSE;
slouken@3536
   156
        int clip_minx = clip->x;
slouken@3536
   157
        int clip_miny = clip->y;
slouken@3536
   158
        int clip_maxx = clip->x+clip->w-1;
slouken@3536
   159
        int clip_maxy = clip->y+clip->h-1;
slouken@3536
   160
slouken@3536
   161
        for (i = 0; i < count; ++i) {
slouken@3536
   162
            x = points[i].x;
slouken@3536
   163
            y = points[i].y;
slouken@3536
   164
slouken@3536
   165
            if (x < clip_minx || x > clip_maxx ||
slouken@3536
   166
                y < clip_miny || y > clip_maxy) {
slouken@3536
   167
                continue;
slouken@3536
   168
            }
slouken@3536
   169
            if (!added) {
aschiffler@5902
   170
                /* Special case: if no result was requested, we are done */
aschiffler@5902
   171
                if (result == NULL) {
aschiffler@5902
   172
                    return SDL_TRUE;
aschiffler@5902
   173
                }
aschiffler@5902
   174
aschiffler@5902
   175
                /* First point added */
slouken@3536
   176
                minx = maxx = x;
slouken@3536
   177
                miny = maxy = y;
slouken@3536
   178
                added = SDL_TRUE;
slouken@3536
   179
                continue;
slouken@3536
   180
            }
slouken@3536
   181
            if (x < minx) {
slouken@3536
   182
                minx = x;
slouken@3536
   183
            } else if (x > maxx) {
slouken@3536
   184
                maxx = x;
slouken@3536
   185
            }
slouken@3536
   186
            if (y < miny) {
slouken@3536
   187
                miny = y;
slouken@3536
   188
            } else if (y > maxy) {
slouken@3536
   189
                maxy = y;
slouken@3536
   190
            }
slouken@3536
   191
        }
slouken@3536
   192
        if (!added) {
slouken@3536
   193
            return SDL_FALSE;
slouken@3536
   194
        }
slouken@3536
   195
    } else {
aschiffler@5902
   196
        /* Special case: if no result was requested, we are done */
aschiffler@5902
   197
        if (result == NULL) {
aschiffler@5902
   198
            return SDL_TRUE;
aschiffler@5902
   199
        }
aschiffler@5902
   200
        
slouken@3536
   201
        /* No clipping, always add the first point */
slouken@3536
   202
        minx = maxx = points[0].x;
slouken@3536
   203
        miny = maxy = points[0].y;
slouken@3536
   204
slouken@3536
   205
        for (i = 1; i < count; ++i) {
slouken@3536
   206
            x = points[i].x;
slouken@3536
   207
            y = points[i].y;
slouken@3536
   208
slouken@3536
   209
            if (x < minx) {
slouken@3536
   210
                minx = x;
slouken@3536
   211
            } else if (x > maxx) {
slouken@3536
   212
                maxx = x;
slouken@3536
   213
            }
slouken@3536
   214
            if (y < miny) {
slouken@3536
   215
                miny = y;
slouken@3536
   216
            } else if (y > maxy) {
slouken@3536
   217
                maxy = y;
slouken@3536
   218
            }
slouken@3536
   219
        }
slouken@3536
   220
    }
slouken@3536
   221
slouken@3536
   222
    if (result) {
slouken@3536
   223
        result->x = minx;
slouken@3536
   224
        result->y = miny;
slouken@3536
   225
        result->w = (maxx-minx)+1;
slouken@3536
   226
        result->h = (maxy-miny)+1;
slouken@3536
   227
    }
slouken@3536
   228
    return SDL_TRUE;
slouken@3536
   229
}
slouken@3536
   230
slouken@3541
   231
/* Use the Cohen-Sutherland algorithm for line clipping */
slouken@3541
   232
#define CODE_BOTTOM 1
slouken@3541
   233
#define CODE_TOP    2
slouken@3541
   234
#define CODE_LEFT   4
slouken@3541
   235
#define CODE_RIGHT  8
slouken@3541
   236
slouken@3541
   237
static int ComputeOutCode(const SDL_Rect * rect, int x, int y)
slouken@3541
   238
{
slouken@3541
   239
    int code = 0;
slouken@3541
   240
    if (y < 0) {
slouken@3541
   241
        code |= CODE_TOP;
slouken@3541
   242
    } else if (y >= rect->y + rect->h) {
slouken@3541
   243
        code |= CODE_BOTTOM;
slouken@3541
   244
    }
slouken@3541
   245
    if (x < 0) {
slouken@3541
   246
        code |= CODE_LEFT;
slouken@3541
   247
    } else if (x >= rect->x + rect->w) {
slouken@3541
   248
        code |= CODE_RIGHT;
slouken@3541
   249
    }
slouken@3541
   250
    return code;
slouken@3541
   251
}
slouken@3541
   252
slouken@3536
   253
SDL_bool
slouken@2920
   254
SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
slouken@2920
   255
                         int *Y2)
slouken@2909
   256
{
slouken@4456
   257
    int x = 0;
slouken@4456
   258
    int y = 0;
slouken@2909
   259
    int x1, y1;
slouken@2909
   260
    int x2, y2;
slouken@2909
   261
    int rectx1;
slouken@2909
   262
    int recty1;
slouken@2909
   263
    int rectx2;
slouken@2909
   264
    int recty2;
slouken@3541
   265
    int outcode1, outcode2;
slouken@2909
   266
slouken@2909
   267
    if (!rect || !X1 || !Y1 || !X2 || !Y2) {
aschiffler@5869
   268
        // TODO error message
slouken@3046
   269
        return SDL_FALSE;
slouken@2909
   270
    }
slouken@2909
   271
slouken@2909
   272
    x1 = *X1;
slouken@2909
   273
    y1 = *Y1;
slouken@2909
   274
    x2 = *X2;
slouken@2909
   275
    y2 = *Y2;
slouken@2909
   276
    rectx1 = rect->x;
slouken@2909
   277
    recty1 = rect->y;
slouken@2909
   278
    rectx2 = rect->x + rect->w - 1;
slouken@2909
   279
    recty2 = rect->y + rect->h - 1;
slouken@2909
   280
slouken@2909
   281
    /* Check to see if entire line is inside rect */
slouken@2909
   282
    if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
slouken@2909
   283
        y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
slouken@2909
   284
        return SDL_TRUE;
slouken@2909
   285
    }
slouken@2909
   286
slouken@2994
   287
    /* Check to see if entire line is to one side of rect */
slouken@2909
   288
    if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
slouken@3004
   289
        (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
slouken@2909
   290
        return SDL_FALSE;
slouken@2909
   291
    }
slouken@2909
   292
slouken@2994
   293
    if (y1 == y2) {
slouken@2909
   294
        /* Horizontal line, easy to clip */
slouken@2909
   295
        if (x1 < rectx1) {
slouken@2909
   296
            *X1 = rectx1;
slouken@2909
   297
        } else if (x1 > rectx2) {
slouken@2909
   298
            *X1 = rectx2;
slouken@2909
   299
        }
slouken@2909
   300
        if (x2 < rectx1) {
slouken@2909
   301
            *X2 = rectx1;
slouken@2909
   302
        } else if (x2 > rectx2) {
slouken@2909
   303
            *X2 = rectx2;
slouken@2909
   304
        }
slouken@2909
   305
        return SDL_TRUE;
slouken@2909
   306
    }
slouken@2909
   307
slouken@2909
   308
    if (x1 == x2) {
slouken@2909
   309
        /* Vertical line, easy to clip */
slouken@2909
   310
        if (y1 < recty1) {
slouken@2909
   311
            *Y1 = recty1;
slouken@2909
   312
        } else if (y1 > recty2) {
slouken@2909
   313
            *Y1 = recty2;
slouken@2909
   314
        }
slouken@2909
   315
        if (y2 < recty1) {
slouken@2909
   316
            *Y2 = recty1;
slouken@2909
   317
        } else if (y2 > recty2) {
slouken@2909
   318
            *Y2 = recty2;
slouken@2909
   319
        }
slouken@2909
   320
        return SDL_TRUE;
slouken@2909
   321
    }
slouken@2909
   322
slouken@3541
   323
    /* More complicated Cohen-Sutherland algorithm */
slouken@3541
   324
    outcode1 = ComputeOutCode(rect, x1, y1);
slouken@3541
   325
    outcode2 = ComputeOutCode(rect, x2, y2);
slouken@3541
   326
    while (outcode1 || outcode2) {
slouken@3541
   327
        if (outcode1 & outcode2) {
slouken@3541
   328
            return SDL_FALSE;
slouken@2994
   329
        }
slouken@2994
   330
slouken@3541
   331
        if (outcode1) {
slouken@3541
   332
            if (outcode1 & CODE_TOP) {
slouken@3541
   333
                y = recty1;
slouken@3541
   334
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   335
            } else if (outcode1 & CODE_BOTTOM) {
slouken@3541
   336
                y = recty2;
slouken@3541
   337
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   338
            } else if (outcode1 & CODE_LEFT) {
slouken@3541
   339
                x = rectx1;
slouken@3541
   340
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   341
            } else if (outcode1 & CODE_RIGHT) {
slouken@3541
   342
                x = rectx2;
slouken@3541
   343
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   344
            }
slouken@3541
   345
            x1 = x;
slouken@3541
   346
            y1 = y;
slouken@3541
   347
            outcode1 = ComputeOutCode(rect, x, y);
slouken@3542
   348
        } else {
slouken@3541
   349
            if (outcode2 & CODE_TOP) {
slouken@3541
   350
                y = recty1;
slouken@3541
   351
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   352
            } else if (outcode2 & CODE_BOTTOM) {
slouken@3541
   353
                y = recty2;
slouken@3541
   354
                x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
slouken@3541
   355
            } else if (outcode2 & CODE_LEFT) {
slouken@3541
   356
                x = rectx1;
slouken@3541
   357
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   358
            } else if (outcode2 & CODE_RIGHT) {
slouken@3541
   359
                x = rectx2;
slouken@3541
   360
                y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
slouken@3541
   361
            }
slouken@3541
   362
            x2 = x;
slouken@3541
   363
            y2 = y;
slouken@3541
   364
            outcode2 = ComputeOutCode(rect, x, y);
slouken@2994
   365
        }
slouken@2994
   366
    }
slouken@3541
   367
    *X1 = x1;
slouken@3541
   368
    *Y1 = y1;
slouken@3541
   369
    *X2 = x2;
slouken@3541
   370
    *Y2 = y2;
slouken@3541
   371
    return SDL_TRUE;
slouken@2909
   372
}
slouken@2909
   373
slouken@5294
   374
SDL_bool
slouken@5294
   375
SDL_GetSpanEnclosingRect(int width, int height,
slouken@5294
   376
                         int numrects, SDL_Rect * rects, SDL_Rect *span)
slouken@5294
   377
{
slouken@5294
   378
    int i;
slouken@5294
   379
    int span_y1, span_y2;
slouken@5294
   380
    int rect_y1, rect_y2;
slouken@5294
   381
aschiffler@5869
   382
    if (width < 1 || height < 1) {
aschiffler@5869
   383
        // TODO error message
aschiffler@5869
   384
        return SDL_FALSE;
aschiffler@5869
   385
    }
aschiffler@5869
   386
aschiffler@5869
   387
    if (!rects || !span) {
aschiffler@5869
   388
        // TODO error message
aschiffler@5869
   389
        return SDL_FALSE;
aschiffler@5869
   390
    }
aschiffler@5869
   391
aschiffler@5869
   392
    if (numrects < 1) {
aschiffler@5869
   393
        // TODO error message
aschiffler@5869
   394
        return SDL_FALSE;
aschiffler@5869
   395
    }
aschiffler@5869
   396
slouken@5294
   397
    /* Initialize to empty rect */
slouken@5294
   398
    span_y1 = height;
slouken@5294
   399
    span_y2 = 0;
slouken@5294
   400
slouken@5294
   401
    for (i = 0; i < numrects; ++i) {
slouken@5294
   402
        rect_y1 = rects[i].y;
slouken@5294
   403
        rect_y2 = rect_y1 + rects[i].h;
slouken@5294
   404
slouken@5294
   405
        /* Clip out of bounds rectangles, and expand span rect */
slouken@5294
   406
        if (rect_y1 < 0) {
slouken@5294
   407
            span_y1 = 0;
slouken@5294
   408
        } else if (rect_y1 < span_y1) {
slouken@5294
   409
            span_y1 = rect_y1;
slouken@5294
   410
        }
slouken@5294
   411
        if (rect_y2 > height) {
slouken@5294
   412
            span_y2 = height;
slouken@5294
   413
        } else if (rect_y2 > span_y2) {
slouken@5294
   414
            span_y2 = rect_y2;
slouken@5294
   415
        }
slouken@5294
   416
    }
slouken@5294
   417
    if (span_y2 > span_y1) {
slouken@5294
   418
        span->x = 0;
slouken@5294
   419
        span->y = span_y1;
slouken@5294
   420
        span->w = width;
slouken@5294
   421
        span->h = (span_y2 - span_y1);
slouken@5294
   422
        return SDL_TRUE;
slouken@5294
   423
    }
slouken@5294
   424
    return SDL_FALSE;
slouken@5294
   425
}
slouken@5294
   426
slouken@1895
   427
/* vi: set ts=4 sw=4 expandtab: */