#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int over = 1;
int change = 1;
// 画布由20行,36列的二维数组成,通过符号常数定义
#define N_ROWS 20
#define N_COLS 36
// 初始给出的两种阴影的定义
#define BLACK 0 // 表示黑色
#define DARK 1
#define GREY 2
#define LIGHT 3
#define WHITE 4 // 表示白色
int color = BLACK;
int brush[3][3];
// 显示画布内容
void printCanvas(int canvas[N_ROWS][N_COLS]);
// 将整个画布设置为白色背景,即画布的初始化操作
void setBlankCanvas(int canvas[N_ROWS][N_COLS]);
// 显示帮助信息
void Show_help_info();
void item(int canvas[N_ROWS][N_COLS]);
void item1(int canvas[N_ROWS][N_COLS]);
void itembush(int canvas[N_ROWS][N_COLS]);
void item2(int canvas[N_ROWS][N_COLS]);
void item3();
void item4(int canvas[N_ROWS][N_COLS]);
void item5();
void item6(int canvas[N_ROWS][N_COLS]);
void item7(int canvas[N_ROWS][N_COLS]);
void item8();
void item9(int canvas[N_ROWS][N_COLS]);
void Stick(int canvas[N_ROWS][N_COLS], int row, int col, int tm[row][col], int target_row, int target_col)
{
int i, j;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
canvas[target_row + i][target_col + j] = tm[i][j];
}
void Copy(int canvas[N_ROWS][N_COLS], int row, int col, int tm[row][col], int start_row, int start_col)
{
int i, j;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
tm[i][j] = canvas[start_row + i][start_col + j];
}
void Fix(int canvas[N_ROWS][N_COLS])
{
for (int i = 0; i < N_ROWS; i++)
for (int j = 0; j < N_COLS; j++)
{
if (canvas[i][j] > 4)
canvas[i][j] = 4;
else if (canvas[i][j] < 0)
canvas[i][j] = 0;
}
}
int main(void)
{
int canvas[N_ROWS][N_COLS]; // 定义画布对应的二维整型数组
setBlankCanvas(canvas); // 画布的初始化操作
printCanvas(canvas); // 显示当前画布内容
Show_help_info(); // 显示程序的使用说明帮助
item(canvas);
getchar();
}
// ---------------------------------------------------
// 在终端上面显示画布内容
// ---------------------------------------------------
void printCanvas(int canvas[N_ROWS][N_COLS])
{
int row = 0;
int i = 0;
printf(" ");
while (i < N_COLS)
{
printf("%2d", i);
i++;
}
printf("\n -------------------------------------------------------------------------\n");
while (row < N_ROWS)
{
int col = 0;
printf("%2d|", row);
while (col < N_COLS)
{
printf("%2d", canvas[row][col]);
col++;
}
row++;
printf(" |\n");
}
printf(" -------------------------------------------------------------------------\n");
}
// 将整个画布设置为白色背景,即画布的初始化操作
void setBlankCanvas(int canvas[N_ROWS][N_COLS])
{
int row = 0;
while (row < N_ROWS)
{
int col = 0;
while (col < N_COLS)
{
canvas[row][col] = WHITE;
col++;
}
row++;
}
}
// --------------------------------------------------------------
// 显示帮助信息
// --------------------------------------------------------------
void Show_help_info()
{
printf("0. Show this help screen!\n");
printf("1. Draw a straight line (horizontal, vertical, or 45cm 135 degree diagonal)\n");
printf("2. Rectangular area filling\n");
printf("3. Change the gray scale of the brush\n");
printf("4. Copy and paste of rectangular area\n");
printf("5. Define additive brushes\n");
printf("6. Reset the canvas\n");
printf("7. Show only the contents of the canvas\n");
printf("8. Displays the contents of the current 9-pixel brush\n");
printf("9. Display the contents of the current canvas\n");
printf("Enter other non-numeric characters to exit the program!\n");
}
void item(int canvas[N_ROWS][N_COLS])
{
char x;
int i;
while (over)
{
scanf("%c", &x);
getchar();
i = x - '0';
switch (i)
{
case 0:
Show_help_info();
break;
case 1:
if (change)
item1(canvas);
else
itembush(canvas);
break;
case 2:
item2(canvas);
break;
case 3:
item3();
break;
case 4:
item4(canvas);
break;
case 5:
item5();
break;
case 6:
item6(canvas);
break;
case 7:
item7(canvas);
break;
case 8:
item8();
break;
case 9:
item9(canvas);
break;
default:
printf("Press any key to continue.\n");
over = 0;
break;
}
}
}
void item1(int canvas[N_ROWS][N_COLS])
{
int start_row, start_col, end_row, end_col;
int a = 1;
scanf("%d%d%d%d", &start_row, &start_col, &end_row, &end_col);
if (start_row < 0 || start_row >= N_ROWS || start_col < 0 || start_col >= N_COLS ||
end_row < 0 || end_row >= N_ROWS || end_col < 0 || end_col >= N_COLS)
printf("Drawing area beyond canvas\n");
else if (start_row != end_row && start_col != end_col && abs(end_row - start_row) != abs(end_col - start_col))
printf("Can't forn a diagonal of 45 degrees.\nYou can't draw a normal slash.\n");
else if (start_row == end_row && start_col != end_col)
{
if (start_col < end_col)
for (int i = 0; i <= end_col - start_col; i++)
canvas[start_row][start_col + i] = color;
else
for (int i = 0; i < start_col - end_col; i++)
canvas[start_row][start_col - i] = color;
}
else if (start_col == end_col && start_row != end_row)
{
if (start_row < end_row)
for (int i = 0; i <= end_row - start_row; i++)
canvas[start_row + i][start_col] = color;
else
for (int i = 0; i <= start_row - end_row; i++)
canvas[start_row - i][start_col] = color;
}
else if (abs(end_row - start_row) == abs(end_col - start_col))
{
if (start_row <= end_row && start_col <= end_col)
for (int i = 0; i <= abs(end_row - start_row); i++)
canvas[start_row + i][start_col + i] = color;
else if (start_row <= end_row && start_col > end_col)
for (int i = 0; i <= abs(end_row - start_row); i++)
canvas[start_row + i][start_col - i] = color;
else if (start_row > end_row && start_col <= end_col)
for (int i = 0; i <= abs(end_row - start_row); i++)
canvas[start_row - i][start_col + i] = color;
else if (start_row > end_row && start_col > end_col)
for (int i = 0; i <= abs(end_row - start_row); i++)
canvas[start_row - i][start_col - i] = color;
}
getchar();
}
void itembush(int canvas[N_ROWS][N_COLS])
{
int start_row, start_col, end_row, end_col;
int a = 1;
scanf("%d%d%d%d", &start_row, &start_col, &end_row, &end_col);
if (start_row < 0 || start_row >= N_ROWS || start_col < 0 || start_col >= N_COLS ||
end_row < 0 || end_row >= N_ROWS || end_col < 0 || end_col >= N_COLS)
{
printf("Drawing area beyond canvas\n");
getchar();
return;
}
else if (start_row < 1 || start_row >= N_ROWS - 1 || start_col < 1 || start_col >= N_COLS - 1 ||
end_row < 1 || end_row >= N_ROWS - 1 || end_col < 1 || end_col >= N_COLS - 1)
{
if (start_row < 1 && end_row < 1 && start_col == end_col || start_row >= N_ROWS - 1 && end_row >= N_ROWS - 1 && start_col == end_col ||
start_col < 1 && end_col < 1 && start_row == end_row || start_col >= N_ROWS - 1 && end_col >= N_ROWS - 1 && start_row == end_row)
{
printf("Drawing area beyond canvas\n");
getchar();
return;
}
else
{
if (start_row == 0)
start_row++;
if (start_row == N_ROWS - 1)
start_row--;
if (end_row == 0)
end_row++;
if (end_row == N_ROWS - 1)
end_row--;
if (start_col == 0)
start_col++;
if (start_col == N_ROWS - 1)
start_col--;
if (end_col == 0)
end_col++;
if (end_col == N_ROWS - 1)
end_col--;
}
}
if (start_row != end_row && start_col != end_col && abs(end_row - start_row) != abs(end_col - start_col))
printf("Can't forn a diagonal of 45 degrees.\nYou can't draw a normal slash.\n");
else if (start_row == end_row && start_col != end_col)
{
if (start_col < end_col)
for (int i = 0; i <= end_col - start_col; i++)
{
canvas[start_row - 1][start_col + i - 1] += brush[0][0];
canvas[start_row - 1][start_col + i] += brush[0][1];
canvas[start_row - 1][start_col + i + 1] += brush[0][2];
canvas[start_row][start_col + i - 1] += brush[1][0];
canvas[start_row][start_col + i] += brush[1][1];
canvas[start_row][start_col + i + 1] += brush[1][2];
canvas[start_row + 1][start_col + i - 1] += brush[2][0];
canvas[start_row + 1][start_col + i] += brush[2][1];
canvas[start_row + 1][start_col + i + 1] += brush[2][2];
}
else
for (int i = 0; i < start_col - end_col; i++)
{
//canvas[start_row][start_col - i] = color;
canvas[start_row - 1][start_col - i - 1] += brush[0][0];
canvas[start_row - 1][start_col - i] += brush[0][1];
canvas[start_row - 1][start_col - i + 1] += brush[0][2];
canvas[start_row][start_col - i - 1] += brush[1][0];
canvas[start_row][start_col - i] += brush[1][1];
canvas[start_row][start_col - i + 1] += brush[1][2];
canvas[start_row + 1][start_col - i - 1] += brush[2][0];
canvas[start_row + 1][start_col - i] += brush[2][1];
canvas[start_row + 1][start_col - i + 1] += brush[2][2];
}
}
else if (start_col == end_col && start_row != end_row)
{
if (start_row < end_row)
for (int i = 0; i <= end_row - start_row; i++)
{
//canvas[start_row + i][start_col] = color;
canvas[start_row + i - 1][start_col - 1] += brush[0][0];
canvas[start_row + i - 1][start_col] += brush[0][1];
canvas[start_row + i - 1][start_col + 1] += brush[0][2];
canvas[start_row + i][start_col - 1] += brush[1][0];
canvas[start_row + i][start_col] += brush[1][1];
canvas[start_row + i][start_col + 1] += brush[1][2];
canvas[start_row + i + 1][start_col - 1] += brush[2][0];
canvas[start_row + i + 1][start_col] += brush[2][1];
canvas[start_row + i + 1][start_col + 1] += brush[2][2];
}
else
for (int i = 0; i <= start_row - end_row; i++)
{
//canvas[start_row - i][start_col] = color;
canvas[start_row - i - 1][start_col - 1] += brush[0][0];
canvas[start_row - i - 1][start_col] += brush[0][1];
canvas[start_row - i - 1][start_col + 1] += brush[0][2];
canvas[start_row - i][start_col - 1] += brush[1][0];
canvas[start_row - i][start_col] += brush[1][1];
canvas[start_row - i][start_col + 1] += brush[1][2];
canvas[start_row - i + 1][start_col - 1] += brush[2][0];
canvas[start_row - i + 1][start_col] += brush[2][1];
canvas[start_row - i + 1][start_col + 1] += brush[2][2];
}
}
else if (abs(end_row - start_row) == abs(end_col - start_col))
{
if (start_row <= end_row && start_col <= end_col)
for (int i = 0; i <= abs(end_row - start_row); i++)
{
//canvas[start_row + i][start_col + i] = color;
canvas[start_row + i - 1][start_col + i - 1] += brush[0][0];
canvas[start_row + i - 1][start_col + i] += brush[0][1];
canvas[start_row + i - 1][start_col + i + 1] += brush[0][2];
canvas[start_row + i][start_col + i - 1] += brush[1][0];
canvas[start_row + i][start_col + i] += brush[1][1];
canvas[start_row + i][start_col + i + 1] += brush[1][2];
canvas[start_row + i + 1][start_col + i - 1] += brush[2][0];
canvas[start_row + i + 1][start_col + i] += brush[2][1];
canvas[start_row + i + 1][start_col + i + 1] += brush[2][2];
}
else if (start_row <= end_row && start_col > end_col)
for (int i = 0; i <= abs(end_row - start_row); i++)
{
//canvas[start_row + i][start_col - i] = color;
canvas[start_row + i - 1][start_col - i - 1] += brush[0][0];
canvas[start_row + i - 1][start_col - i] += brush[0][1];
canvas[start_row + i - 1][start_col - i + 1] += brush[0][2];
canvas[start_row + i][start_col - i - 1] += brush[1][0];
canvas[start_row + i][start_col - i] += brush[1][1];
canvas[start_row + i][start_col - i + 1] += brush[1][2];
canvas[start_row + i + 1][start_col - i - 1] += brush[2][0];
canvas[start_row + i + 1][start_col - i] += brush[2][1];
canvas[start_row + i + 1][start_col - i + 1] += brush[2][2];
}
else if (start_row > end_row && start_col <= end_col)
for (int i = 0; i <= abs(end_row - start_row); i++)
{
//canvas[start_row - i][start_col + i] = color;
canvas[start_row - i - 1][start_col + i - 1] += brush[0][0];
canvas[start_row - i - 1][start_col + i] += brush[0][1];
canvas[start_row - i - 1][start_col + i + 1] += brush[0][2];
canvas[start_row - i][start_col + i - 1] += brush[1][0];
canvas[start_row - i][start_col + i] += brush[1][1];
canvas[start_row - i][start_col + i + 1] += brush[1][2];
canvas[start_row - i + 1][start_col + i - 1] += brush[2][0];
canvas[start_row - i + 1][start_col + i] += brush[2][1];
canvas[start_row - i + 1][start_col + i + 1] += brush[2][2];
}
else if (start_row > end_row && start_col > end_col)
for (int i = 0; i <= abs(end_row - start_row); i++)
{
//canvas[start_row - i][start_col - i] = color;
canvas[start_row - i - 1][start_col - i - 1] += brush[0][0];
canvas[start_row - i - 1][start_col - i] += brush[0][1];
canvas[start_row - i - 1][start_col - i + 1] += brush[0][2];
canvas[start_row - i][start_col - i - 1] += brush[1][0];
canvas[start_row - i][start_col - i] += brush[1][1];
canvas[start_row - i][start_col - i + 1] += brush[1][2];
canvas[start_row - i + 1][start_col - i - 1] += brush[2][0];
canvas[start_row - i + 1][start_col - i] += brush[2][1];
canvas[start_row - i + 1][start_col - i + 1] += brush[2][2];
}
}
Fix(canvas);
getchar();
}
void item2(int canvas[N_ROWS][N_COLS])
{
int start_row, start_col, end_row, end_col;
scanf("%d%d%d%d", &start_row, &start_col, &end_row, &end_col);
if (start_row < 0 || start_row >= N_ROWS || start_col < 0 || start_col >= N_COLS ||
end_row < 0 || end_row >= N_ROWS || end_col < 0 || end_col >= N_COLS)
printf("Drawing area beyond canvas\n");
else if (end_row >= start_row && end_col >= start_col)
for (int i = start_row; i <= end_row; i++)
for (int j = start_col; j <= end_col; j++)
canvas[i][j] = color;
else if (end_row < start_row && end_col >= start_col)
for (int i = end_row; i <= start_row; i++)
for (int j = start_col; j <= end_col; j++)
canvas[i][j] = color;
else if (end_row < start_row && end_col < start_col)
for (int i = end_row; i <= start_row; i++)
for (int j = end_col; j <= start_col; j++)
canvas[i][j] = color;
else if (end_row >= start_row && end_col < start_col)
for (int i = start_row; i <= end_row; i++)
for (int j = end_col; j <= start_col; j++)
canvas[i][j] = color;
getchar();
}
void item3()
{
int i;
scanf("%d", &i);
getchar();
switch (i)
{
case 0:
color = BLACK;
break;
case 1:
color = DARK;
break;
case 2:
color = GREY;
break;
case 3:
color = LIGHT;
break;
case 4:
color = WHITE;
break;
default:
printf("Invalid shade command!\n");
break;
}
change = 1;
}
void item4(int canvas[N_ROWS][N_COLS])
{
int start_row, start_col, end_row, end_col, target_row, target_col, tmp;
scanf("%d%d%d%d%d%d", &start_row, &start_col, &end_row, &end_col, &target_row, &target_col);
int tm[abs(end_row - start_row) + 1][abs(end_col - start_col) + 1];
if (start_row < 0 || start_row >= N_ROWS || start_col < 0 || start_col >= N_COLS ||
end_row < 0 || end_row >= N_ROWS || end_col < 0 || end_col >= N_COLS || target_row < 0 ||
target_col < 0 || target_col >= N_COLS || target_row >= N_ROWS ||
target_col + abs(end_col - start_col) >= N_COLS ||
target_row + abs(end_row - start_row) >= N_ROWS)
printf("Drawing area beyond canvas\n");
else if (start_row > end_row && start_col <= end_col)
{
tmp = end_row;
end_row = start_row;
start_row = tmp;
Copy(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, start_row, start_col);
Stick(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, target_row, target_col);
}
else if (start_row <= end_row && start_col > end_col)
{
tmp = end_col;
end_col = start_col;
start_col = tmp;
Copy(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, start_row, start_col);
Stick(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, target_row, target_col);
}
else if (start_row > end_row && start_col > end_col)
{
tmp = end_row;
end_row = start_row;
start_row = tmp;
tmp = end_col;
end_col = start_col;
start_col = tmp;
Copy(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, start_row, start_col);
Stick(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, target_row, target_col);
}
else
{
Copy(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, start_row, start_col);
Stick(canvas, abs(end_row - start_row) + 1, abs(end_col - start_col) + 1, tm, target_row, target_col);
}
getchar();
}
void item5()
{
change = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
scanf("%d", &brush[i][j]);
getchar();
}
void item6(int canvas[N_ROWS][N_COLS])
{
setBlankCanvas(canvas);
printCanvas(canvas);
item8();
change = 1;
}
void item7(int canvas[N_ROWS][N_COLS])
{
int row = 0;
int i = 0;
printf(" ");
while (i < N_COLS)
{
printf("%2d", i);
i++;
}
printf("\n -------------------------------------------------------------------------\n");
while (row < N_ROWS)
{
int col = 0;
printf("%2d|", row);
while (col < N_COLS)
{
if (canvas[row][col] != 4)
printf("%2d", canvas[row][col]);
else
printf(" ");
col++;
}
row++;
printf(" |\n");
}
printf(" -------------------------------------------------------------------------\n");
}
void item8()
{
if (change)
printf("Signal Pixel Brush!\n");
else
printf("Nine Pixel Brush!\n");
printf("Shade = %d\n", color);
}
void item9(int canvas[N_ROWS][N_COLS])
{
printCanvas(canvas);
}
//Written on March 28,2021.
//BUPT 2020 LinZhi.
NJUST 2021 机械工程学院大一C语言大作业 简易绘图板
最新推荐文章于 2024-09-17 11:14:41 发布
这是一个使用C语言实现的图形画布程序,允许用户进行线条绘制、矩形填充、颜色选择、区域复制粘贴等操作。程序初始化为白色背景,并提供了丰富的帮助信息,用户可以输入数字指令来执行不同的绘图命令。程序中还包含了一个九宫格刷子的概念,用于增强画布的编辑功能。
摘要由CSDN通过智能技术生成