该程序实现了一个基于C语言的画布绘图系统,主要功能包括:
- 绘制直线(水平线、垂直线和对角线);
- 矩形区域填充;
- 复制粘贴画布区域;
- 画布显示和初始化。
系统支持两种画笔类型(单像素和9像素模板画笔),提供5种灰度等级,并包含坐标规范化、边界检查等辅助功能。
程序通过二维数组表示20x36的画布,包含调试模式和操作帮助信息,实现了基本的绘图操作功能。
c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
// ----------------------------------------------------
// 符号常量定义
// ----------------------------------------------------
#define OK 1 // 函数执行成功返回值
#define ERROR 0 // 函数执行失败返回值
#define N_ROWS 20 // 画布行数
#define N_COLS 36 // 画布列数
#define BLACK 0 // 黑色灰度等级
#define DARK 1 // 暗色灰度等级
#define GREY 2 // 灰色灰度等级
#define LIGHT 3 // 亮色灰度等级
#define WHITE 4 // 白色灰度等级
#define DEBUGINFO 1 // 调试模式开关
// ----------------------------------------------------
// 函数原型声明
// ----------------------------------------------------
/**
* 功能:显示程序操作帮助信息
* 参数:无
* 返回值:无
*/
void Show_help_info();
/**
* 功能:将坐标按左上到右下的顺序规范化
* 参数:start_end_coordination[4] - 包含起始和结束坐标的数组
* 索引说明:0=起始行,1=起始列,2=结束行,3=结束列
* 返回值:无
*/
void Coordination_Normalize(int start_end_coordination[4]);
// ----------------------------------------------------
// 画线功能相关函数
// ----------------------------------------------------
/**
* 功能:绘制直线(水平线、垂直线或45/135度对角线)
* 参数:start_end_coordination[4] - 起始和结束坐标
* canvas[N_ROWS][N_COLS] - 画布数组
* Brush_Type - 画笔类型(0:单像素画笔,1:相加画笔)
* brush_color - 画笔颜色灰度等级(0-4)
* Customized_Brush[3][3] - 自定义画笔9像素模板
* 返回值:OK(1)表示成功,ERROR(0)表示失败
*/
int Draw_Line(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]);
/**
* 功能:绘制水平线
* 参数:start_end_coordination[4] - 起始和结束坐标
* canvas[N_ROWS][N_COLS] - 画布数组
* Brush_Type - 画笔类型
* brush_color - 画笔颜色
* Customized_Brush[3][3] - 自定义画笔模板
* 返回值:无
*/
void Horizontal_Print(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]);
/**
* 功能:绘制垂直线
* 参数:start_end_coordination[4] - 起始和结束坐标
* canvas[N_ROWS][N_COLS] - 画布数组
* Brush_Type - 画笔类型
* brush_color - 画笔颜色
* Customized_Brush[3][3] - 自定义画笔模板
* 返回值:无
*/
void Vertical_Print(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]);
/**
* 功能:检查坐标能否构成45/135度对角线
* 参数:start_end_coordination[4] - 起始和结束坐标
* 返回值:1表示能构成对角线,0表示不能
*/
int Diagonal_Check(int start_end_coordination[4]);
/**
* 功能:绘制45/135度对角线
* 参数:start_end_coordination[4] - 起始和结束坐标
* canvas[N_ROWS][N_COLS] - 画布数组
* Brush_Type - 画笔类型
* brush_color - 画笔颜色
* Customized_Brush[3][3] - 自定义画笔模板
* 返回值:无
*/
void Diagonal_Print(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]);
// ----------------------------------------------------
// 矩形填充功能相关函数
// ----------------------------------------------------
/**
* 功能:矩形区域填充
* 参数:start_end_coordination[4] - 矩形区域的左上角和右下角坐标
* canvas[N_ROWS][N_COLS] - 画布数组
* Brush_Type - 画笔类型
* brush_color - 画笔颜色
* Customized_Brush[3][3] - 自定义画笔模板
* 返回值:OK(1)表示成功,ERROR(0)表示失败
*/
int Fill_Rectangle(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]);
// ----------------------------------------------------
// 复制粘贴功能相关函数
// ----------------------------------------------------
/**
* 功能:复制矩形区域并粘贴到目标位置
* 参数:start_end_coordination[4] - 要复制的矩形区域坐标
* target_row - 粘贴目标的起始行
* target_col - 粘贴目标的起始列
* canvas[N_ROWS][N_COLS] - 画布数组
* 返回值:OK(1)表示成功,ERROR(0)表示失败
*/
int Copy_Paste(int start_end_coordination[4],
int target_row,
int target_col,
int canvas[N_ROWS][N_COLS]);
// ----------------------------------------------------
// 画布显示功能相关函数
// ----------------------------------------------------
/**
* 功能:在终端上显示画布内容(包含背景)
* 参数:canvas[N_ROWS][N_COLS] - 画布数组
* 返回值:无
*/
void printCanvas(int canvas[N_ROWS][N_COLS]);
/**
* 功能:初始化画布为白色背景
* 参数:canvas[N_ROWS][N_COLS] - 画布数组
* 返回值:无
*/
void setBlankCanvas(int canvas[N_ROWS][N_COLS]);
/**
* 功能:只显示画布内容(将白色部分替换为空格)
* 参数:canvas[N_ROWS][N_COLS] - 画布数组
* 返回值:无
*/
void Display_Canvas(int canvas[N_ROWS][N_COLS]);
/**
* 功能:检查绘图区域是否越界
* 参数:start_end_coordination[4] - 要检查的区域坐标
* 返回值:1表示越界,0表示没有越界
*/
int Check_Drawing_Area(int start_end_coordination[4]);
// ----------------------------------------------------
// 相加画笔功能相关函数
// ----------------------------------------------------
/**
* 功能:根据参数定义9像素"相加画笔"
* 参数:UL, above, UR - 上方三个像素的灰度值
* left, center, right - 中间三个像素的灰度值
* LL, below, LR - 下方三个像素的灰度值
* Customized_Brush[3][3] - 存储自定义画笔的二维数组
* 返回值:无
*/
void Additive_Shape(int UL, int above, int UR,
int left, int center, int right,
int LL, int below, int LR,
int Customized_Brush[3][3]);
/**
* 功能:使用"相加画笔"在画布上绘制一个点
* 参数:Center_Row - 画笔中心点所在行
* Center_Col - 画笔中心点所在列
* canvas[N_ROWS][N_COLS] - 画布数组
* Customized_Brush[3][3] - 自定义画笔模板
* 返回值:无
*/
void Brush_Paint(int Center_Row,
int Center_Col,
int canvas[N_ROWS][N_COLS],
int Customized_Brush[3][3]);
/**
* 功能:显示当前画笔类型和画笔内容
* 参数:Brush_Type - 画笔类型
* brush_color - 画笔颜色灰度等级
* Customized_Brush[3][3] - 自定义画笔模板
* 返回值:无
*/
void Display_Brush(int Brush_Type,
int brush_color,
int Customized_Brush[3][3]);
// ----------------------------------------------------
// 主函数
// ----------------------------------------------------
/**
* 功能:程序主函数,处理用户命令输入并调用相应功能
* 参数:无
* 返回值:程序退出状态码
*/
int main(void) {
int Command; // 用户输入的命令编号(0-9)
int brush_color = BLACK; // 当前画笔灰度等级,初始为黑色
int current_color; // 用于临时存储新设置的灰度等级
int start_row, start_col, end_row, end_col; // 起始和结束坐标变量
int start_end_coordination[4]; // 坐标数组:0=起始行,1=起始列,2=结束行,3=结束列
int target_row, target_col; // 粘贴目标的坐标
int Customized_Brush[3][3] = {0}; // 自定义画笔9像素模板,初始化为0
int Brush_Type = 0; // 画笔类型,0=单像素画笔,1=相加画笔
int canvas[N_ROWS][N_COLS]; // 主画布数组,20行×36列
// 初始化画布为白色背景
setBlankCanvas(canvas);
// 显示当前画笔信息
Display_Brush(Brush_Type, brush_color, Customized_Brush);
// 显示初始画布内容
printCanvas(canvas);
// 显示帮助信息
Show_help_info();
// 主命令循环:等待并处理用户输入的命令
while (scanf("%d", &Command) == 1) {
// 命令0:显示帮助信息
if (Command == 0) {
// 待实现:调用Show_help_info()或直接显示帮助
}
// 命令1:画直线
if (Command == 1) {
// 待实现:获取坐标并调用Draw_Line()
}
// 命令2:矩形区域填充
if (Command == 2) {
// 待实现:获取坐标并调用Fill_Rectangle()
}
// 命令3:改变画笔灰度等级
if (Command == 3) {
// 待实现:获取新灰度等级并更新brush_color
}
// 命令4:矩形区域的复制和粘贴
if (Command == 4) {
// 待实现:获取源区域和目标位置并调用Copy_Paste()
}
// 命令5:定义"相加画笔"
if (Command == 5) {
// 待实现:获取9个像素值并调用Additive_Shape()
}
// 命令6:复位画布
if (Command == 6) {
// 待实现:重置画布、画笔等所有状态
}
// 命令7:只显示画布内容(过滤白色背景)
if (Command == 7) {
// 待实现:调用Display_Canvas()
}
// 命令8:显示当前画笔信息
if (Command == 8) {
// 待实现:调用Display_Brush()
}
// 命令9:显示完整画布(包含白色背景)
if (Command == 9) {
// 待实现:调用printCanvas()
}
// 非法命令处理
printf("Invalid Command Code!\n");
printf("The Command Code must be [0-9]!\n");
continue;
}
return 0; // 程序正常退出
}
// ----------------------------------------------------
// 第1阶段:绘制直线和对角线Draw_Line()和矩形填充Fill_Rectangle()
// ----------------------------------------------------
int Draw_Line(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]) {
return 0; // 待实现
}
void Horizontal_Print(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]) {
// 待实现
}
void Vertical_Print(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]) {
// 待实现
}
void Diagonal_Print(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]) {
// 待实现
}
int Diagonal_Check(int start_end_coordination[4]) {
return 0; // 待实现
}
int Fill_Rectangle(int start_end_coordination[4],
int canvas[N_ROWS][N_COLS],
int Brush_Type,
int brush_color,
int Customized_Brush[3][3]) {
return 0; // 待实现
}
// ----------------------------------------------------
// 第3阶段:矩形区域的复制和拷贝操作Copy_Paste()
// ----------------------------------------------------
int Copy_Paste(int start_end_coordination[4],
int target_row,
int target_col,
int canvas[N_ROWS][N_COLS]) {
return 0; // 待实现
}
// ----------------------------------------------------
// 画布显示功能实现
// ----------------------------------------------------
void printCanvas(int canvas[N_ROWS][N_COLS]) {
// 待实现
}
void setBlankCanvas(int canvas[N_ROWS][N_COLS]) {
// 待实现
}
void Display_Canvas(int canvas[N_ROWS][N_COLS]) {
// 待实现
}
// ----------------------------------------------------
// 第4阶段:定义画刷Additive_Shape()
// ----------------------------------------------------
void Additive_Shape(int UL, int above, int UR,
int left, int center, int right,
int DL, int below, int DR,
int Customized_Brush[3][3]) {
// 待实现
}
void Brush_Paint(int Center_Row,
int Center_Col,
int canvas[N_ROWS][N_COLS],
int Customized_Brush[3][3]) {
// 待实现
}
void Display_Brush(int Brush_Type,
int brush_color,
int Customized_Brush[3][3]) {
// 待实现
}
// ----------------------------------------------------
// 其他辅助函数实现
// ----------------------------------------------------
int Check_Drawing_Area(int start_end_coordination[4]) {
return 0; // 待实现
}
void Coordination_Normalize(int start_end_coordination[4]) {
// 待实现
}
void Show_help_info() {
// 待实现
}