EasyX基础内容(和易错的地方)

该文详细介绍了如何使用EasyX库进行图形绘制,包括初始化窗口、改变背景颜色、画点、画线、画圆、画矩形等基本操作,以及线条样式、填充颜色和图形边框等进阶技巧。每个知识点都配有代码示例,强调了在实际编程中的注意事项和常见问题。
摘要由CSDN通过智能技术生成

注意代码里面的文字,是易错点

安装 EasyX 绘图库: https://easyx.cn

下载好后,头文件得加上 #include<easyx.h> 

代码中一定要加上getchar(),否则图片会一闪而过

🚥🚥🚥🚥🚥🚥

⭐1.用initgraph建立的窗口默认是黑色的

cleardevice()可以改变颜色,用setbkcolor()表示自己想要什么颜色

一定要注意cleardevice()和setbkcolor()的先后顺序

#include<easyx.h>
#include<stdio.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));//先写setbkcolor()
	cleardevice();                 //后写cleardevice()
	getchar();
	closegraph();
	return 0;
}

结果

 ⭐2.画点

putpixel(x, y, WHITE);//x,y是坐标,第三个是颜色

#include<easyx.h>
#include<stdio.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	int x, y;
	for (int i = 0; i <= 400; i ++ )//随机画点
	{
		x = rand() % (400 + 1) - 200;
		y = rand() % (400 + 1) - 200;
		putpixel(x, y, WHITE);
	}
	getchar();
	closegraph();
	return 0;
}

结果

⭐3.反转坐标   (Code的y坐标与数学里面的是相反的)

setaspectratio(x,y)

使用setaspectratio(1,-1);可以翻转y轴

⭐4.画线

line(x1,y1,x2,y2);//(x1,y1):起始坐标     (x2,y2):结尾坐标

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	line(10, 10, 300, 300);
	getchar();
	closegraph();
	return 0;
}

 

⭐5.画圆

circle(x,y,r);//(x,y)是圆心坐标 ,r是半径

默认圆心在(0,0)处,但是可以用setorigin()来改变圆心位置

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();
	setorigin(200, 200);
	circle(0, 0, 50);//已经设置了圆心在(200,200)那么circle里面用0,0即可
	getchar();
	closegraph();
	return 0;
}

如果想画多个同心圆 ,使用for循环即可

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();
	setorigin(200, 200);
	for (int r = 0; r <= 300; r += 50)
	{
		circle(0, 0, r);//其实不断扩大半径即可
	}
	getchar();
	closegraph();
	return 0;
}

 

⭐6小插曲——为什么会出现坐标是负数

是因为使用了setaspectratio(1,-1);翻转了y轴setorigin()改变了(0,0)的位置

 ⭐7.画矩形

rectangle(x1,y1,x2,y2);//(x1,y1)矩形左上角坐标  (x2,y2)矩形右下角坐标

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	setaspectratio(1, -1);
	setorigin(200, 200);
	cleardevice();
	rectangle(-50, 50, -150, -150);
	getchar();
	closegraph();
	return 0;
}

⭐8.画椭圆

ellipse(x1,y1,x2,y2);//(x1,y1)是椭圆外接矩形左上角坐标(x2,y2)是椭圆外接矩形右下角坐标

相当于在椭圆外套了个矩形 

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	setaspectratio(1, -1);
	setorigin(200, 200);
	cleardevice();
	ellipse(-50, 50, -150, -150);
	getchar();
	closegraph();
	return 0;
}

 

⭐9.画 圆角矩形——顾名思义,就是一个矩形,只是尖角处比较光滑

roundrect(x1,y1,x2,y2,w,h);//x,y与矩形的定义一样,

w,h看图片理解

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	setaspectratio(1, -1);
	setorigin(200, 200);
	cleardevice();
	roundrect(-50, 50, -150, -150,50,50);
	getchar();
	closegraph();
	return 0;
}

⭐10.画 扇形

pie(x1,y1,x2,y2,stangle,endangle);

//x,y看图片, stangle:扇形起始角的弧度,endangle:扇形终止角的弧度

#define PI 3.14 //把3.14当作360°

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
#define PI 3.14
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	setaspectratio(1, -1);
	setorigin(200, 200);
	cleardevice();
	pie(-50, 50, -150, -150, 0, PI / 1);
	getchar();
	closegraph();
	return 0;
}

⭐11.画 圆弧

arc(x1,y1,x2,y2,stangle,enangle);//定义与扇形一样,不同的是圆弧就是一个弧

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
#define PI 3.14
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	setaspectratio(1, -1);
	setorigin(200, 200);
	cleardevice();
	arc(-50, 50, -150, -150, 0, PI / 1);
	getchar();
	closegraph();
	return 0;
}

 ⭐12.把所有的点都连起来

ploygon用法(比如画三角形)

POINT points[]= { {0,100},{100,-100},{-100,-100} };//POINT结构数组,并传入三个点的坐标

