summaryrefslogtreecommitdiff
path: root/src/line.c
blob: 6f0f7490830d28cfe006809dfc10202ed7a47554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
  }
}