src/video/SDL_rect.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2009 19:33:21 +0000
changeset 2994 7563b99e9a49
parent 2920 cdb01906cb7e
child 2997 e4f025078c1c
permissions -rw-r--r--
Date: Sat, 3 Jan 2009 22:11:18 -0500
From: "Donny Viszneki"
Subject: Re: [SDL] Want to help with SDL 1.3?

>> > For example, here's a good quick project for someone from the TODO list:
>> > * Add diagonal line clipping to SDL_IntersectRectAndLine()

Just wanted to point out that the patch is available at
http://codebad.com/rect-line-ix.patch

I hereby grant Sam Lantinga an irrevocable non-exclusive distribution
license to this patch to do with as he wishes.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#include "SDL_video.h"
slouken@1895
    25
#include "SDL_rect_c.h"
slouken@1895
    26
slouken@1895
    27
SDL_bool
slouken@1895
    28
SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
slouken@1895
    29
{
slouken@1895
    30
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    31
slouken@1895
    32
    /* Horizontal intersection */
slouken@1895
    33
    Amin = A->x;
slouken@1895
    34
    Amax = Amin + A->w;
slouken@1895
    35
    Bmin = B->x;
slouken@1895
    36
    Bmax = Bmin + B->w;
slouken@1895
    37
    if (Bmin > Amin)
slouken@1895
    38
        Amin = Bmin;
slouken@1895
    39
    if (Bmax < Amax)
slouken@1895
    40
        Amax = Bmax;
slouken@1895
    41
    if (Amax <= Amin)
slouken@1895
    42
        return SDL_FALSE;
slouken@1895
    43
slouken@1895
    44
    /* Vertical intersection */
slouken@1895
    45
    Amin = A->y;
slouken@1895
    46
    Amax = Amin + A->h;
slouken@1895
    47
    Bmin = B->y;
slouken@1895
    48
    Bmax = Bmin + B->h;
slouken@1895
    49
    if (Bmin > Amin)
slouken@1895
    50
        Amin = Bmin;
slouken@1895
    51
    if (Bmax < Amax)
slouken@1895
    52
        Amax = Bmax;
slouken@1895
    53
    if (Amax <= Amin)
slouken@1895
    54
        return SDL_FALSE;
slouken@1895
    55
slouken@1895
    56
    return SDL_TRUE;
slouken@1895
    57
}
slouken@1895
    58
slouken@1895
    59
SDL_bool
slouken@1895
    60
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    61
{
slouken@1895
    62
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    63
slouken@1895
    64
    /* Horizontal intersection */
slouken@1895
    65
    Amin = A->x;
slouken@1895
    66
    Amax = Amin + A->w;
slouken@1895
    67
    Bmin = B->x;
slouken@1895
    68
    Bmax = Bmin + B->w;
slouken@1895
    69
    if (Bmin > Amin)
slouken@1895
    70
        Amin = Bmin;
slouken@1895
    71
    result->x = Amin;
slouken@1895
    72
    if (Bmax < Amax)
slouken@1895
    73
        Amax = Bmax;
slouken@1895
    74
    result->w = Amax - Amin;
slouken@1895
    75
slouken@1895
    76
    /* Vertical intersection */
slouken@1895
    77
    Amin = A->y;
slouken@1895
    78
    Amax = Amin + A->h;
slouken@1895
    79
    Bmin = B->y;
slouken@1895
    80
    Bmax = Bmin + B->h;
slouken@1895
    81
    if (Bmin > Amin)
slouken@1895
    82
        Amin = Bmin;
slouken@1895
    83
    result->y = Amin;
slouken@1895
    84
    if (Bmax < Amax)
slouken@1895
    85
        Amax = Bmax;
slouken@1895
    86
    result->h = Amax - Amin;
slouken@1895
    87
slouken@1895
    88
    return !SDL_RectEmpty(result);
slouken@1895
    89
}
slouken@1895
    90
slouken@1895
    91
void
slouken@1895
    92
SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
slouken@1895
    93
{
slouken@1895
    94
    int Amin, Amax, Bmin, Bmax;
slouken@1895
    95
slouken@1895
    96
    /* Horizontal union */
slouken@1895
    97
    Amin = A->x;
slouken@1895
    98
    Amax = Amin + A->w;
slouken@1895
    99
    Bmin = B->x;
slouken@1895
   100
    Bmax = Bmin + B->w;
slouken@1895
   101
    if (Bmin < Amin)
slouken@1895
   102
        Amin = Bmin;
slouken@1895
   103
    result->x = Amin;
slouken@1895
   104
    if (Bmax > Amax)
slouken@1895
   105
        Amax = Bmax;
slouken@1895
   106
    result->w = Amax - Amin;
slouken@1895
   107
slouken@1895
   108
    /* Vertical intersection */
slouken@1895
   109
    Amin = A->y;
slouken@1895
   110
    Amax = Amin + A->h;
slouken@1895
   111
    Bmin = B->y;
slouken@1895
   112
    Bmax = Bmin + B->h;
slouken@1895
   113
    if (Bmin < Amin)
slouken@1895
   114
        Amin = Bmin;
slouken@1895
   115
    result->y = Amin;
slouken@1895
   116
    if (Bmax > Amax)
slouken@1895
   117
        Amax = Bmax;
slouken@1895
   118
    result->h = Amax - Amin;
slouken@1895
   119
}
slouken@1895
   120
slouken@2909
   121
SDL_bool
slouken@2920
   122
SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
slouken@2920
   123
                         int *Y2)
slouken@2909
   124
{
slouken@2909
   125
    int x1, y1;
slouken@2909
   126
    int x2, y2;
slouken@2909
   127
    int rectx1;
slouken@2909
   128
    int recty1;
slouken@2909
   129
    int rectx2;
slouken@2909
   130
    int recty2;
slouken@2909
   131
slouken@2909
   132
    if (!rect || !X1 || !Y1 || !X2 || !Y2) {
slouken@2909
   133
        SDL_FALSE;
slouken@2909
   134
    }
slouken@2909
   135
slouken@2909
   136
    x1 = *X1;
slouken@2909
   137
    y1 = *Y1;
slouken@2909
   138
    x2 = *X2;
slouken@2909
   139
    y2 = *Y2;
slouken@2909
   140
    rectx1 = rect->x;
slouken@2909
   141
    recty1 = rect->y;
slouken@2909
   142
    rectx2 = rect->x + rect->w - 1;
slouken@2909
   143
    recty2 = rect->y + rect->h - 1;
slouken@2909
   144
slouken@2909
   145
    /* Check to see if entire line is inside rect */
slouken@2909
   146
    if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
slouken@2909
   147
        y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
slouken@2909
   148
        return SDL_TRUE;
slouken@2909
   149
    }
slouken@2909
   150
slouken@2994
   151
    /* Check to see if entire line is to one side of rect */
slouken@2909
   152
    if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
slouken@2909
   153
        (y1 < recty1 && y2 < recty2) || (y1 > recty2 && y2 > recty2)) {
slouken@2909
   154
        return SDL_FALSE;
slouken@2909
   155
    }
slouken@2909
   156
slouken@2994
   157
    if (y1 == y2) {
slouken@2909
   158
        /* Horizontal line, easy to clip */
slouken@2909
   159
        if (x1 < rectx1) {
slouken@2909
   160
            *X1 = rectx1;
slouken@2909
   161
        } else if (x1 > rectx2) {
slouken@2909
   162
            *X1 = rectx2;
slouken@2909
   163
        }
slouken@2909
   164
        if (x2 < rectx1) {
slouken@2909
   165
            *X2 = rectx1;
slouken@2909
   166
        } else if (x2 > rectx2) {
slouken@2909
   167
            *X2 = rectx2;
slouken@2909
   168
        }
slouken@2909
   169
        return SDL_TRUE;
slouken@2909
   170
    }
slouken@2909
   171
slouken@2909
   172
    if (x1 == x2) {
slouken@2909
   173
        /* Vertical line, easy to clip */
slouken@2909
   174
        if (y1 < recty1) {
slouken@2909
   175
            *Y1 = recty1;
slouken@2909
   176
        } else if (y1 > recty2) {
slouken@2909
   177
            *Y1 = recty2;
slouken@2909
   178
        }
slouken@2909
   179
        if (y2 < recty1) {
slouken@2909
   180
            *Y2 = recty1;
slouken@2909
   181
        } else if (y2 > recty2) {
slouken@2909
   182
            *Y2 = recty2;
slouken@2909
   183
        }
slouken@2909
   184
        return SDL_TRUE;
slouken@2909
   185
    }
