Here's my sourcecode
Code: Select all
#include <stdio.h>
int xcounter, ycounter; /* Simple counting vars */
int x1, x2, y1, y2, swap; /* Coordinates used in commands */
int C=260, R=260; /* M is num of columns, N is num of rows */
char cmd; /* Actual command to be processed */
char color, oldcolor; /* The color stored in the image array */
char filename[255]; /* Place to the store the file name string */
int rows=260; /* Maximum number of rows */
int cols=260; /* Maximum number of columns */
char image[260][260]; /* Array to hold the image */
int tempx, tempy;
char tempnewcolor, tempoldcolor;
void floodfill(tempx, tempy, tempnewcolor, tempoldcolor)
{
/* Is this a legal pixel */
if ((tempx<1)||(tempx>C)||(tempy<1)||(tempy>R))
return;
if (image[tempy][tempx]==tempoldcolor)
{
image[tempy][tempx]=tempnewcolor;
floodfill(tempx+1,tempy,tempnewcolor,tempoldcolor);
floodfill(tempx-1,tempy,tempnewcolor,tempoldcolor);
floodfill(tempx,tempy+1,tempnewcolor,tempoldcolor);
floodfill(tempx,tempy-1,tempnewcolor,tempoldcolor);
}
else
{
return;
}
}
int main(int argc, char *argv[])
{
while (1)
{
/* Read in a command */
scanf("%c", &cmd);
switch(cmd) /* Process the input */
{
case 'X':
return 0; /* End the program */
break;
case 'I':
/* Create a new image and init to white (O) */
scanf("%d %d",&C, &R);
for (ycounter=1;ycounter<=R;ycounter++)
{
for (xcounter=1;xcounter<=C;xcounter++)
{
image[ycounter][xcounter]='O';
}
}
break;
case 'C':
/* Clear the table already made to white */
for (ycounter=1;ycounter<=R;ycounter++)
{
for (xcounter=1;xcounter<=C;xcounter++)
{
image[ycounter][xcounter]='O';
}
}
break;
case 'L':
/* Get the pixel then color it */
scanf("%d %d %c",&x1, &y1, &color);
image[y1][x1]=color;
break;
case 'V':
/* Draw a vertical segment of color in X, between
rows y1 and y2 inclusive */
scanf("%d %d %d %c",&x1,&y1,&y2,&color);
if (y1<=y2)
{
for(ycounter=y1;ycounter<=y2;ycounter++)
image[ycounter][x1]=color;
}
else
{
for(ycounter=y2;ycounter<=y1;ycounter++)
image[ycounter][x1]=color;
}
break;
case 'H':
/* Draw a horizontal segment of color in Y between cols
x1 and x2 inclusive */
scanf("%d %d %d %c",&x1,&x2,&y1,&color);
if (x1<=x2)
{
for(xcounter=x1;xcounter<=x2;xcounter++)
image[y1][xcounter]=color;
}
else
{
for(xcounter=x2;xcounter<=x1;xcounter++)
image[y1][xcounter]=color;
}
break;
case 'K':
/* Draw a filled in rectangle of color */
scanf("%d %d %d %d %c",&x1,&x2,&y1,&y2,&color);
/* What follows here is a way to always go from 1
end to the other, no matter which number is
bigger than the other */
for (ycounter=y1;
((y1<=y2)?(ycounter<=y2):(ycounter>=y2));
((y1<=y2)?(ycounter++):(ycounter--)) )
{
for (xcounter = x1;
((x1<=x2)?(xcounter<=x2):(xcounter>=x2));
((x1<=x2)?(xcounter++):(xcounter--)) )
{
image[ycounter][xcounter]=color;
}
}
break;
case 'F':
/* Perform the notorious flood fill */
scanf("%d %d %c",&x1,&y1,&color);
oldcolor=image[y1][x1];
floodfill(x1,y1,color,oldcolor);
break;
case 'S':
color=getchar();
gets(filename);
printf("%s\n",filename);
for (ycounter=1;ycounter<=R;ycounter++)
{
for (xcounter=1;xcounter<=C;xcounter++)
{
printf("%c",image[ycounter][xcounter]);
}
printf("\n");
}
break;
}
}
return 0;
}