-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.c
136 lines (125 loc) · 3.75 KB
/
segment.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* segment.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: imane <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/17 21:14:27 by irhesri #+# #+# */
/* Updated: 2023/02/13 23:52:11 by imane ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static void draw_segment_up(t_segment *seg, t_image *img, int dx, int dy)
{
int delta;
int color;
char *dst;
color = seg->color;
delta = 2 * dy - dx;
while ((!seg->b && seg->xi <= seg->xf) || (seg->b && seg->yi <= seg->yf))
{
dst = img->address + (seg->yi * img->size + seg->xi * img->bit / 8);
*(unsigned int *) dst = color;
if (delta < 0)
delta += 2 * dy;
else
{
delta += 2 * (dy - dx);
!seg->b && seg->yi++;
seg->b && seg->xi++;
}
!seg->b && seg->xi++;
seg->b && seg->yi++;
}
}
static void draw_segment_down(t_segment *seg, t_image *img, int dx, int dy)
{
int delta;
int color;
char *dst;
color = seg->color;
delta = 2 * dy - dx;
while ((!seg->b && seg->xi >= seg->xf) || (seg->b && seg->yi <= seg->yf))
{
dst = img->address + (seg->yi * img->size + seg->xi * img->bit / 8);
*(unsigned int *) dst = color;
if (delta < 0)
delta += 2 * dy;
else
{
delta += 2 * (dy - dx);
!seg->b && seg->yi++;
seg->b && seg->xi--;
}
!seg->b && seg->xi--;
seg->b && seg->yi++;
}
}
static void seg_init(t_segment *seg, t_window *win, t_point *p0, t_point *p1)
{
int dx;
int dy;
t_point *p;
if (p1->y < p0->y)
{
p = p0;
p0 = p1;
p1 = p;
}
seg->xi = p0->x + abs(win->n[1]) * ((win->n[1] < 0) - (win->n[1] > 0)) + 1;
seg->yi = p0->y + abs(win->n[0]) * ((win->n[0] < 0) - (win->n[0] > 0)) + 1;
seg->xf = p1->x + abs(win->n[1]) * ((win->n[1] < 0) - (win->n[1] > 0)) + 1;
seg->yf = p1->y + abs(win->n[0]) * ((win->n[0] < 0) - (win->n[0] > 0)) + 1;
dx = abs(seg->xf - seg->xi);
dy = abs(seg->yf - seg->yi);
seg->b = (dx < dy);
if ((seg->xf >= seg->xi) && (dx >= dy))
draw_segment_up(seg, win->image, dx, dy);
else if (seg->xf >= seg->xi)
draw_segment_up(seg, win->image, dy, dx);
else if (dx >= dy)
draw_segment_down(seg, win->image, dx, dy);
else
draw_segment_down(seg, win->image, dy, dx);
}
static void draw_map(t_point **map, t_window *win)
{
int i;
int j;
t_segment *seg;
i = -1;
seg = malloc(sizeof(t_segment));
!(seg) && error_case(0);
while (++i < win->map_size[0])
{
j = -1;
while (++j < win->map_size[1] - 1)
{
seg->color = (&map[i][j])->color;
if (seg->color == -1)
seg->color = 0xffffff;
seg_init(seg, win, &map[i][j], &map[i][j + 1]);
if (map[i + 1])
seg_init(seg, win, &map[i][j], &map[i + 1][j]);
}
if (map[i + 1])
seg_init(seg, win, &map[i][j], &map[i + 1][j]);
}
free (seg);
}
void new_image(t_window *win, int width, int length)
{
char *a;
t_image *img;
img = (t_image *) malloc(sizeof(t_image));
!(img) && error_case(0);
img->img = mlx_new_image(win->mlx, width - 10, length - 10);
!(img->img) && error_case(0);
a = mlx_get_data_addr(img->img, &(img->bit), &(img->size), &(img->endian));
img->address = a;
win->image = img;
draw_map(win->map, win);
mlx_clear_window(win->mlx, win->win);
mlx_put_image_to_window (win->mlx, win->win, img->img, 10, 10);
}