Commit 18b9f5b2 authored by Sam Lantinga's avatar Sam Lantinga

Working Bresenham line drawing algorithm. We can optimize later, if needed.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403365
parent 5a7c477e
...@@ -283,49 +283,58 @@ do { \ ...@@ -283,49 +283,58 @@ do { \
#define ABS(_x) ((_x) < 0 ? -(_x) : (_x)) #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
#define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0) #define BRESENHAM(x1, y1, x2, y2, op) \
#define BRESENHAM(x0, y0, x1, y1, op) \
{ \ { \
int deltax, deltay, steep, error, xstep, ystep, x, y; \ int i, deltax, deltay, numpixels; \
int d, dinc1, dinc2; \
int x, xinc1, xinc2; \
int y, yinc1, yinc2; \
\ \
deltax = ABS(x1 - x0); \ deltax = ABS(x2 - x1); \
deltay = ABS(y1 - y0); \ deltay = ABS(y2 - y1); \
steep = (deltay > deltax); \ \
if (steep) { \ if (deltax >= deltay) { \
SWAP(x0, y0); \ numpixels = deltax + 1; \
SWAP(x1, y1); \ d = (2 * deltay) - deltax; \
SWAP(deltax, deltay); \ dinc1 = deltay * 2; \
} \ dinc2 = (deltay - deltax) * 2; \
error = (x1 - x0) / 2; \ xinc1 = 1; \
y = y0; \ xinc2 = 1; \
if (x0 > x1) { \ yinc1 = 0; \
xstep = -1; \ yinc2 = 1; \
} else { \ } else { \
xstep = 1; \ numpixels = deltay + 1; \
d = (2 * deltax) - deltay; \
dinc1 = deltax * 2; \
dinc2 = (deltax - deltay) * 2; \
xinc1 = 0; \
xinc2 = 1; \
yinc1 = 1; \
yinc2 = 1; \
} \ } \
if (y0 < y1) { \ \
ystep = 1; \ if (x1 > x2) { \
} else { \ xinc1 = -xinc1; \
ystep = -1; \ xinc2 = -xinc2; \
} \ } \
if (!steep) { \ if (y1 > y2) { \
for (x = x0; x != x1; x += xstep) { \ yinc1 = -yinc1; \
op(x, y); \ yinc2 = -yinc2; \
error -= deltay; \ } \
if (error < 0) { \ \
y += ystep; \ x = x1; \
error += deltax; \ y = y1; \
} \ \
} \ for (i = 1; i < numpixels; ++i) { \
} else { \ op(x, y); \
for (x = x0; x != x1; x += xstep) { \ if (d < 0) { \
op(y, x); \ d += dinc1; \
error -= deltay; \ x += xinc1; \
if (error < 0) { \ y += yinc1; \
y += ystep; \ } else { \
error += deltax; \ d += dinc2; \
} \ x += xinc2; \
y += yinc2; \
} \ } \
} \ } \
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment