Working Bresenham line drawing algorithm. We can optimize later, if needed.
authorSam Lantinga <slouken@libsdl.org>
Sun, 21 Dec 2008 20:16:21 +0000
changeset 2903e426c4fc9cf7
parent 2902 83c3a4b0e421
child 2904 fa81cc1ef3d0
Working Bresenham line drawing algorithm. We can optimize later, if needed.
src/video/SDL_draw.h
     1.1 --- a/src/video/SDL_draw.h	Sun Dec 21 17:55:02 2008 +0000
     1.2 +++ b/src/video/SDL_draw.h	Sun Dec 21 20:16:21 2008 +0000
     1.3 @@ -283,49 +283,58 @@
     1.4  
     1.5  #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
     1.6  
     1.7 -#define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0)
     1.8 -
     1.9 -#define BRESENHAM(x0, y0, x1, y1, op) \
    1.10 +#define BRESENHAM(x1, y1, x2, y2, op) \
    1.11  { \
    1.12 -    int deltax, deltay, steep, error, xstep, ystep, x, y; \
    1.13 +    int i, deltax, deltay, numpixels; \
    1.14 +    int d, dinc1, dinc2; \
    1.15 +    int x, xinc1, xinc2; \
    1.16 +    int y, yinc1, yinc2; \
    1.17   \
    1.18 -    deltax = ABS(x1 - x0); \
    1.19 -    deltay = ABS(y1 - y0); \
    1.20 -    steep = (deltay > deltax); \
    1.21 -    if (steep) { \
    1.22 -        SWAP(x0, y0); \
    1.23 -        SWAP(x1, y1); \
    1.24 -        SWAP(deltax, deltay); \
    1.25 +    deltax = ABS(x2 - x1); \
    1.26 +    deltay = ABS(y2 - y1); \
    1.27 + \
    1.28 +    if (deltax >= deltay) { \
    1.29 +        numpixels = deltax + 1; \
    1.30 +        d = (2 * deltay) - deltax; \
    1.31 +        dinc1 = deltay * 2; \
    1.32 +        dinc2 = (deltay - deltax) * 2; \
    1.33 +        xinc1 = 1; \
    1.34 +        xinc2 = 1; \
    1.35 +        yinc1 = 0; \
    1.36 +        yinc2 = 1; \
    1.37 +    } else { \
    1.38 +        numpixels = deltay + 1; \
    1.39 +        d = (2 * deltax) - deltay; \
    1.40 +        dinc1 = deltax * 2; \
    1.41 +        dinc2 = (deltax - deltay) * 2; \
    1.42 +        xinc1 = 0; \
    1.43 +        xinc2 = 1; \
    1.44 +        yinc1 = 1; \
    1.45 +        yinc2 = 1; \
    1.46      } \
    1.47 -    error = (x1 - x0) / 2; \
    1.48 -    y = y0; \
    1.49 -    if (x0 > x1) { \
    1.50 -        xstep = -1; \
    1.51 -    } else { \
    1.52 -        xstep = 1; \
    1.53 + \
    1.54 +    if (x1 > x2) { \
    1.55 +        xinc1 = -xinc1; \
    1.56 +        xinc2 = -xinc2; \
    1.57      } \
    1.58 -    if (y0 < y1) { \
    1.59 -        ystep = 1; \
    1.60 -    } else { \
    1.61 -        ystep = -1; \
    1.62 +    if (y1 > y2) { \
    1.63 +        yinc1 = -yinc1; \
    1.64 +        yinc2 = -yinc2; \
    1.65      } \
    1.66 -    if (!steep) { \
    1.67 -        for (x = x0; x != x1; x += xstep) { \
    1.68 -            op(x, y); \
    1.69 -            error -= deltay; \
    1.70 -            if (error < 0) { \
    1.71 -                y += ystep; \
    1.72 -                error += deltax; \
    1.73 -            } \
    1.74 -        } \
    1.75 -    } else { \
    1.76 -        for (x = x0; x != x1; x += xstep) { \
    1.77 -            op(y, x); \
    1.78 -            error -= deltay; \
    1.79 -            if (error < 0) { \
    1.80 -                y += ystep; \
    1.81 -                error += deltax; \
    1.82 -            } \
    1.83 + \
    1.84 +    x = x1; \
    1.85 +    y = y1; \
    1.86 + \
    1.87 +    for (i = 1; i < numpixels; ++i) { \
    1.88 +        op(x, y); \
    1.89 +        if (d < 0) { \
    1.90 +            d += dinc1; \
    1.91 +            x += xinc1; \
    1.92 +            y += yinc1; \
    1.93 +        } else { \
    1.94 +            d += dinc2; \
    1.95 +            x += xinc2; \
    1.96 +            y += yinc2; \
    1.97          } \
    1.98      } \
    1.99  }