-
Notifications
You must be signed in to change notification settings - Fork 2
/
micro_paint.c
137 lines (120 loc) · 2.25 KB
/
micro_paint.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
137
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
typedef struct s_zone
{
int w, h;
char *zone;
} t_zone;
typedef struct s_shape
{
char type;
float x, y, w, h;
char c;
} t_shape;
size_t ft_strlen(char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
int ft_put_error(char *s)
{
write(1, s, ft_strlen(s));
return (1);
}
int ft_quit(FILE *fp, t_zone *z, char *s)
{
if (fp)
fclose(fp);
if (z->zone)
free(z->zone);
if (s)
ft_put_error(s);
return (1);
}
int set_zone(FILE *fp, t_zone *z)
{
char c;
int i;
if (fscanf(fp, "%d %d %c\n", &z->w, &z->h, &c) != 3)
return (1);
if (!(z->w > 0 && z->w <= 300 && z->h > 0 && z->h <= 300))
return (1);
if (!(z->zone = calloc(z->w * z->h + 1, sizeof(char))))
return (1);
i = 0;
while (i < z->w * z->h)
z->zone[i++] = c;
return (0);
}
void put_zone(t_zone z)
{
int y;
y = 0;
while (y < z.h)
{
write(1, &z.zone[y * z.w], z.w);
write(1, "\n", 1);
y++;
}
}
int is_in_rectangle(float x, float y, t_shape s)
{
if (x - s.x < 0.0 || s.x - x + s.w < 0.0 || y - s.y < 0.0 || s.y - y + s.h < 0.0)
return (0);
if (x - s.x < 1.0 || s.x - x + s.w < 1.0 || y - s.y < 1.0 || s.y - y + s.h < 1.0)
return (1);
return (2);
}
void draw_shape(t_zone *z, t_shape s)
{
int x, y, ret;
y = 0;
while (y < z->h)
{
x = 0;
while (x < z->w)
{
ret = is_in_rectangle(x, y, s);
if (ret == 1 || (s.type == 'R' && ret == 2))
z->zone[y * z->w + x] = s.c;
x++;
}
y++;
}
}
int draw_shapes(FILE *fp, t_zone *z)
{
t_shape s;
int ret;
while ((ret = fscanf(fp, "%c %f %f %f %f %c\n", &s.type, &s.x, &s.y, &s.w, &s.h, &s.c)) == 6)
{
if (!((s.type == 'r' || s.type == 'R') && s.w > 0.0 && s.h > 0.0))
return (1);
draw_shape(z, s);
}
if (ret != -1)
return (1);
return (0);
}
int main(int argc, char *argv[])
{
FILE *fp;
t_zone z;
if (argc != 2)
return (ft_put_error("Error: argument\n"));
if (!(fp = fopen(argv[1], "r")))
return (ft_put_error("Error: Operation file corrupted\n"));
memset(&z, 0, sizeof(t_zone));
if (set_zone(fp, &z))
return (ft_quit(fp, &z, "Error: Operation file corrupted\n"));
if (draw_shapes(fp, &z))
return (ft_quit(fp, &z, "Error: Operation file corrupted\n"));
put_zone(z);
ft_quit(fp, &z, NULL);
return (0);
}