summaryrefslogtreecommitdiff
path: root/src/line.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/line.c')
-rw-r--r--src/line.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/line.c b/src/line.c
new file mode 100644
index 0000000..6f0f749
--- /dev/null
+++ b/src/line.c
@@ -0,0 +1,56 @@
+/*
+** line.c for in /home/gayot_o/prog/lib/sdl-digit
+**
+** Made by olivier gayot
+** Login <gayot_o@epitech.net>
+**
+** 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;
+ }
+}