-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
363 lines (304 loc) · 9.44 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <stdio.h>
#define MAX_HEIGHT 25
#define MAX_WIDTH 25
#define point_is_within_image(x, y) x >= 0 && y >= 0 && x < MAX_HEIGHT && y < MAX_WIDTH
#define red(x, y) x < 0 && y < 0
#define yellow(x, y) x < 0 && y >= 0 && y < MAX_WIDTH
#define green(x, y) x < 0 && y >= MAX_WIDTH
#define magenta(x, y) x >= 0 && x < MAX_HEIGHT && y >= MAX_WIDTH
#define white(x, y) x >= MAX_HEIGHT && y >= MAX_WIDTH
#define blue(x, y) x >= MAX_HEIGHT && y >= 0 && y < MAX_WIDTH
#define black(x, y) x >= MAX_HEIGHT && y < 0
#define cyan(x, y) x >= 0 && x < MAX_HEIGHT && y < 0
void bucket_fill(int x, int y, int seed_color, int target_color);
void print_image();
int color_chosen(int x, int y);
void standardize_coors(int copy_point1[], int copy_point2[]);
int normal_copy_paste(int copy_point1[], int copy_point2[], int paste_point1[], int paste_point2[]);
int cosine(int theta);
int sine(int theta);
void rotate_and_copy(int aux_arr[][12], int image_arr[][12],
int startx, int endx, int starty, int endy);
int paste_rotated(int aux_arr[][12], int startx, int endx, int starty, int endy);
/* declaring image array as a global variable */
int image_arr[MAX_HEIGHT][MAX_WIDTH];
int main(void)
{
char op_code;
int i, j;
/* Take input for image */
for (i = 0; i < MAX_HEIGHT; i++)
{
for (j = 0; j < MAX_WIDTH; j++)
{
scanf("%d", &image_arr[i][j]);
}
}
/* Get user's choice for the operation */
scanf(" %c", &op_code);
if (op_code == 'F')
{
int seed_x, seed_y, color_x, color_y;
scanf("%d %d %d %d", &color_x, &color_y, &seed_x, &seed_y);
bucket_fill(seed_x, seed_y, image_arr[seed_x][seed_y], color_chosen(color_x, color_y));
print_image();
}
else if (op_code == 'P')
{
int copy_point1[2], copy_point2[2], paste_point1[2], paste_point2[2],
num_of_changes = 0;
scanf("%d %d %d %d %d %d %d %d", ©_point1[0], ©_point1[1],
©_point2[0], ©_point2[1], &paste_point1[0], &paste_point1[1],
&paste_point2[0], &paste_point2[1]);
standardize_coors(copy_point1, copy_point2);
standardize_coors(paste_point1, paste_point2);
num_of_changes = normal_copy_paste(copy_point1, copy_point2, paste_point1, paste_point2);
print_image();
printf("%d\n", num_of_changes);
}
else if (op_code == 'R')
{
int aux_arr90[12][12], aux_arr180[12][12], aux_arr270[12][12];
int copy_point1[2], copy_point2[2], paste_point1[2], paste_point2[2],
num_of_changes = 0, degree, size;
char direction;
scanf(" %c %d %d %d %d %d %d %d %d %d", &direction, °ree, ©_point1[0], ©_point1[1],
©_point2[0], ©_point2[1], &paste_point1[0], &paste_point1[1],
&paste_point2[0], &paste_point2[1]);
size = copy_point2[0] - copy_point1[0] + 1;
if (degree == 0)
{
num_of_changes = normal_copy_paste(copy_point1, copy_point2, paste_point1, paste_point2);
print_image();
printf("%d\n", num_of_changes);
}
else
{
int temp_arr[12][12], i, j, l, m;
for (i = copy_point1[0], l = 0; i <= copy_point2[0]; i++, l++)
{
for (j = copy_point1[1], m = 0; j <= copy_point2[1]; j++, m++)
{
temp_arr[l][m] = image_arr[i][j];
}
}
rotate_and_copy(aux_arr90, temp_arr, 0, size - 1, 0, size - 1);
rotate_and_copy(aux_arr180, aux_arr90, 0, size - 1, 0, size - 1);
rotate_and_copy(aux_arr270, aux_arr180, 0, size - 1, 0, size - 1);
if (direction == 'R')
{
if (degree == 90)
{
num_of_changes = paste_rotated(aux_arr90, paste_point1[0], paste_point2[0], paste_point1[1], paste_point2[1]);
}
else if (degree == 180)
{
num_of_changes = paste_rotated(aux_arr180, paste_point1[0], paste_point2[0], paste_point1[1], paste_point2[1]);
}
else if (degree == 270)
{
num_of_changes = paste_rotated(aux_arr270, paste_point1[0], paste_point2[0], paste_point1[1], paste_point2[1]);
}
}
else if (direction == 'L')
{
if (degree == 90)
{
num_of_changes = paste_rotated(aux_arr270, paste_point1[0], paste_point2[0], paste_point1[1], paste_point2[1]);
}
else if (degree == 180)
{
num_of_changes = paste_rotated(aux_arr180, paste_point1[0], paste_point2[0], paste_point1[1], paste_point2[1]);
}
else if (degree == 270)
{
num_of_changes = paste_rotated(aux_arr90, paste_point1[0], paste_point2[0], paste_point1[1], paste_point2[1]);
}
}
print_image();
printf("%d\n", num_of_changes);
}
}
return 0;
}
void bucket_fill(int x, int y, int seed_color, int target_color)
{
if (!(point_is_within_image(x, y)))
{
return;
}
else if (image_arr[x][y] == target_color)
{
return;
}
else if (image_arr[x][y] != seed_color)
{
return;
}
image_arr[x][y] = target_color;
bucket_fill(x - 1, y, seed_color, target_color); /* North */
bucket_fill(x + 1, y, seed_color, target_color); /* South */
bucket_fill(x, y + 1, seed_color, target_color); /* East */
bucket_fill(x, y - 1, seed_color, target_color); /* West */
bucket_fill(x - 1, y + 1, seed_color, target_color); /* North East */
bucket_fill(x - 1, y - 1, seed_color, target_color); /* North West */
bucket_fill(x + 1, y + 1, seed_color, target_color); /* South East */
bucket_fill(x + 1, y - 1, seed_color, target_color); /* South West */
}
void print_image()
{
int i, j;
for (i = 0; i < MAX_HEIGHT; i++)
{
for (j = 0; j < MAX_WIDTH; j++)
{
printf("%d ", image_arr[i][j]);
}
printf("\n");
}
}
int color_chosen(int x, int y)
{
if (point_is_within_image(x, y))
{
return image_arr[x][y];
}
else if (red(x, y))
{
return 0;
}
else if (yellow(x, y))
{
return 1;
}
else if (green(x, y))
{
return 2;
}
else if (magenta(x, y))
{
return 3;
}
else if (white(x, y))
{
return 4;
}
else if (blue(x, y))
{
return 5;
}
else if (black(x, y))
{
return 6;
}
else
{
return 7;
}
}
void standardize_coors(int copy_point1[], int copy_point2[])
{
if (copy_point1[0] >= copy_point2[0] && copy_point2[1] >= copy_point1[1])
{
int temp_x;
temp_x = copy_point1[0];
copy_point1[0] = copy_point2[0];
copy_point2[0] = temp_x;
}
else if (copy_point1[0] >= copy_point2[0] && copy_point1[1] >= copy_point2[1])
{
int temp_x, temp_y;
temp_x = copy_point1[0];
copy_point1[0] = copy_point2[0];
copy_point2[0] = temp_x;
temp_y = copy_point1[1];
copy_point1[1] = copy_point2[1];
copy_point2[1] = temp_y;
}
else if (copy_point1[0] <= copy_point2[0] && copy_point1[1] >= copy_point2[1])
{
int temp_y;
temp_y = copy_point1[1];
copy_point1[1] = copy_point2[1];
copy_point2[1] = temp_y;
}
}
int normal_copy_paste(int copy_point1[], int copy_point2[], int paste_point1[], int paste_point2[])
{
int num_of_changes = 0, l, m, i, j;
for (i = copy_point1[0], l = paste_point1[0];
i <= copy_point2[0] && l <= paste_point2[0];
i++, l++)
{
for (j = copy_point1[1], m = paste_point1[1];
j <= copy_point2[1] && m <= paste_point2[1];
j++, m++)
{
if (image_arr[l][m] != image_arr[i][j])
{
image_arr[l][m] = image_arr[i][j];
num_of_changes++;
}
}
}
return num_of_changes;
}
int cosine(int theta)
{
if (theta == 0)
{
return 1;
}
else if (theta == 180 || theta == -180)
{
return -1;
}
else
{
return 0;
}
}
int sine(int theta)
{
if (theta == 90 || theta == -270)
{
return 1;
}
else if (theta == 270 || theta == -90)
{
return -1;
}
else
{
return 0;
}
}
void rotate_and_copy(int aux_arr[][12], int image_arr[][12],
int startx, int endx,
int starty, int endy)
{
int i, j, x, y, size = endx - startx + 1;
for (i = startx, y = size - 1; i <= endx; i++, y--)
{
for (j = starty, x = 0; j <= endy; j++, x++)
{
aux_arr[x][y] = image_arr[i][j];
}
}
}
int paste_rotated(int aux_arr[][12], int startx, int endx, int starty, int endy)
{
int i, j, temp = starty, num_of_changes = 0;
for (i = 0; startx <= endx; startx++, i++)
{
for (j = 0, starty = temp; starty <= endy; starty++, j++)
{
if (image_arr[startx][starty] != aux_arr[i][j])
{
image_arr[startx][starty] = aux_arr[i][j];
num_of_changes++;
}
}
}
return num_of_changes;
}