polygon(points, 3);有多少个坐标,后面的数字就是多少

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	setaspectratio(1, -1);
	setorigin(200, 200);
	cleardevice();
	POINT points[] = { {0,100},{100,-100},{-100,-100} };
	polygon(points, 3);
	getchar();
	closegraph();
	return 0;
}

⭐13.不 把所有点都连起来 

ployline();//不会连接首尾两个点,其他的与polygn一样

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>
#include<math.h>
#define PI 3.14
int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();
	setaspectratio(1, -1);
	setorigin(200, 200);
	POINT points[3] = { {100,100},{0,0},{-100,100} };
	polyline(points, 3);
	getchar();
	closegraph();
	return 0;
}

 

⭐14. 设置图形边缘的颜色

setlinecolor();

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>

int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();
	setaspectratio(1, -1);
	setorigin(200, 200);

	setlinecolor(YELLOW);//先写setlinecolor
	circle(0, 0, 100);

	getchar();
	closegraph();
	return 0;
}

⭐15.填充样式+填充的颜色

样式  在前面加上solid     

circle-----soliodcircle

rectangle------solidrectangle 

颜色  使用setfillcolor

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>

int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();
	setaspectratio(1, -1);
	setorigin(200, 200);

	setfillcolor(YELLOW);//先写setfillcolor
	solidcircle(0, 0, 100);
	
	getchar();
	closegraph();
	return 0;
}

✨16.注意,填充颜色不包括描边

 如果实现 填充颜色+描边   加上fill

circle-----fillcircle

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>

int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();
	setaspectratio(1, -1);
	setorigin(200, 200);

	setfillcolor(YELLOW);
	setlinecolor(RED);
	fillcircle(0,0,100);
	
	getchar();
	closegraph();
	return 0;
}

 如果写成circle而不是fillcircle

⭐ 17.设置描边的样式

setlinestyle(a,num);//a是样式  num是描边的粗细

 

#include<stdio.h>
#include<easyx.h>
#include<graphics.h>

int main()
{
	initgraph(400, 400);
	setbkcolor(RGB(164, 225, 202));
	cleardevice();
	setaspectratio(1, -1);
	setorigin(200, 200);

	setlinestyle(PS_ENDCAP_ROUND, 40);//圆形 round       num=40
	line(-100, 100, 100, 100);

	setlinestyle(PS_ENDCAP_SQUARE, 40);//方形 square
	line(-100, 50, 100, 50);

	setlinestyle(PS_ENDCAP_FLAT, 40);//平坦 flat
	line(-100, 0, 100, 0);

	getchar();
	closegraph();
	return 0;
}

 

 

