/
paint4.c
287 lines (251 loc) · 4.76 KB
/
paint4.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define WIDTH 70
#define HEIGHT 40
#define BUFSIZE 1000
#define COM_OTHER 0
#define COM_QUIT 1
#define HISTORY_SIZE 100
#define COM_UNDO 2
char canvas[WIDTH][HEIGHT];
char *history[HISTORY_SIZE];
char copy[WIDTH][HEIGHT];
void print_canvas(FILE *fp)
{
int x, y;
fprintf(fp, "----------\n");
for (y = 0; y < HEIGHT; y++) {
for (x = 0; x < WIDTH; x++) {
char c = canvas[x][y];
fputc(c, fp);
}
fputc('\n', fp);
}
fflush(fp);
}
int init_canvas()
{
memset(canvas, ' ', sizeof(canvas));
}
void draw_line(int x0, int y0, int x1, int y1)
{
double dx, dy;
double k;
int x,y,xs,xf,ys,yf;
dx = x1-x0;
dy = y1-y0;
if(x0 <x1){
xs= x0;
xf =x1;
}
else {
xs = x1;
xf = x0;
}
if(y0 <y1){
ys= y0;
yf =y1;
}
else {
ys = y1;
yf = y0;
}
if (dx != 0){
k = dy/dx;
y = ys;
for(x = xs; x <=xf; x++){
canvas[x][(int)(y+0.5)] = '#';
y += k;
}
}
else{
for(y = ys; y <= yf; y ++){
canvas[x0][y] = '#';
}
}
}
void draw_circle(int x0, int y0, int r){
int x,y;
double x1,y1;
double t;
for(t = 0; t <2*M_PI;t += M_PI/160){
x1 = x0+r*sin(t);
y1 = y0+r*cos(t);
if ((x1-(int)x1) < 0.5){
x = (int)x1;
}
else{
x = (int)(x1) +1;
}
if ((y1-(int)y1) < 0.5){
y = (int)y1;
}
else{
y = (int)(y1) +1;
}
canvas[x][y] = '#';
}
}
void line_command()
{
int x0, y0, x1, y1;
x0 = atoi(strtok(NULL, " "));
y0 = atoi(strtok(NULL, " "));
x1 = atoi(strtok(NULL, " "));
y1 = atoi(strtok(NULL, " "));
draw_line(x0, y0, x1, y1);
}
void rec_command(){
int x0,y0,x1,y1;
x0 = atoi(strtok(NULL," "));
y0 = atoi(strtok(NULL," "));
x1 = atoi(strtok(NULL," "));
y1 = atoi(strtok(NULL," "));
draw_line(x0,y0,x1,y0);
draw_line(x1,y0,x1,y1);
draw_line(x1,y1,x0,y1);
draw_line(x0,y1,x0,y0);
}
void circle_command(){
int x0,y0,r;
x0 = atoi(strtok(NULL," "));
y0 = atoi(strtok(NULL," "));
r = atoi(strtok(NULL," "));
draw_circle(x0,y0,r);
}
void undo_command(){
int n;
n =0;
while (strcmp(history[n],"undo\n") != 0){
n++;
}
free(history[n]);
int com,m;
const char *canvas_file = "canvas.txt";
FILE *fp;
if ((fp = fopen(canvas_file, "w")) == NULL) {
fprintf(stderr, "error: cannot open %s.\n", canvas_file);
}
init_canvas();
print_canvas(fp);
for(m = 0; m < n-1; m ++){
com = interpret_command(history[m]);
if(strcmp(history[m],"undo\n") == 0)
break;
}
}
/*void fill_command(){
int x0,y0;
x0 = atoi(strtok(NULL," "));
y0 = atoi(strtok(NULL," "));
draw_fill(x0,y0);
}*/
/*void draw_fill(int x0,int y0){
int i,j;
for(i = x0-1; i <= x0+1; i++){
for (j = y0-1; j <= y0+1; j ++){
if(canvas[i][j]='#') {
canvas[i][j]='#';
draw_fill(i,j);
}
}
}
}*/
void copy_command(){
int x0,y0,x1,y1;
int x,y;
for(x= 0; x <WIDTH; x++){
for(y = 0; y <HEIGHT; y++){
copy[x][y] = ' ';
}
}
x0 = atoi(strtok(NULL," "));
y0 = atoi(strtok(NULL," "));
x1 = atoi(strtok(NULL," "));
y1 = atoi(strtok(NULL," "));
for (x = x0; x< x1; x++){
for (y = y0; y < y1; y ++){
copy[x-x0][y-y0] = canvas[x][y];
}
}
}
void paste_command(){
int x0,y0,x1,y1;
int x,y;
x0 = atoi(strtok(NULL," "));
y0 = atoi(strtok(NULL," "));
x1 = atoi(strtok(NULL," "));
y1 = atoi(strtok(NULL," "));
for (x = x0; x< x1; x++){
for (y = y0; y < y1; y ++){
canvas[x][y] = copy[x-x0][y-y0];
}
}
}
int interpret_command(const char *command)
{
char buf[BUFSIZE];
strcpy(buf, command);
char *s = strtok(buf, " ");
char c = tolower(s[0]);
switch (c) {
case 'l':
line_command();
break;
case 'q':
return COM_QUIT;
case 'r':
rec_command();
break;
case'c':
circle_command();
break;
case 'u':
undo_command();
break;
/*case 'f':
fill_command();
break;*/
case 'a':
copy_command();
break;
case 'p':
paste_command();
break;
default:
break;
}
return COM_OTHER;
}
int main()
{
int com, n, i;
const char *canvas_file = "canvas.txt";
FILE *fp;
char buf[BUFSIZE];
if ((fp = fopen(canvas_file, "w")) == NULL) {
fprintf(stderr, "error: cannot open %s.\n", canvas_file);
return 1;
}
init_canvas();
print_canvas(fp);
n = 0;
while (1) {
printf("%d > ", n);
fgets(buf, BUFSIZE, stdin);
history[n] = (char*)malloc(sizeof(char) * strlen(buf));
strcpy(history[n], buf);
if (++n >= HISTORY_SIZE) break;
com = interpret_command(buf);
if (com == COM_QUIT) break;
print_canvas(fp);
}
fclose(fp);
if ((fp = fopen("history.txt", "w")) != NULL) {
for (i = 0; i < n; i++)
fprintf(fp, "%s", history[i]);
fclose(fp);
}
}