当前位置: 首页 > 工具软件 > medium-editor > 使用案例 >

Graphical Editor

祖麻雀
2023-12-01
110105 Graphical Editor
Graphical editors such as Photoshop allow us to alter bit-mapped images in the same way that
text editors allow us to modify documents. Images are represented as an M N array of pixels, where
each pixel has a given color.
Your task is to write a program which simulates a simple interactive graphical editor.
Input
The input consists of a sequence of editor commands, one per line. Each command is represented
by one capital letter placed as the rst character of the line. If the command needs parameters, they
will be given on the same line separated by spaces.
Pixel coordinates are represented by two integers, a column number between 1 : : : M and a row
number between 1: : : N, where 1 M; N 250. The origin sits in the upper-left corner of the table.
Colors are speci ed by capital letters.
The editor accepts the following commands:
I M N Create a new M N image with all pixels initially
colored white (O).
C Clear the table by setting all pixels white (O). The
size remains unchanged.
L X Y C Colors the pixel (X; Y ) in color (C).
V X Y1 Y2 C Draw a vertical segment of color (C) in column X,
between the rows Y 1 and Y 2 inclusive.
H X1 X2 Y C Draw a horizontal segment of color (C) in the row Y ,
between the columns X1 and X2 inclusive.
K X1 Y1 X2 Y2 C Draw a lled rectangle of color C, where (X1; Y 1) is
the upper-left and (X2; Y 2) the lower right corner.
F X Y C Fill the region R with the color C, where R is de ned
as follows. Pixel (X; Y ) belongs to R. Any other pixel
which is the same color as pixel (X; Y ) and shares a
common side with any pixel in R also belongs to this
region.
S Name Write the le name in MSDOS 8.3 format followed by
the contents of the current image.
X Terminate the session.
Output
On every command S NAME, print the lename NAME and contents of the current image. Each
row is represented by the color contents of each pixel. See the sample output.
Ignore the entire line of any command de ned by a character other than I, C, L, V, H, K,
F, S, or X, and pass on to the next command. In case of other errors, the program behavior is
unpredictable.
Sample Input
I 5 6
L 2 3 A
S one.bmp

G 2 3 J

F 3 3 J
V 2 3 4 W
H 3 4 2 Z
S two.bmp
X
Sample Output
one.bmp
OOOOO
OOOOO
OAOOO
OOOOO
OOOOO
OOOOO
two.bmp
JJJJJ
JJZZJ
JWJJJ
JWJJJ
JJJJJ
JJJJJ


思路:刚开始一个没想通Fill函数的意思,后来我问了别人才知道,从一开始建立一个图像,后面的命令都是在修改这个图形,也就是说在two.bmp中使用的命令,是在one.bmp里面进行修改得到的图形,当时我理解的是two.bmp是一个新的图形,所以一直未能理解fill函数的意义。

           通过一个switch 来解决问题,然后显示那些命令。

          关于fill函数,我但是用到一个递归,来查找旁边与之相同的颜色,这也是图论里面深度搜索的一个知识点。


代码:

#include <stdio.h>

int M,N;

//将图像清除,并且将所有涂成白色 
void reset(char *s){//N 为行,M为列 
     int i,j;
     for(i = 0 ;i < N;i++){
        for(j = 0;j < M;j++)
              *(s + i*N+j) = 'o';
        printf("\n");
     }         
}

//把像素(x,y)涂成新的颜色,即 new_color 
void ser_pixel(char *s,int x,int y,char new_color){
     *(s + x*N+y) = new_color;     
}

//将左上角(x1,y1)到(x2,y2)形成的矩形,涂成颜色 new_color 
void file_rect(char *s,int x1,int y1,int x2,int y2,char new_color){
    int i,j;
        for(i = x1;i <= x2;i++){
          for(j = y1;j <= y2;j++)
             *(s + i*N + j) = new_color;
            printf("\n");  
          }        
}

//将某区域涂成新颜色 new_color 
void fill_region(char *s,int x,int y,char old_color,char new_color){//x 为 行,y 为 列 
    // 当新旧颜色相同时需要结束递归,否则会形成无限循环。  
    if (old_color == new_color)  
        return;  
        
    *(s + x*N + y) = new_color; 
    
    if (x > 0)  
        if (*(s + (x-1)* N + y) == old_color)  
            fill_region(s, x - 1, y, old_color, new_color);  
      
    if (x < N - 1)  
        if (*(s + (x+1)* N + y) == old_color)  
            fill_region(s, x + 1, y, old_color, new_color);  
      
    if (y > 0)  
        if (*(s + x*N+ y-1) == old_color)  
            fill_region(s, x, y - 1, old_color, new_color);  
      
    if (y < M - 1)  
        if (*(s + x* N + y+1) == old_color)  
            fill_region(s, x, y + 1, old_color, new_color);        
}

int main(){
    int i,j,x1,x2,y1,y2,x,y;
    char cmd,color;
    //scanf("%d %d",&M,&N);
    char pixel[250][250];
    while(scanf("%c",&cmd) != 'X'){
       switch(cmd){
                case 'I':
                    scanf("%d %d",&M,&N);
                    for(i = 0;i < N;i++){
                      for(j =0;j < M;j++)
                         printf("o");
                     printf("\n");    
                    }
                    break;
                 case 'C':
                     reset(&pixel[0][0]);
                     break;
                 case 'L':
                      scanf("%d %d %c",&x,&y,&color);
                      ser_pixel(&pixel[0][0], x, y,color);          
                      break;
                 case 'V':
                      scanf("%d %d %d %c",&y1,&x1,&x2,&color);
                      for(x = x1;x <= x2;x++)
                           pixel[x-1][y1-1] = color;
                      break;
                 case 'H':
                      scanf("%d %d %d %c",&y1,&y2,&x1,&color);
                      for(y = y1;y <= y2;y++)
                           pixel[x1-1][y-1] = color;
                      break;
                 case 'K':
                      scanf("%d %d %d %d %c",&x1,&y1,&x2,&y2,&color);
                      file_rect(&pixel[0][0], x1, y1, x2, y2,color);
                      break;
                 case 'F':
                      scanf("%d %d %c",&x,&y,&color);
                      fill_region(&pixel[0][0], x, y,pixel[x][y],color);
                      break;                     
                  }
           system("pause");       
    }            
    
}


还是有一些小错误,暂时没有找出来。

 类似资料:

相关阅读

相关文章

相关问答