/
paint2.c
199 lines (168 loc) · 3.12 KB
/
paint2.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
#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
char canvas[WIDTH][HEIGHT];
char *history[HISTORY_SIZE];
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);
}
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;
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);
}
}