/* ** line.c for in /home/gayot_o/prog/lib/sdl-digit ** ** Made by olivier gayot ** Login ** ** Started on Mon Apr 23 08:07:45 2012 olivier gayot ** Last update Mon Apr 23 08:07:45 2012 olivier gayot */ #include "sdl_digit.h" static void perform_draw_line(SDL_Surface *, SDL_Rect, SDL_Rect, t_vect, Uint32); void draw_line(SDL_Surface *surf, SDL_Rect r1, SDL_Rect r2, Uint32 color) { t_vect vect; SDL_Rect begin, end; if (ABS(r2.x - r1.x) > ABS(r2.y - r1.y)) { if (r1.x < r2.x) { begin = r1; end = r2; } else { begin = r2; end = r1; } vect.x = 1; vect.y = (float)(end.y - begin.y) / (float)(end.x - begin.x); } else { if (r1.y < r2.y) { begin = r1; end = r2; } else { begin = r2; end = r1; } vect.y = 1; vect.x = (float)(end.x - begin.x) / (float)(end.y - begin.y); } perform_draw_line(surf, begin, end, vect, color); } static void perform_draw_line(SDL_Surface *surf, SDL_Rect begin, SDL_Rect end, t_vect vect, Uint32 color) { float x = (float)begin.x; float y = (float)begin.y; while (x != (float)end.x || y != (float)end.y) { set_pixel_color(surf, (int)x, (int)y, color); x += vect.x; y += vect.y; } }