From 4d5e96d61bc68435c9c2eafe8a231505514d62f4 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 9 Nov 2003 19:46:14 +0000 Subject: [PATCH] * libee/line.c: + ee_draw_thin_line(). * test/demo.c: + Added a thin lines demo. --- libee/ee.h | 1 + libee/line.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++--- test/demo.c | 75 +++++++++++++++++++++----------- 3 files changed, 165 insertions(+), 31 deletions(-) diff --git a/libee/ee.h b/libee/ee.h index b573d21..75f084d 100644 --- a/libee/ee.h +++ b/libee/ee.h @@ -75,6 +75,7 @@ char ee_get_key(void); void ee_draw_circle(int, int, int, char); void ee_draw_line(int, int, int, int, char); +void ee_draw_thin_line(int, int, int, int); int ee_rand(int, int); int ee_sqrt(int); diff --git a/libee/line.c b/libee/line.c index ef7e8b2..0468a57 100644 --- a/libee/line.c +++ b/libee/line.c @@ -38,6 +38,7 @@ struct line static void clip_line(struct line*); static uint8_t clip_bits(int, int); static void draw_solid_line(struct line*); +static void draw_thin_line(struct line*); void ee_draw_line(int x1, int y1, int x2, int y2, char c) { @@ -51,6 +52,17 @@ void ee_draw_line(int x1, int y1, int x2, int y2, char c) clip_line(&s); } +void ee_draw_thin_line(int x1, int y1, int x2, int y2) +{ + struct line s; + s.x1 = x1; + s.y1 = y1; + s.x2 = x2; + s.y2 = y2; + s.draw = draw_thin_line; + clip_line(&s); +} + static void clip_line(struct line* s) { uint8_t bits1, bits2; @@ -121,15 +133,14 @@ static uint8_t clip_bits(int x, int y) static void draw_solid_line(struct line* s) { - int x1 = s->x1; - int y1 = s->y1; - int x2 = s->x2; - int y2 = s->y2; + int x1, y1, x2, y2; + int dx, dy; + int xinc, yinc; - int dx = abs(x2-x1); - int dy = abs(y2-y1); + x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2; - int xinc, yinc; + dx = abs(x2 - x1); + dy = abs(y2 - y1); xinc = (x1 > x2) ? -1 : 1; yinc = (y1 > y2) ? -1 : 1; @@ -182,3 +193,98 @@ static void draw_solid_line(struct line* s) } } +static void draw_thin_line(struct line* s) +{ + char *charmapx, *charmapy; + int x1, y1, x2, y2; + int dx, dy; + int yinc; + + if(s->x2 >= s->x1) + { + if(s->y1 > s->y2) + charmapx = ",'"; + else + charmapx = "`."; + x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2; + } + else + { + if(s->y1 > s->y2) + charmapx = "`."; + else + charmapx = ",'"; + x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2; + } + + dx = abs(x2 - x1); + dy = abs(y2 - y1); + + if(y1 > y2) + { + charmapy = ",'"; + yinc = -1; + } + else + { + yinc = 1; + charmapy = "`."; + } + + if(dx >= dy) + { + int dpr = dy << 1; + int dpru = dpr - (dx << 1); + int delta = dpr - dx; + int prev = 0; + + for(; dx>=0; dx--) + { + ee_goto(x1, y1); + if(delta > 0) + { + ee_putchar(charmapy[1]); + x1++; + y1 += yinc; + delta += dpru; + prev = 1; + } + else + { + if(prev) + ee_putchar(charmapy[0]); + else + ee_putchar('-'); + x1++; + delta += dpr; + prev = 0; + } + } + } + else + { + int dpr = dx << 1; + int dpru = dpr - (dy << 1); + int delta = dpr - dy; + + for(; dy >= 0; dy--) + { + ee_goto(x1, y1); + if(delta > 0) + { + ee_putchar(charmapx[0]); + ee_putchar(charmapx[1]); + x1++; + y1 += yinc; + delta += dpru; + } + else + { + ee_putchar('|'); + y1 += yinc; + delta += dpr; + } + } + } +} + diff --git a/test/demo.c b/test/demo.c index c8d41aa..f3cc5bd 100644 --- a/test/demo.c +++ b/test/demo.c @@ -29,6 +29,7 @@ static void display_menu(void); static void demo_dots(void); static void demo_lines(void); +static void demo_thin_lines(void); static void demo_circles(void); static void demo_radar(void); @@ -73,9 +74,13 @@ int main(int argc, char **argv) break; case '3': ee_clear(); - demo = demo_circles; + demo = demo_thin_lines; break; case '4': + ee_clear(); + demo = demo_circles; + break; + case '5': ee_clear(); demo = demo_radar; break; @@ -114,9 +119,11 @@ static void display_menu(void) ee_goto(4, 7); ee_putstr("2: lines demo"); ee_goto(4, 8); - ee_putstr("3: circles demo"); + ee_putstr("3: thin lines demo"); ee_goto(4, 9); - ee_putstr("4: radar demo"); + ee_putstr("4: circles demo"); + ee_goto(4, 10); + ee_putstr("5: radar demo"); ee_goto(4, yo - 2); ee_putstr("q: quit"); @@ -126,14 +133,15 @@ static void display_menu(void) static void demo_dots(void) { + int xmax = ee_get_width() - 1; + int ymax = ee_get_height() - 1; int i; - for(i=1000; i--;) + for(i = 1000; i--;) { /* Putpixel */ ee_color(ee_rand(1, 10)); - ee_goto(ee_rand(0, ee_get_width() - 1), - ee_rand(0, ee_get_height() - 1)); + ee_goto(ee_rand(0, xmax), ee_rand(0, ymax)); ee_putchar('#'); } ee_refresh(); @@ -141,46 +149,65 @@ static void demo_dots(void) static void demo_lines(void) { + int w = ee_get_width(); + int h = ee_get_height(); + /* Draw lines */ ee_color(ee_rand(1, 10)); if(clipping) { - ee_draw_line(ee_rand(- ee_get_width(), 2 * ee_get_width()), - ee_rand(- ee_get_height(), 2 * ee_get_height()), - ee_rand(- ee_get_width(), 2 * ee_get_width()), - ee_rand(- ee_get_height(), 2 * ee_get_height()), - '#'); + ee_draw_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), + ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), '#'); } else { - ee_draw_line(ee_rand(0, ee_get_width()), - ee_rand(0, ee_get_height()), - ee_rand(0, ee_get_width()), - ee_rand(0, ee_get_height()), - '*'); + ee_draw_line(ee_rand(0, w - 1), ee_rand(0, h - 1), + ee_rand(0, w - 1), ee_rand(0, h - 1), '*'); + } + ee_refresh(); +} + +static void demo_thin_lines(void) +{ + int w = ee_get_width(); + int h = ee_get_height(); + + /* Draw lines */ + ee_color(ee_rand(1, 10)); + if(clipping) + { + ee_draw_thin_line(ee_rand(- w, 2 * w), ee_rand(- h, 2 * h), + ee_rand(- w, 2 * w), ee_rand(- h, 2 * h)); + } + else + { + ee_draw_thin_line(ee_rand(0, w), ee_rand(0, h), + ee_rand(0, w), ee_rand(0, h)); } ee_refresh(); } static void demo_circles(void) { + int w = ee_get_width(); + int h = ee_get_height(); + /* Draw circles */ if(clipping) { ee_color(ee_rand(1, 10)); - ee_draw_circle(ee_rand(- ee_get_width(), 2 * ee_get_width()), - ee_rand(- ee_get_height(), 2 * ee_get_height()), - ee_rand(0, ee_get_width() + ee_get_height()) / 2, + ee_draw_circle(ee_rand(- w, 2 * w), + ee_rand(- h, 2 * h), + ee_rand(0, (w + h) / 2), '#'); } else { - int x = ee_rand(0, ee_get_width()); - int y = ee_rand(0, ee_get_height()); - int r = ee_rand(0, (ee_get_width() + ee_get_height()) / 2); + int x = ee_rand(0, w); + int y = ee_rand(0, h); + int r = ee_rand(0, (w + h) / 2); - if(x - r < 0 || x + r >= ee_get_width() || - y - r < 0 || y + r >= ee_get_height()) + if(x - r < 0 || x + r >= w || y - r < 0 || y + r >= h) { demo_circles(); return;