slouken@2909
   186
slouken@2994
   187
    else
slouken@2994
   188
    {
slouken@2994
   189
    /* The task of clipping a line with finite slope ratios in a fixed-
slouken@2994
   190
     * precision coordinate space is not as immediately simple as it is
slouken@2994
   191
     * with coordinates of arbitrary precision. If the ratio of slopes
slouken@2994
   192
     * between the input line segment and the result line segment is not
slouken@2994
   193
     * a whole number, you have in fact *moved* the line segment a bit,
slouken@2994
   194
     * and there can be no avoiding it without more precision
slouken@2994
   195
     */
slouken@2994
   196
        int *x_result_[] = {X1, X2, NULL}, **x_result = x_result_;
slouken@2994
   197
        int *y_result_[] = {Y1, Y2, NULL}, **y_result = y_result_;
slouken@2994
   198
        SDL_bool intersection = SDL_FALSE;
slouken@2994
   199
        double b, m, left, right, bottom, top;
slouken@2994
   200
        int xl, xh, yl, yh;
slouken@2994
   201
slouken@2994
   202
        /* solve mx+b line formula */
slouken@2994
   203
        m = (double)(y1-y2) / (double)(x1-x2);
slouken@2994
   204
        b = y2 - m * (double) x2;
slouken@2994
   205
slouken@2994
   206
        /* find some linear intersections */
slouken@2994
   207
        left = (m * (double) rectx1) + b;
slouken@2994
   208
        right = (m * (double) rectx2) + b;
slouken@2994
   209
        top = (recty1 - b) / m;
slouken@2994
   210
        bottom = (recty2 - b) / m;
slouken@2994
   211
slouken@2994
   212
        /* sort end-points' x and y components individually */
slouken@2994
   213
        if (x1 < x2) {
slouken@2994
   214
            xl = x1;
slouken@2994
   215
            xh = x2;
slouken@2994
   216
        } else {
slouken@2994
   217
            xl = x2;
slouken@2994
   218
            xh = x1;
slouken@2994
   219
        }
slouken@2994
   220
        if (y1 < y2) {
slouken@2994
   221
            yl = y1;
slouken@2994
   222
            yh = y2;
slouken@2994
   223
        } else {
slouken@2994
   224
            yl = y2;
slouken@2994
   225
            yh = y1;
slouken@2994
   226
        }
slouken@2994
   227
slouken@2994
   228
#define RISING(a, b, c) (((a)<=(b))&&((b)<=(c)))
slouken@2994
   229
slouken@2994
   230
        /* check for a point that's entirely inside the rect */
slouken@2994
   231
        if (RISING(rectx1, x1, rectx2) && RISING(recty1, y1, recty2)) {
slouken@2994
   232
            x_result++;
slouken@2994
   233
            y_result++;
slouken@2994
   234
            intersection = SDL_TRUE;
slouken@2994
   235
        } else /* it was determined earlier that *both* end-points are not contained */
slouken@2994
   236
slouken@2994
   237
        if (RISING(rectx1, x2, rectx2) && RISING(recty1, y2, recty2)) {
slouken@2994
   238
            **(x_result++) = x2;
slouken@2994
   239
            **(y_result++) = y2;
slouken@2994
   240
            intersection = SDL_TRUE;
slouken@2994
   241
        }
slouken@2994
   242
slouken@2994
   243
        if (RISING(recty1, left, recty2) && RISING(xl, rectx1, xh)) {
slouken@2994
   244
            **(x_result++) = rectx1;
slouken@2994
   245
            **(y_result++) = (int) left;
slouken@2994
   246
            intersection = SDL_TRUE;
slouken@2994
   247
        }
slouken@2994
   248
slouken@2994
   249
        if (*x_result == NULL) return intersection;
slouken@2994
   250
        if (RISING(recty1, right, recty2) && RISING(xl, rectx2, xh)) {
slouken@2994
   251
            **(x_result++) = rectx2;
slouken@2994
   252
            **(y_result++) = (int) right;
slouken@2994
   253
            intersection = SDL_TRUE;
slouken@2994
   254
        }
slouken@2994
   255
slouken@2994
   256
        if (*x_result == NULL) return intersection;
slouken@2994
   257
        if (RISING(rectx1, top, rectx2) && RISING(yl, recty1, yh)) {
slouken@2994
   258
            **(x_result++) = (int) top;
slouken@2994
   259
            **(y_result++) = recty1;
slouken@2994
   260
            intersection = SDL_TRUE;
slouken@2994
   261
        }
slouken@2994
   262
slouken@2994
   263
        if (*x_result == NULL) return intersection;
slouken@2994
   264
        if (RISING(rectx1, bottom, rectx2) && RISING(yl, recty2, yh)) {
slouken@2994
   265
            **(x_result++) = (int) bottom;
slouken@2994
   266
            **(y_result++) = recty2;
slouken@2994
   267
            intersection = SDL_TRUE;
slouken@2994
   268
        }
slouken@2994
   269
slouken@2994
   270
        return intersection;
slouken@2994
   271
    }
