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