注意代码里面的文字,是易错点
安装 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;
}