forked from AndreIglesias/fdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute.c
More file actions
103 lines (91 loc) · 2.88 KB
/
compute.c
File metadata and controls
103 lines (91 loc) · 2.88 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* compute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ksoto <ksoto@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/11 20:40:11 by ciglesia #+# #+# */
/* Updated: 2021/10/14 11:44:29 by ksoto ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
/*
** max_calculator: calculate the max number between 2
*/
int max_calculator(int a, int b)
{
if (a > b)
return (a);
return (b);
}
/*
** module: apply module to a number
*/
int module(int a)
{
if (a < 0)
a = a * -1;
return (a);
}
/*
** convert_isometric: reasign the values of x y to a isometric
*/
void convert_isometric(t_pixel *px, int z, t_fdf *fdf)
{
if (fdf->view == 3)
{
px->x = (px->x - px->y) * cos(0.7);
px->y = (px->x + px->y) * sin(0.7) - z;
}
}
/*
** set_color: set color number in the structure
*/
void set_color(t_fdf *fdf, int **bmp, int z, int z1)
{
while (((int)(fdf->init->x - fdf->end->x) || \
(int)(fdf->init->y - fdf->end->y)) && \
((int)fdf->init->y > 0) && \
((int)fdf->init->x > 0) && \
((int)fdf->init->y < fdf->res[1]) && \
((int)fdf->init->x < fdf->res[0])
)
{
if (z || z1)
bmp[(int)fdf->init->y][(int)fdf->init->x] = 0xE80C0C;
else
bmp[(int)fdf->init->y][(int)fdf->init->x] = 0xFFFFFF;
if (fdf->color != -1)
bmp[(int)fdf->init->y][(int)fdf->init->x] = fdf->color;
else if (fdf->color1 != -1)
bmp[(int)fdf->init->y][(int)fdf->init->x] = fdf->color1;
fdf->init->x += fdf->diff_x;
fdf->init->y += fdf->diff_y;
}
}
/*
** bresenham_line: bresenham algorithm to plot a line pixel by pixel
** diff_x: distance that x need to advance
** diff_y: distance that y need to advance
*/
void bresenham_line(t_fdf *fdf, int **bmp)
{
int max;
long z;
long z1;
z = fdf->map[(int)fdf->init->y][(int)fdf->init->x].z;
z1 = fdf->map[(int)fdf->end->y][(int)fdf->end->x].z;
fdf->color = fdf->map[(int)fdf->init->y][(int)fdf->init->x].color;
fdf->color1 = fdf->map[(int)fdf->init->y][(int)fdf->init->x].color;
convert_zoom(fdf);
convert_isometric(fdf->init, z, fdf);
convert_isometric(fdf->end, z1, fdf);
convert_shift(fdf);
fdf->diff_x = fdf->end->x - fdf->init->x;
fdf->diff_y = fdf->end->y - fdf->init->y;
max = max_calculator(module(fdf->diff_x), module(fdf->diff_y));
fdf->diff_x /= max;
fdf->diff_y /= max;
set_color(fdf, bmp, z, z1);
}