图形库篇 | EasyX | 图形绘制

本文介绍了EasyX图形库在C语言中的使用,包括设置颜色、绘制直线、圆和矩形的方法,详细讲解了各个函数的功能及参数,提供了代码示例,帮助读者掌握图形绘制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图形库篇 | EasyX | 图形绘制


设置颜色

函数功能函数
设置画线颜色void setlinecolor(COLORREF color)
设置填充颜色void setfillcolor(COLORREF color)

设置画线颜色:void setlinecolor(COLORREF color)

  • 具体功能:设置当前设备画线颜色
  • 返回值:空
  • 参数:
    • color:设置的画线颜色

设置填充颜色:void setfillcolor(COLORREF color)

  • 具体功能:设置当前设备填充颜色
  • 返回值:空
  • 参数:
    • color:设置的填充颜色

为什么先讲解设置颜色相关的函数,因为在绘制任何图形之前,都应该先设置颜色,然后再进行绘制,否则绘制出来的图形颜色为默认的白色。

  • 针对无填充图形:在绘制前使用 setlinecolor 函数设置画线颜色
  • 针对填充图形:在绘制前使用 setlinecolor 函数和 setfillcolor 函数设置画线颜色和填充颜色(设置画线颜色的原因是填充图形是有边框的)

绘制直线

函数功能函数
绘制直线void line(int x1,int y1,int x2,int y2)

绘制直线:void line(int x1,int y1,int x2,int y2)

  • 具体功能:绘制直线
  • 返回值:空
  • 参数:
    • x1:起始点x坐标
    • y1:起始点y坐标
    • x2:终止点x坐标
    • y2:终止点y坐标

代码示例:绘制横线

#include <easyx.h> // 引用图形库头文件
#include <conio.h>

int main()
{
	initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

	setbkcolor(RGB(255, 255, 255)); // 设置背景色:白色
	cleardevice(); // 刷新屏幕

	// 绘制横线:起始点和终止点的y坐标一致
	setlinecolor(RGB(71, 143, 221)); // 设置画线颜色:蓝色
	line(70, 200, 570, 200); // 绘制一条直线,长:570-70=500

	_getch(); // 按任意键继续
	closegraph(); // 关闭绘图窗口
	return 0;
}

请添加图片描述

代码示例:绘制竖线

#include <easyx.h> // 引用图形库头文件
#include <conio.h>

int main()
{
	initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

	setbkcolor(RGB(255, 255, 255)); // 设置背景色:白色
	cleardevice(); // 刷新屏幕

	// 绘制竖线:起始点和终止点的x坐标一致
	setlinecolor(RGB(71, 143, 221)); // 设置画线颜色:蓝色
	line(300, 40, 300, 440); // 绘制一条直线,长:440-40=400

	_getch(); // 按任意键继续
	closegraph(); // 关闭绘图窗口
	return 0;
}

请添加图片描述


绘制圆

函数功能函数
绘制无填充的圆void circle(int x,int y,int radius)
绘制填充圆void fillcircle(int x,int y,int radius)

绘制无填充的圆:void circle(int x,int y,int radius)

  • 具体功能:绘制无填充的圆
  • 返回值:空
  • 参数:
    • x:圆心x坐标
    • y:圆心y坐标
    • radius:圆的半径

绘制填充圆:void fillcircle(int x,int y,int radius)

  • 具体功能:绘制有边框的填充圆
  • 返回值:空
  • 参数:
    • x:圆心x坐标
    • y:圆心y坐标
    • radius:圆的半径

代码示例:绘制无填充的圆

#include <easyx.h> // 引用图形库头文件
#include <conio.h>

int main()
{
	initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

	setbkcolor(RGB(255, 255, 255)); // 设置背景色:白色
	cleardevice(); // 刷新屏幕

	setlinecolor(RGB(71, 143, 221)); // 设置画线颜色:蓝色
	circle(320, 240, 100); // 圆心(320,240),半径:100

	_getch(); // 按任意键继续
	closegraph(); // 关闭绘图窗口
	return 0;
}

请添加图片描述

代码示例:绘制填充圆

#include <easyx.h> // 引用图形库头文件
#include <conio.h>

int main()
{
	initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

	setbkcolor(RGB(255, 255, 255)); // 设置背景色:白色
	cleardevice(); // 刷新屏幕

	setlinecolor(RGB(71, 143, 221)); // 设置画线颜色:蓝色
	setfillcolor(RGB(71, 143, 221)); // 设置填充颜色:蓝色
	fillcircle(320, 240, 100); // 圆心(320,240),半径:100

	_getch(); // 按任意键继续
	closegraph(); // 关闭绘图窗口
	return 0;
}

请添加图片描述


绘制矩形

函数功能函数
绘制无填充的矩形void rectangle(int left,int top,int right,int bottom)
绘制填充矩形void fillrectangle(int left,int top,int right,int bottom)
绘制无填充的圆角矩形void roundrect(int left,int top,int right,int bottom,int ellipsewidth,int ellipseheight)
绘制填充圆角矩形void fillroundrect(int left,int top,int right,int bottom,int ellipsewidth,int ellipseheight)

