为c语言安装easyx图形库

按照图上的步骤,安装easyx图形库。

 接下来看代码:

#include<easyx.h>
#include<stdio.h>
#define width 800
#define height 600

int main()
{
	initgraph(width, height);       // 初始化窗口(宽度, 高度) (单位像素)
	setorigin(width/2, height/2);   // 设置逻辑原点为窗口中心
	setaspectratio(1, -1);          //  设置xy坐标轴(x:1,y:-1)代表y翻转, x不动
	circle(0, 100, 100);            // 画一个圆(x:0,y:100的位置,半径为100)
	getchar();                      //阻断一下程序,防止窗口一闪而过。
	closegraph();                   // 关闭窗口
	return 0;
}

物理坐标在窗口的左上角是0,0的位置,但是我们使用的是逻辑坐标,所以要设置逻辑坐标原点的位置:setorigin(width/2, height/2);   // 设置逻辑原点为窗口中心

之后的x方向是从左向右的,y的方向是从上到下的,不符合我们上学是的看图策略,y方向要从上到下,所以要设置y轴方向:setaspectratio(1, -1);          //  设置xy坐标轴(x:1,y:-1)代表y翻转, x不动。

接下来就是画圆了:circle(0, 0, 100);            // 画一个圆(x:0,y:0的位置,半径为100)如下图所示:

 

下面是画4个点:

#include<easyx.h>
#include<stdio.h>
#define width 800
#define height 600

int main()
{
	initgraph(width, height);            // 初始化窗口(宽度, 高度) (单位像素)
	setorigin(width / 2, height / 2);    // 设置逻辑原点为窗口中心
	setaspectratio(1, -1);               //  设置xy坐标轴(x:1,y:-1)代表y翻转, x不动
	putpixel(100,100, RED);              //画一个点(x:100, y:100, 红色)
	putpixel(100, -100, YELLOW);         //画一个点(x:100, y:-100, 黄色)
	putpixel(-100, -100, GREEN);         //画一个点(x:-100, y:-100, 绿色)
	putpixel(-100, 100, BLUE);           //画一个点(x:-100, y:100, 蓝色)
	getchar();
	closegraph();
	return 0;
}

根据上面的学习,接下来绘制1000个随机的点:

#include<easyx.h>
#include<stdio.h>
#define width 1200      // 窗口宽度
#define height 960      // 窗口高度
#include<time.h>

int main()
{
	srand((unsigned int)time(NULL));
	initgraph(width, height);            // 初始化窗口(宽度, 高度) (单位像素)
	setorigin(width / 2, height / 2);    // 设置逻辑原点为窗口中心
	setaspectratio(1, -1);               //  设置xy坐标轴(x:1,y:-1)代表y翻转, x不动
	int i = 0;
	for (i = 0; i < 1000; i++)      // 绘制1000个点,位置随机
	{
		int x = rand() % (width + 1) - width / 2;
		int y = rand() % (height + 1) - height / 2;
		putpixel(x, y, GREEN);              //画一个点(x:100, y:100, 红色)
	}
		
	putpixel(100, -100, YELLOW);         //画一个点(x:100, y:-100, 黄色)
	putpixel(-100, -100, GREEN);         //画一个点(x:-100, y:-100, 绿色)
	putpixel(-100, 100, BLUE);           //画一个点(x:-100, y:100, 蓝色)
	getchar();
	closegraph();
	return 0;
}

 line(起点x,起点y,终点x,终点y):

画线:

#include<easyx.h>
#include<stdio.h>
#define W 900    //  窗口宽度
#define H 600    //  窗口高度

int main()
{
	initgraph(W, H);
	setaspectratio(1,-1);
	setorigin(W / 2, H / 2);
	line(-200, 200, 200, -200);   // 从坐标-200, 200  到坐标200, -200画一条线
	getchar();
	closegraph();
	return 0;
}

 接下来用画圆函数,画一个靶心:

#include<easyx.h>
#include<stdio.h>
#define W 900
#define H 700

int main()
{
	initgraph(W, H);
	setorigin(W/2,H/2);
	setaspectratio(1, -1);
	int i = 0;
	for (i = 10; i <= 300; i += 10)
	{
		circle(0, 0, i);
	}
	getchar();
	closegraph();
	return 0;
}

 接下来学习绘制矩形函数:

rectangle(左上角x坐标,左上角y坐标,右下角x坐标,右下角y坐标)

#include<easyx.h>
#include<stdio.h>
#define W 800
#define H 600

int main()
{
	initgraph(W, H);
	setorigin(W / 2, H / 2);
	setaspectratio(1, -1);

	rectangle(-200, 200, 200, -200);

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

 下面学习绘制椭圆函数:

ellipse(左上角x坐标,左上角y坐标,右下角x坐标,右下角y坐标)

#include<easyx.h>
#include<stdio.h>
#define W 800
#define H 600

int main()
{
	initgraph(W, H);
	setorigin(W / 2, H / 2);
	setaspectratio(1, -1);
	
	ellipse(-200, 100, 200, -100);

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

 下面学习绘制圆角矩形:

 圆角矩形的四个圆角实际上是由4个椭圆构成的:

#include<easyx.h>
#include<stdio.h>
#define W 800
#define H 600

int main()
{
	initgraph(W, H);
	setorigin(W / 2, H / 2);
	setaspectratio(1, -1);

	roundrect(-200, 100, 200, -100, 200, 100);

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

 

 绘制扇形函数:

 

#include<easyx.h>
#include<stdio.h>
#define W 800
#define H 600
#define PI 3.14

int main()
{
	initgraph(W, H);
	setorigin(W / 2, H / 2);
	setaspectratio(1, -1);

	pie(-200, 100, 200, -100, 0, PI/4);

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

绘制圆弧函数:

#include<easyx.h>
#include<stdio.h>
#define W 800
#define H 600
#define PI 3.14

int main()
{
	initgraph(W, H);
	setorigin(W / 2, H / 2);
	setaspectratio(1, -1);

	arc(-200, 100, 200, -100, 0, PI / 4);

	getchar();
	closegraph();

	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/xingyuncao520025/article/details/132225740