From c3c0481efb4d98df0a76cf57df65f8f165a4376d Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Tue, 12 Aug 2008 04:01:18 -0300 Subject: [PATCH] Use Bresenham to draw the line between faces This solves the problem of the slope and direction when drawing lines. --- movie.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/movie.c b/movie.c index 42310e6..4184b82 100644 --- a/movie.c +++ b/movie.c @@ -22,6 +22,12 @@ #include #include +#define SWAP(x, y) do { \ + x ^= y; y ^= x; x ^= y; \ + } while (0) + +#define ABS(x) ((x) < 0 ? -(x) : (x)) + #define IS_CENTER(cx, cy, x, y) \ ((x + WIDTH/2 == cx) && (y + HEIGHT/2 == cy)) @@ -34,19 +40,43 @@ SDL_Rect * GetNextPoint (void) { static SDL_Rect rect = {0, 0, WIDTH, HEIGHT}; - static int cur = 0; - int dx, dy; + static int cur = -1; + static int inc, err, thre, swap; + static int x1, y1, x2, y2; + static int x, y; int next; next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect)); - if (IS_CENTER (points[next].x, points[next].y, rect.x, rect.y)) + if (IS_CENTER (points[next].x, points[next].y, rect.x, rect.y) || cur == -1) { cur = next; next = (cur + 1) % (sizeof (points) / sizeof (SDL_Rect)); + err = 0; + swap = 0; + x1 = points[cur].x; + y1 = points[cur].y; + x2 = points[next].x; + y2 = points[next].y; + inc = y2 - y1; + thre = x2 - x1; + if (ABS (inc) > ABS (thre)) + { + SWAP (inc, thre); + SWAP (x1, y1); + SWAP (x2, y2); + swap = 1; + } + x = x1; + y = y1; + } + rect.x = (swap ? y : x) - WIDTH/2; + rect.y = (swap ? x : y) - HEIGHT/2; + (x2 < x1) ? x-- : x++; + err += ABS (inc); + if (err >= ABS (thre)) + { + err -= ABS (thre); + y += (inc < 0) ? -1 : 1; } - dx = (points[next].x > points[cur].x) ? 1 : -1; - dy = (points[next].y > points[cur].y) ? 1 : -1; - rect.x = (rect.x + dx) % WIDTH; - rect.y = (rect.y + dy) % HEIGHT; return ▭ } -- 2.20.1