This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_line.c
73 lines (67 loc) · 1.73 KB
/
draw_line.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: magrab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/18 20:30:03 by magrab #+# #+# */
/* Updated: 2019/02/13 19:52:33 by magrab ### ########.fr */
/* */
/* ************************************************************************** */
#include <mlx.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
void *g_mlx(int setup);
void draw_line(void *win, int x0, int y0, int x1, int y1)
{
int dx;
int dy;
int err;
int e2;
dx = abs(x1 - x0);
dy = abs(y1 - y0);
err = (dx>dy ? dx : -dy);
while (x0 != x1 || y0 != y1)
{
mlx_pixel_put(g_mlx(0), win, x0, y0, 0xFFFFFF);
e2 = err;
if (e2 > -dx)
{
err -= dy;
x0 += (x0 < x1 ? 1 : -1);
}
if (e2 < dy)
{
err += dx;
y0 += (y0 < y1 ? 1 : -1);
}
}
}
void test_draw(void *win, int x, int y)
{
static int x0;
static int y0;
static int x1;
static int y1;
if (x0 == 0)
{
x0 = x;
y0 = y;
}
else if (x1 == 0)
{
x1 = x;
y1 = y;
}
if (x0 != 0 && x1 != 0)
{
printf("drawing :\nx0 = %d\ty0 = %d\nx1 = %d\ty1 = %d\n", x0, y0, x1, y1);
draw_line(win, x0, y0, x1, y1);
x0 = 0;
x1 = 0;
y0 = 0;
y1 = 0;
}
}