/****************************************************** * EasyX Library for C++ (Ver:20151015(beta)) * http://www.easyx.cn * * graphics.h * 基于 EasyX.h,实现在 VC 下实现简单绘图。 * 同时,为了兼容 Turbo C/C++ 和 Borland C/C++ 等一系 * 列开发环境中的 BGI 绘图库函数,做了相应扩展。 ******************************************************/ #pragma once #include <easyx.h> // 兼容 initgraph 绘图模式的宏定义(无实际意义) #define DETECT 0 #define VGA 0 #define VGALO 0 #define VGAMED 0 #define VGAHI 0 #define CGA 0 #define CGAC0 0 #define CGAC1 0 #define CGAC2 0 #define CGAC3 0 #define CGAHI 0 #define EGA 0 #define EGALO 0 #define EGAHI 0 // BGI 格式的初始化图形设备,默认 640 x 480 HWND initgraph(int* gdriver, int* gmode, char* path); void bar(int left, int top, int right, int bottom); // 画无边框填充矩形 void bar3d(int left, int top, int right, int bottom, int depth, bool topflag); // 画有边框三维填充矩形 void drawpoly(int numpoints, const int *polypoints); // 画多边形 void fillpoly(int numpoints, const int *polypoints); // 画填充的多边形 int getmaxx(); // 获取最大的宽度值 int getmaxy(); // 获取最大的高度值 COLORREF getcolor(); // 获取当前绘图前景色 void setcolor(COLORREF color); // 设置当前绘图前景色 void setwritemode(int mode); // 设置前景的二元光栅操作模式 /////////////////////////////////////////////////////////////////////////////////// // 以下函数仅为兼容早期 EasyX 的接口,不建议使用。请使用相关新函数替换。 // #if _MSC_VER > 1200 #define _EASYX_DEPRECATE(_NewFunc) __declspec(deprecated("This function is deprecated. Instead, use this new function: " #_NewFunc ". See http://www.easyx.cn/help?" #_NewFunc " for details.")) #define _EASYX_DEPRECATE_OVERLOAD(_Func) __declspec(deprecated("This overload is deprecated. See http://www.easyx.cn/help?" #_Func " for details.")) #else #define _EASYX_DEPRECATE(_NewFunc) #define _EASYX_DEPRECATE_OVERLOAD(_Func) #endif // 设置当前字体样式(该函数已不再使用,请使用 settextstyle 代替) // nHeight: 字符的平均高度; // nWidth: 字符的平均宽度(0 表示自适应); // lpszFace: 字体名称; // nEscapement: 字符串的书写角度(单位 0.1 度); // nOrientation: 每个字符的书写角度(单位 0.1 度); // nWeight: 字符的笔画粗细(0 表示默认粗细); // bItalic: 是否斜体; // bUnderline: 是否下划线; // bStrikeOut: 是否删除线; // fbCharSet: 指定字符集; // fbOutPrecision: 指定文字的输出精度; // fbClipPrecision: 指定文字的剪辑精度; // fbQuality: 指定文字的输出质量; // fbPitchAndFamily: 指定以常规方式描述字体的字体系列。 _EASYX_DEPRECATE(settextstyle) void setfont(int nHeight, int nWidth, LPCTSTR lpszFace); _EASYX_DEPRECATE(settextstyle) void setfont(int nHeight, int nWidth, LPCTSTR lpszFace, int nEscapement, int nOrientation, int nWeight, bool bItalic, bool bUnderline, bool bStrikeOut); _EASYX_DEPRECATE(settextstyle) void setfont(int nHeight, int nWidth, LPCTSTR lpszFace, int nEscapement, int nOrientation, int nWeight, bool bItalic, bool bUnderline, bool bStrikeOut, BYTE fbCharSet, BYTE fbOutPrecision, BYTE fbClipPrecision, BYTE fbQuality, BYTE fbPitchAndFamily); _EASYX_DEPRECATE(settextstyle) void setfont(const LOGFONT *font); // 设置当前字体样式 _EASYX_DEPRECATE(gettextstyle) void getfont(LOGFONT *font); // 获取当前字体样式 // 以下填充样式不再使用,新的填充样式请参见帮助文件 #define NULL_FILL BS_NULL #define EMPTY_FILL BS_NULL #define SOLID_FILL BS_SOLID // 普通一组 #define BDIAGONAL_FILL BS_HATCHED, HS_BDIAGONAL // /// 填充 (对应 BGI 的 LTSLASH_FILL) #define CROSS_FILL BS_HATCHED, HS_CROSS // +++ 填充 #define DIAGCROSS_FILL BS_HATCHED, HS_DIAGCROSS // xxx 填充 (heavy cross hatch fill) (对应 BGI 的 XHTACH_FILL) #define DOT_FILL (BYTE*)"\x80\x00\x08\x00\x80\x00\x08\x00" // xxx 填充 (对应 BGI 的 WIDE_DOT_FILL) #define FDIAGONAL_FILL BS_HATCHED, HS_FDIAGONAL // \\\ 填充 #define HORIZONTAL_FILL BS_HATCHED, HS_HORIZONTAL // === 填充 #define VERTICAL_FILL BS_HATCHED, HS_VERTICAL // ||| 填充 // 密集一组 #define BDIAGONAL2_FILL (BYTE*)"\x44\x88\x11\x22\x44\x88\x11\x22" #define CROSS2_FILL (BYTE*)"\xff\x11\x11\x11\xff\x11\x11\x11" // (对应 BGI 的 fill HATCH_FILL) #define DIAGCROSS2_FILL (BYTE*)"\x55\x88\x55\x22\x55\x88\x55\x22" #define DOT2_FILL (BYTE*)"\x88\x00\x22\x00\x88\x00\x22\x00" // (对应 BGI 的 CLOSE_DOT_FILL) #define FDIAGONAL2_FILL (BYTE*)"\x22\x11\x88\x44\x22\x11\x88\x44" #define HORIZONTAL2_FILL (BYTE*)"\x00\x00\xff\x00\x00\x00\xff\x00" #define VERTICAL2_FILL (BYTE*)"\x11\x11\x11\x11\x11\x11\x11\x11" // 粗线一组 #define BDIAGONAL3_FILL (BYTE*)"\xe0\xc1\x83\x07\x0e\x1c\x38\x70" // (对应 BGI 的 SLASH_FILL) #define CROSS3_FILL (BYTE*)"\x30\x30\x30\x30\x30\x30\xff\xff" #define DIAGCROSS3_FILL (BYTE*)"\xc7\x83\xc7\xee\x7c\x38\x7c\xee" #define DOT3_FILL (BYTE*)"\xc0\xc0\x0c\x0c\xc0\xc0\x0c\x0c" #define FDIAGONAL3_FILL (BYTE*)"\x07\x83\xc1\xe0\x70\x38\x1c\x0e" #define HORIZONTAL3_FILL (BYTE*)"\xff\xff\x00\x00\xff\xff\x00\x00" // (对应 BGI 的 LINE_FILL) #define VERTICAL3_FILL (BYTE*)"\x33\x33\x33\x33\x33\x33\x33\x33" // 其它 #define INTERLEAVE_FILL (BYTE*)"\xcc\x33\xcc\x33\xcc\x33\xcc\x33" // (对应 BGI 的 INTERLEAVE_FILL)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

在下小吉.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值