slouken@2994
   272
slouken@2909
   273
    return SDL_FALSE;
slouken@2909
   274
}
slouken@2909
   275
slouken@1895
   276
void
slouken@1895
   277
SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
slouken@1895
   278
{
slouken@1895
   279
    SDL_DirtyRect *dirty;
slouken@2223
   280
slouken@2223
   281
    /* FIXME: At what point is this optimization too expensive? */
slouken@2223
   282
    for (dirty = list->list; dirty; dirty = dirty->next) {
slouken@2223
   283
        if (SDL_HasIntersection(&dirty->rect, rect)) {
slouken@2223
   284
            SDL_UnionRect(&dirty->rect, rect, &dirty->rect);
slouken@2223
   285
            return;
slouken@2223
   286
        }
slouken@2223
   287
    }
slouken@1895
   288
slouken@1895
   289
    if (list->free) {
slouken@1895
   290
        dirty = list->free;
slouken@1895
   291
        list->free = dirty->next;
slouken@1895
   292
    } else {
slouken@1895
   293
        dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
slouken@1895
   294
        if (!dirty) {
slouken@1895
   295
            return;
slouken@1895
   296
        }
slouken@1895
   297
    }
slouken@1895
   298
    dirty->rect = *rect;
slouken@1895
   299
    dirty->next = list->list;
slouken@1895
   300
    list->list = dirty;
slouken@1895
   301
}
slouken@1895
   302
slouken@1895
   303
void
slouken@1895
   304
SDL_ClearDirtyRects(SDL_DirtyRectList * list)
slouken@1895
   305
{
slouken@2224
   306
    SDL_DirtyRect *prev, *curr;
slouken@2224
   307
slouken@2224
   308
    /* Skip to the end of the free list */
slouken@2224
   309
    prev = NULL;
slouken@2224
   310
    for (curr = list->free; curr; curr = curr->next) {
slouken@2224
   311
        prev = curr;
slouken@2224
   312
    }
slouken@2224
   313
slouken@2224
   314
    /* Add the list entries to the end */
slouken@2224
   315
    if (prev) {
slouken@2224
   316
        prev->next = list->list;
slouken@2224
   317
    } else {
slouken@2224
   318
        list->free = list->list;
slouken@2224
   319
    }
slouken@2223
   320
    list->list = NULL;
slouken@1895
   321
}
slouken@1895
   322
slouken@1895
   323
void
slouken@1895
   324
SDL_FreeDirtyRects(SDL_DirtyRectList * list)
slouken@1895
   325
{
slouken@1895
   326
    while (list->list) {
slouken@1895
   327
        SDL_DirtyRect *elem = list->list;
slouken@1895
   328
        list->list = elem->next;
slouken@1895
   329
        SDL_free(elem);
slouken@1895
   330
    }
slouken@1895
   331
    while (list->free) {
slouken@1895
   332
        SDL_DirtyRect *elem = list->free;
slouken@1895
   333
        list->free = elem->next;
slouken@1895
   334
        SDL_free(elem);
slouken@1895
   335
    }
slouken@1895
   336
}
slouken@1895
   337
slouken@1895
   338
/* vi: set ts=4 sw=4 expandtab: */