easyx图形库网站:https://easyx.cn/
一.窗体创建~万物伊始
int main() {
basicsetting();
getchar();//阻塞程序,防止窗口一闪而过
closegraph();//关闭绘图窗口
return 0;
}
void basicsetting() {
initgraph(800, 600);//初始化绘图窗口,宽/高
setaspectratio(1, -1);//x轴,y轴缩放因子;翻转y轴,使其向上
setorigin(400, 300);//将逻辑坐标的原点设置为物理坐标的(400,300)
setbkcolor(RGB(116, 225, 202));//设置背景色
cleardevice();//清空窗口为背景色
}
设置线条与图案的样式,才能绘制满意的图形!
void mystyle() {
setbkcolor(WHITE);//设置背景色
cleardevice();//清空窗体,使窗体全部变成背景色
setlinecolor(RED);//设置描边颜色
setlinestyle(PS_DASH, 4);//设置描边样式'线型/宽度
circle(0, 0, 200);
setfillcolor(BLUE);//填充的颜色
solidcircle(0, 0, 150);//绘制填充图形
setlinecolor(GREEN);
setlinestyle(PS_DASH, 10);
setfillcolor(RED);
fillcircle(0, 0, 100);//绘制描边加填充图形
}
三.来绘制基本的图形吧!
void myBasicGraphical() {
circle(0, 0, 300);//绘制圆,x,y坐标/半径
putpixel(0, 0, RED);//绘制点,x,y坐标/颜色
line(-200, -200, 200, 200);//绘制直线,起始点/终点
line(-200, 200, 200, -200);
int x, y;
for (int i = 1;i <= 1000;i++) {
x = rand() % (800 + 1) - 400;//rand()返回一个不为负数的随机值
y = rand() % (600 + 1) - 300;//运算后x取值为(-400,400),y(-300,300)
putpixel(x, y, BLUE);
}
rectangle(-200, 100, 200, -100);//绘制矩形,左上角坐标/右下角坐标
ellipse(-200, 100, 200, -100);//绘制椭圆;外切矩形坐标
roundrect(-200, 100, 200, -100, 200, 100);//绘制圆角矩形;矩形参数/圆角处椭圆的长短轴
pie(-200, 100, 200, -100, 0, PI / 4);//绘制扇形;椭圆参数/起始角度,终止角度
arc(-200, 100, 200, -100, 0, PI / 4);//绘制圆弧,与扇形参数一致
}
四.进阶!绘制复杂多边形
void mypolygon() {
polygon(points_1, 4);//绘制"封闭"多边形(首尾相连);存储顶点的数组指针/元素个数
polyline(points_1, 4);//绘制不封闭图形,首尾不相连
POINT points_1[] = { {200,150},{200,-150},{-200,-150},{-200,150} };//存储顶点,POINT结构体含有x与y坐标两个变量
POINT points_2[5];
int r = 200;
for (int i = 0;i <= 4;i++) {//绘制正五边形
points_2[i].x = cos(PI / 2 + 2 * i * PI / 5) * r;
points_2[i].y = sin(PI / 2 + 2 * i * PI / 5) * r;
}
polygon(points_2, 5);
五.应用图形来绘制可爱的形象吧~
void Adream() {//绘制哆啦A梦头像
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 10);
setfillcolor(BLUE);
fillellipse(118, 125, 990, 931);
setfillcolor(WHITE);
fillellipse(189, 271, 919, 931);
fillellipse(375, 170, 555, 420);
fillellipse(555, 170, 735, 420);
setfillcolor(BLACK);
solidcircle(484, 333, 25);
solidcircle(617, 333, 25);
setfillcolor(WHITE);
solidcircle(484, 333, 10);
solidcircle(617, 333, 10);
setfillcolor(RED);
fillcircle(554, 420, 35);
line(554, 460, 554, 828);
arc(320, 510, 789, 827, PI, 2 * PI);
line(125, 313, 296, 410);
line(83, 444, 270, 474);
line(83, 595, 262, 527);
line(819, 414, 990, 320);
line(845, 478, 4029, 448);
line(853, 542, 1029, 660);
}
void monitor() {//绘制一台显示器
setfillcolor(RGB(232, 235, 240));
solidroundrect(100, 50, 700, 450, 20, 20);
setfillcolor(RGB(71, 78, 94));
solidrectangle(100, 390, 700, 410);
solidroundrect(100, 50, 700, 410, 20, 20);
setfillcolor(RGB(115, 199, 235));
solidrectangle(120, 70, 680, 390);
setfillcolor(RGB(232, 235, 240));
solidcircle(400, 60, 5);
setfillcolor(RGB(71, 78, 94));
solidcircle(400, 430, 12);
setfillcolor(RGB(218, 219, 224));
solidellipse(275, 515, 525, 545);
setfillcolor(RGB(232, 235, 240));
POINT bigTrapezoid[4] = { {345,450},{455,450},{475,530},{325,530} };
solidpolygon(bigTrapezoid, 4);
}
六.变得更好看!运用RGB与HSV色彩知识绘制彩虹
void SkyAndRainbow() {//绘制天空与彩虹
float s = 0.76;
float ds = s / 600.0;
for (int y = 0;y < 600;y++) {//天蓝渐变背景色
setlinecolor(HSVtoRGB(216, s, 0.95));
line(0, y, 800, y);
s -= ds;
}
float h = 0;
float dH = 360.0 / 100.0;
for (int r = 300;r >= 200;r--) {
setlinecolor(HSVtoRGB(h, 1, 1));
circle(400, 600, r);
h += dH;
}
七.让图形动起来吧——五角星的平动与转动
void FivePointedStart(int x,int y,int r, double startAngle) {//绘制五角星
POINT points[5];
double delta = 4 * PI / 5;
for (int i = 0;i < 5;i++) {
points[i].x = cos(startAngle + i * delta) * r+x;
points[i].y = sin(startAngle + i * delta) * r+y;
}
solidpolygon(points, 5);
}
setfillcolor(WHITE);
setpolyfillmode(WINDING);//指定填充模式为WINDING,这样五角星内部才会被填充满
int x, y = 0;
double theta = 0;
double dtheta = 0.05;
double startAngle = PI / 2;
double dstartAngle = 0.05;
while(1){
cleardevice();
x = cos(theta) * 200;
y = sin(theta) * 200;
FivePointedStart(x, y, 50, startAngle);
startAngle += dstartAngle;
theta += dtheta;
Sleep(40);//让程序休眠40毫秒,即一秒钟25帧
}
八.这是什么?直线运动!
void LinearMotion(int x1, int y1, int x2, int y2, int v) {//直线运动
setfillcolor(WHITE);
double tanTheta = (double)abs(y2 - y1) / (double)abs(x2 - x1);
double theta = atan(tanTheta);
double vxflag = 0, vyflag = 0;
if (x2 - x1 > 0) vxflag = 1;
else vxflag = -1;
if (y2 - y1 > 0) vyflag = 1;
else vyflag = -1;
if (vxflag == 0 && vyflag == 0) return;
double dx = vxflag * v * cos(theta);
double dy = vyflag * v * sin(theta);
double x = x1;
double y = y1;
while (1) {
cleardevice();
solidcircle(x, y, 15);
Sleep(40);
x += dx;
y += dy;
if (vxflag == 1) {
if (x >= x2) break;
}
if (vxflag == -1) {
if (x <= x2) break;
}
if (vyflag == 1) {
if (y >= y2) break;
}
if (vyflag == 1) {
if (y >= y2) break;
}
}
}
LinearMotion(-200, -200, 0, 200, 15);
LinearMotion(0, 200, 200, -200, 15);
LinearMotion(200, -200, -200, -200,15);
九.更多样!图形剪切与组合
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 6);
int r = 150;
HRGN rgn = CreateEllipticRgn(250, 150, 550, 450);//创建圆形(本质是椭圆)区域,保存在为HRGN类型的变量中
setcliprgn(rgn);//将rgn区域设置为剪切区域,除了剪切区域外的图形都无法绘制。注意:必须先设置剪切区域再绘制图形
for (int i = 0;i < 6;i++) {
int x, y;
x = cos(i * PI / 3) * r;
y = sin(i * PI / 3) * r;
circle(x, y, r);
}
setcliprgn(NULL);//将剪切区域设置为空,其后的绘图才不会受到影响
DeleteObject(rgn);//销毁之前创立的圆形区域
/*
HRGN rgn1 = CreateEllipticRgn(250, 250, 550, 550);
HRGN rgn2 = CreateEllipticRgn(250, 100, 550, 400);
HRGN rgn = CreateRectRgn(0, 0, 0, 0);//创建一个空区域作为目标区域
CombineRgn(rgn, rgn1, rgn2, RGN_AND);//取区域rgn1与rgn2的交集部分置于目标区域rgn中
setcliprgn(rgn);//剪切区域,只保留了交集部分
setfillcolor(BLUE);
solidcircle(400, 400, 150);
solidcircle(400, 250, 150);
setcliprgn(NULL);
DeleteObject(rgn1);
DeleteObject(rgn2);
DeleteObject(rgn);
*/
十.键盘互动!区别游戏与动画的根本!
int x=-400, y = 0;
int dx = 15, dy = 0;
while (1) {
cleardevice();
solidcircle(x, y, 50);
Sleep(50);
cleardevice();
solidcircle(x, y, 50);
if (_kbhit() != 0) {//查看输入缓存区中是否有数据,有则返回1,没有则返回0
char c = _getch();//阻塞函数,但区别于getchar的是读取到输入缓存区中的一个字符则立即执行,不需要按下回车
switch (c) {
case 'w':
dx = 0, dy = 15;
break;
case 's':
dx = 0, dy = -15;
break;
case 'a':
dx = -15, dy = 0;
break;
case 'd':
dx = 15, dy = 0;
break;
}
}
x += dx;
y += dy;
}