Follow the steps in the figure to install the easyx graphics library.
Next look at the code:
#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;
}
The physical coordinates are at 0,0 in the upper left corner of the window, but we use logical coordinates, so we need to set the origin of the logical coordinates: setorigin(width/2, height/2); // Set the logical origin to the center of the window
The subsequent x direction is from left to right, and the y direction is from top to bottom, which does not conform to our school-viewing strategy. The y direction should be from top to bottom, so set the y-axis direction: setaspectratio(1, -1); // Set the xy coordinate axis (x: 1, y: -1) to represent y flipping and x not moving.
The next step is to draw a circle: circle(0, 0, 100); // Draw a circle (position x: 0, y: 0, radius 100) as shown in the figure below:
The following is to draw 4 points:
#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;
}
According to the above learning, next draw 1000 random points:
#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 (starting point x, starting point y, end point x, end point y):
draw line:
#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;
}
Next, use the circle function to draw a bullseye:
#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;
}
Next, learn to draw a rectangle function:
rectangle (x coordinate of upper left corner, y coordinate of upper left corner, x coordinate of lower right corner, y coordinate of lower right corner)
#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;
}
Let's learn to draw elliptic functions:
ellipse (x coordinate of the upper left corner, y coordinate of the upper left corner, x coordinate of the lower right corner, y coordinate of the lower right corner)
#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;
}
Let's learn to draw a rounded rectangle:
The four rounded corners of a rounded rectangle are actually composed of four ellipses:
#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;
}
Plot the sector function:
#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;
}
Draw the arc function:
#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;
}