-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.c
122 lines (107 loc) · 3.44 KB
/
draw.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
#include "functions.h"
#define yA 1
#define xA 2
#define yB 3
#define xB 4
#define yC 5
#define xC 6
#define WIDTH 3
#define HEIGHT 4
void draw(char *command, bmp_file *bmp, set *drawset) {
char *arg = malloc(MAX_CHARS_OP * sizeof(char));
check_alloc(arg, "draw");
int skip = strlen("draw");
int i = skip + 1, j = 0;
while (command[i] != ' ') {
arg[j++] = command[i++];
}
arg[j] = '\0';
if (strcmp(arg, "line") == 0) {
pt2D A, B;
i = 0;
char *ptr = strtok(command + skip + 1, " ");
while (ptr != NULL) {
switch (i) {
case yA:
A.y = atoi(ptr);
break;
case xA:
A.x = atoi(ptr);
break;
case yB:
B.y = atoi(ptr);
break;
case xB:
B.x = atoi(ptr);
break;
}
ptr = strtok(NULL, " ");
i++;
}
printf("Drawing line:\n");
printf("(%i,%i) -> (%i,%i)\n", A.y, A.x, B.y, B.x);
line(bmp, drawset, A, B);
} else {
if (strcmp(arg, "rectangle") == 0) {
uint width = 0, height = 0;
pt2D A;
i = 0;
char *ptr = strtok(command + skip + 1, " ");
while (ptr != NULL) {
switch (i) {
case yA:
A.y = atoi(ptr);
break;
case xA:
A.x = atoi(ptr);
break;
case WIDTH:
width = atoi(ptr);
break;
case HEIGHT:
height = atoi(ptr);
break;
}
ptr = strtok(NULL, " ");
i++;
}
printf("Drawing rectangle:\n");
printf("Start point: (%i,%i) W:%i H:%i\n", A.y, A.x, width, height);
rectangle(bmp, drawset, A, width, height);
} else {
if (strcmp(arg, "triangle") == 0) {
pt2D A, B, C;
i = 0;
char *ptr = strtok(command + skip + 1, " ");
while (ptr != NULL) {
switch (i) {
case yA:
A.y = atoi(ptr);
break;
case xA:
A.x = atoi(ptr);
break;
case yB:
B.y = atoi(ptr);
break;
case xB:
B.x = atoi(ptr);
break;
case yC:
C.y = atoi(ptr);
break;
case xC:
C.x = atoi(ptr);
}
ptr = strtok(NULL, " ");
i++;
}
printf("Drawing triangle:\n");
printf("A(%i,%i) ", A.y, A.x);
printf("B(%i,%i) ", B.y, B.x);
printf("C(%i,%i)\n", C.y, C.x);
triangle(bmp, drawset, A, B, C);
}
}
}
}