绘制无填充的矩形:void rectangle(int left,int top,int right,int bottom)

  • 具体功能:绘制无填充的矩形
  • 返回值:空
  • 参数:
    • left:矩形左部x坐标
    • top:矩形顶部y坐标
    • right:矩形右部x坐标
    • bottom:矩形底部y坐标

绘制填充矩形:void fillrectangle(int left,int top,int right,int bottom)

  • 具体功能:绘制有边框的填充矩形
  • 返回值:空
  • 参数:
    • left:矩形左部x坐标
    • top:矩形顶部y坐标
    • right:矩形右部x坐标
    • bottom:矩形底部y坐标

绘制无填充的圆角矩形:void roundrect(int left,int top,int right,int bottom,int ellipsewidth,int ellipseheight)

  • 具体功能:绘制无填充的圆角矩形
  • 返回值:空
  • 参数:
    • left:矩形左部x坐标
    • top:矩形顶部y坐标
    • right:矩形右部x坐标
    • bottom:矩形底部y坐标
    • ellipsewidth:构成圆角矩形的圆角的椭圆的宽度
    • ellipseheight:构成圆角矩形的圆角的椭圆的高度

这里着重说明后两个参数,它们分别表示圆角的椭圆的高和宽。怎么理解呢,其实就是相当于指定一个椭圆的宽和高,然后把它按照轴对称切成四份,将每一份放在矩形对应的四个角,这样就构成了圆角矩形。

需要注意的是,通常我们想要的圆角并非椭圆而是圆,因此会将ellipsewidth和ellipseheight设为同一个值。

如果将一个边长为w的正方形的圆角设为w/2,那么它就会成为一个半径为w/2的圆。

绘制填充圆角矩形:void fillroundrect(int left,int top,int right,int bottom,int ellipsewidth,int ellipseheight)

  • 具体功能:绘制有边框的填充圆角矩形
  • 返回值:空
  • 参数:
    • left:矩形左部x坐标
    • top:矩形顶部y坐标
    • right:矩形右部x坐标
    • bottom:矩形底部y坐标
    • ellipsewidth:构成圆角矩形的圆角的椭圆的宽度
    • ellipseheight:构成圆角矩形的圆角的椭圆的高度

代码示例:绘制无填充的矩形

#include <easyx.h> // 引用图形库头文件
#include <conio.h>

int main()
{
	initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

	setbkcolor(RGB(255, 255, 255)); // 设置背景色
	cleardevice(); // 刷新屏幕

	setlinecolor(RGB(71, 143, 221)); // 设置画线颜色:蓝色
	setfillcolor(RGB(71, 143, 221)); // 设置填充颜色:蓝色
	rectangle(100, 100, 540, 380); // 宽:540-100=440 高:380-100=280 

	_getch(); // 按任意键继续
	closegraph(); // 关闭绘图窗口
	return 0;
}

请添加图片描述
代码示例:绘制填充矩形

#include <easyx.h> // 引用图形库头文件
#include <conio.h>

int main()
{
	initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

	setbkcolor(RGB(255, 255, 255)); // 设置背景色
	cleardevice(); // 刷新屏幕

	setlinecolor(RGB(71, 143, 221)); // 设置画线颜色:蓝色
	setfillcolor(RGB(71, 143, 221)); // 设置填充颜色:蓝色
	fillrectangle(100, 100, 540, 380); // 宽:540-100=440 高:380-100=280 

	_getch(); // 按任意键继续
	closegraph(); // 关闭绘图窗口
	return 0;
}

请添加图片描述
代码示例:绘制无填充的圆角矩形

#include <easyx.h> // 引用图形库头文件
#include <conio.h>

int main()
{
	initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

	setbkcolor(RGB(255, 255, 255)); // 设置背景色
	cleardevice(); // 刷新屏幕

	setlinecolor(RGB(71, 143, 221)); // 设置画线颜色:蓝色
	setfillcolor(RGB(71, 143, 221)); // 设置填充颜色:蓝色
	roundrect(100, 100, 540, 380, 8, 8); // 宽:540-100=440 高:380-100=280 圆角半径:8

	_getch(); // 按任意键继续
	closegraph(); // 关闭绘图窗口
	return 0;
}

请添加图片描述

代码示例:绘制填充圆角矩形

#include <easyx.h> // 引用图形库头文件
#include <conio.h>

int main()
{
	initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

	setbkcolor(RGB(255, 255, 255)); // 设置背景色
	cleardevice(); // 刷新屏幕

	setlinecolor(RGB(71, 143, 221)); // 设置画线颜色:蓝色
	setfillcolor(RGB(71, 143, 221)); // 设置填充颜色:蓝色
	fillroundrect(100, 100, 540, 380, 8, 8); // 宽:540-100=440 高:380-100=280 圆角半径:8

	_getch(); // 按任意键继续
	closegraph(); // 关闭绘图窗口
	return 0;
}

请添加图片描述


作者:百栗.
微信:baili0299

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百栗.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值