一、First of All
上次写ege库的文章也还是上次了。今天我决定继续写关于ege图形库的文章。
那么第一篇是关于基础画图的,那么话不多说,让我们开始。
上期文章:DEV-C++ ege.h库进阶
上上期文章:DEV-C++ ege.h库入门
二、基本绘图函数
1.putpixel和getpixel
还是一样,让我们先看一看函数定义:
color_t EGEAPI getpixel (int x, int y, PCIMAGE pimg = NULL); // 获取点的颜色
void EGEAPI putpixel (int x, int y, color_t color, PIMAGE pimg = NULL); // 画点
这就非常简单了,不必多说
他们的功能就是画一个点和获取一个点的颜色。
示例程序:
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
putpixel(100,100,WHITE);
xyprintf(0,0,"%p",getpixel(100,100));
getch();
closegraph();
return 0;
}
2.putpixels
批量画点。他的意思是画很多个点。
我的建议是使用多个putpixel。
因为他的格式有点奇怪。
void EGEAPI putpixels (int nPoint, int* pPoints, PIMAGE pimg = NULL); // 批量画点
首先nPoint表示要画点的个数。
接下来的pPoints是一个指针,就说明他要传入一个数组。
而数组的格式很奇怪,数组的第3n+1个参数代表第(n+1)个点的x坐标,第3n+2个参数代表第(n+1)个点的y坐标,而第3n个参数代表第n个点的颜色。
就比如说我要画两个坐标分别为(100,100)和(150,150)的白色的点,就需要建一个数组
int a[]={100,100,WHITE,150,150,WHITE};
然后再
putpixels(2,a);
所以就很麻烦。
示例程序:
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
int a[]={50,50,WHITE,150,150,WHITE,200,200,WHITE,250,250,WHITE};
putpixels(4,a);
getch();
closegraph();
return 0;
}
3.putpixel_withalpha
void EGEAPI putpixel_withalpha (int x, int y, color_t color, PIMAGE pimg = NULL); // 带透明度画点
这个函数就是带上Alpha值画点,也就是透明度。
不过我也不知道有什么用。
要表示有Alpha值的颜色非常简单。
正常来说白色的RGB是(255,255,255),把他们变成16进制,0xFFFFFF就代表白色。
要加上透明度,只要在颜色前面再加上一个[0,256)的16进制数字就好了。
For example,0x7FFFFFFF代表Alpha值为0x7f,也就是127,的白色。
有个更简单的方法,就是使用EGERGBA函数。(准确来讲不是个函数)
EGERGBA(255,255,255,127)就相当于0x7FFFFFFF.
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
for(int i=100;i<=200;i++)
for(int j=100;j<=200;j++)
putpixel_withalpha(i,j,EGERGBA(255,255,255,127));
for(int i=200;i<=300;i++)
for(int j=200;j<=300;j++)
putpixel_withalpha(i,j,0xFFFFFFFF);
getch();
closegraph();
return 0;
}
4.line
这就不用多说了吧,就是画一条线。
line(0,0,100,100)
5.moveto
这个函数是把当前的“画笔”移动。
这就像是python的turtle库,有一个画笔。
初始画笔的坐标是(0,0)
6.linerel和lineto
void EGEAPI linerel(int dx, int dy, PIMAGE pimg = NULL); // 画线(至相对坐标)
void EGEAPI lineto(int x, int y, PIMAGE pimg = NULL); // 画线(至绝对坐标)
linerel就是从当前“画笔”的坐标画线到一个相对坐标。
例如,当前的坐标为(100,200),dx=-10,dy=20.
那么就相当于line(100,200,90,220)。
lineto就更直接了,就是从当前坐标画线到(x,y)
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
moveto(100,200);
linerel(10,-20);
moveto(100,200);
lineto(0,0);
getch();
closegraph();
return 0;
}
7.rectangle
void EGEAPI rectangle(int left, int top, int right, int bottom, PIMAGE pimg = NULL); // 画矩形
这个也是非常简单,从(left,top)画一个长方形到(right,bottom)。
8.arc
void EGEAPI arc(int x, int y, int stangle, int endangle, int radius, PIMAGE pimg = NULL); // 画圆弧
这个函数就是画圆弧,准确来说是画一个不完整的圆。
x,y:圆心
stangle,endangle:始边和终边的角度(角度值)
radius:半径
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
arc(100,100,45,210,50);
getch();
closegraph();
return 0;
}
9.circle
circle也非常简单,画圆。
circle(100,100,50)代表以(100,100)为圆心,画一个半径50的圆。
10.pieslice
这个和arc函数也是一样的,只是画一个扇形。
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
pieslice(100,100,45,210,50);
getch();
closegraph();
return 0;
}
11.ellipse
这个也是跟arc差不多,但有两个半径,因为是画椭圆圆弧。
12.fillellipse
这个就是开始正经地画椭圆了,而且是填充了颜色的,所以我们要先setfillcolor设置填充颜色。
void EGEAPI setfillcolor(color_t color, PIMAGE pimg = NULL); // 设置当前绘图填充色
void EGEAPI fillellipse(int x, int y, int xradius, int yradius, PIMAGE pimg = NULL); // 画填充椭圆
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
setfillcolor(WHITE);
fillellipse(100,100,50,70);
getch();
closegraph();
return 0;
}
13.sector
这个就是ellipse和fillellipse的结合了,画一个不完整的椭圆,而且还是填充颜色的。
void EGEAPI sector(int x, int y, int stangle, int endangle, int xradius, int yradius, PIMAGE pimg = NULL); // 画填充椭圆扇形
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
setfillcolor(WHITE);
sector(100,100,45,210,50,70);
getch();
closegraph();
return 0;
}
14.roundrect
画一个圆角矩形。
void EGEAPI roundrect(int left, int top, int right, int bottom, int xradius, int yradius, PIMAGE pimg = NULL); //画圆角矩形
前四个参数与rectangle一样,后两个就是矩形的圆角的两个半径。
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
setfillcolor(WHITE);
roundrect(100,100,600,450,50,70);
getch();
closegraph();
return 0;
}
15.arcf,circlef,pieslicef,ellipsef,fillellipsef,sectorf
功能与前面的都一样,只是参数是float,小数类型。
也就是说,你可以circle(100.25,200.46,35.20)。
虽然说也是没什么太大的用处。
16.bar
与rectangle一样,只是有填充颜色的。
void EGEAPI bar(int left, int top, int right, int bottom, PIMAGE pimg = NULL); // 画无边框填充矩形
17.bar3d
void EGEAPI bar3d(int left, int top, int right, int bottom, int depth, int topflag, PIMAGE pimg = NULL); // 画有边框三维填充矩形
画一个长方体,depth表示深度,topflag如果为一就有顶,否则没有。
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
setfillcolor(WHITE);
bar3d(100,100,200,300,50,0);
bar3d(300,100,400,300,50,1);
getch();
closegraph();
return 0;
}
18.fillrect和fillroundrect
fillrect:画填充矩形
fillroundrect:画填充圆角矩形
fillrect和bar的不同:
fillrect:有边框,也就是说可以使用setcolor调整边框颜色。
bar:无边框。
void EGEAPI fillrect(int left, int top, int right, int bottom, PIMAGE pimg = NULL); //画填充矩形
void EGEAPI fillroundrect(int left, int top, int right, int bottom, int xradius, int yradius, PIMAGE pimg = NULL); //画填充圆角矩形
19.drawpoly
画多边形。(就是给numpoints个点,把他们相连得到的多边形)
void EGEAPI drawpoly(int numpoints, const int *polypoints, PIMAGE pimg = NULL); // 画多边形
numpoints:点的个数
polypoints:和putpixels一样,但没有颜色。
例如,int a[]={100,100,200,200,100,200,200,100}中有4个点,第n个点的坐标为(a[2n-2],a[2n-1])。
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
int a[]={100,100,200,100,100,200,200,200,100,100};
drawpoly(5,a);
getch();
closegraph();
return 0;
}
Tips:点相连的顺序取决于输入数组的顺序,就比如1号点只会连到2号,2号只会连到3号……需要注意的是,n号点默认不与1号相连,也就是说,如果不在最后加一个1号点的坐标的话,画出来就是这样。
20.drawlines
和drawpoly差不多,只不过数组变成了几条线,也就是说int a[]={100,100,200,200}代表一条线。
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
int a[]={100,100,200,100,100,200,200,200};
drawlines(2,a);
getch();
closegraph();
return 0;
}
21.fillpoly
格式与drawpoly一样只不过是填充色的。
22.fillpoly_gradient
fillpoly+渐变色。
void EGEAPI fillpoly_gradient(int numpoints, const ege_colpoint* polypoints, PIMAGE pimg = NULL); // 画渐变填充的多边形
一件值得注意的事情是,数组的类型是ege_colpoint。这是一个结构体。
typedef struct ege_colpoint {
float x;
float y;
color_t color;
}ege_colpoint;
意思也不用我说了吧,直接上代码。
#include<bits/stdc++.h>
#include<ege.h>
using namespace std;
using namespace ege;
int main(){
initgraph(640,480);
setfillcolor(WHITE);
ege_colpoint a[]={
(ege_colpoint){100,100,(color_t)WHITE},
(ege_colpoint){200,100,(color_t)RED},
(ege_colpoint){100,200,(color_t)YELLOW},
(ege_colpoint){200,200,(color_t)BLUE},
(ege_colpoint){100,100,(color_t)GREEN}
};
fillpoly_gradient(5,a);
getch();
closegraph();
return 0;
}
比较麻烦。
23.floodfill
void EGEAPI floodfill(int x, int y, int border, PIMAGE pimg = NULL); // 按边界颜色填充区域
这个函数使用setfillstyle设置的填充方式对区域进行填充。填充颜色由setfillcolor函数决定。
x:填充的起始点 x 坐标。
y:填充的起始点 y 坐标。
border:填充的边界颜色。填充动作在该颜色围成的区域内填充。如果该颜色围成的区域不封闭,那么将使全屏幕都填充上。
三、总结
以上就是ege库里的基本画图函数了。这也还是最基本的函数。
下一期,我将会讲解ege的高级画图函数。
上一期:DEV-C++ ege.h库进阶
四、附件
基本绘图函数的函数声明:
// 基本绘图函数
color_t EGEAPI getpixel (int x, int y, PCIMAGE pimg = NULL); // 获取点的颜色
void EGEAPI putpixel (int x, int y, color_t color, PIMAGE pimg = NULL); // 画点
color_t EGEAPI getpixel_f(int x, int y, PCIMAGE pimg = NULL); // 获取点的颜色
void EGEAPI putpixel_f(int x, int y, color_t color, PIMAGE pimg = NULL); // 绝对坐标画点
void EGEAPI putpixels (int nPoint, int* pPoints, PIMAGE pimg = NULL); // 批量画点
void EGEAPI putpixels_f(int nPoint, int* pPoints, PIMAGE pimg = NULL); // 批量画点
void EGEAPI putpixel_withalpha (int x, int y, color_t color, PIMAGE pimg = NULL); // 带透明度画点
void EGEAPI putpixel_withalpha_f(int x, int y, color_t color, PIMAGE pimg = NULL); // 带透明度绝对坐标画点
void EGEAPI putpixel_savealpha (int x, int y, color_t color, PIMAGE pimg = NULL); // 设置像素点的颜色(同时保留原有alpha值)
void EGEAPI putpixel_savealpha_f(int x, int y, color_t color, PIMAGE pimg = NULL); // 设置像素点的颜色(同时保留原有alpha值,使用绝对坐标)
void EGEAPI moveto(int x, int y, PIMAGE pimg = NULL); // 移动当前点(绝对坐标)
void EGEAPI moverel(int dx, int dy, PIMAGE pimg = NULL); // 移动当前点(相对坐标)
void EGEAPI line(int x1, int y1, int x2, int y2, PIMAGE pimg = NULL); // 画线
void EGEAPI linerel(int dx, int dy, PIMAGE pimg = NULL); // 画线(至相对坐标)
void EGEAPI lineto(int x, int y, PIMAGE pimg = NULL); // 画线(至绝对坐标)
void EGEAPI line_f(float x1, float y1, float x2, float y2, PIMAGE pimg = NULL); // 画线
void EGEAPI linerel_f(float dx, float dy, PIMAGE pimg = NULL); // 画线(至相对坐标)
void EGEAPI lineto_f(float x, float y, PIMAGE pimg = NULL); // 画线(至绝对坐标)
void EGEAPI rectangle(int left, int top, int right, int bottom, PIMAGE pimg = NULL); // 画矩形
void EGEAPI arc(int x, int y, int stangle, int endangle, int radius, PIMAGE pimg = NULL); // 画圆弧
void EGEAPI circle(int x, int y, int radius, PIMAGE pimg = NULL); // 画圆
void EGEAPI pieslice(int x, int y, int stangle, int endangle, int radius, PIMAGE pimg = NULL); // 画填充圆扇形
void EGEAPI ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius, PIMAGE pimg = NULL);// 画椭圆弧线
void EGEAPI fillellipse(int x, int y, int xradius, int yradius, PIMAGE pimg = NULL); // 画填充椭圆
void EGEAPI sector(int x, int y, int stangle, int endangle, int xradius, int yradius, PIMAGE pimg = NULL); // 画填充椭圆扇形
void EGEAPI roundrect(int left, int top, int right, int bottom, int xradius, int yradius, PIMAGE pimg = NULL); //画圆角矩形
void EGEAPI arcf(float x, float y, float stangle, float endangle, float radius, PIMAGE pimg = NULL); // 画圆弧
void EGEAPI circlef(float x, float y, float radius, PIMAGE pimg = NULL); // 画圆
void EGEAPI pieslicef(float x, float y, float stangle, float endangle, float radius, PIMAGE pimg = NULL); // 画填充圆扇形
void EGEAPI ellipsef(float x, float y, float stangle, float endangle, float xradius, float yradius, PIMAGE pimg = NULL);// 画椭圆弧线
void EGEAPI fillellipsef(float x, float y, float xradius, float yradius, PIMAGE pimg = NULL); // 画填充椭圆
void EGEAPI sectorf(float x, float y, float stangle, float endangle, float xradius, float yradius, PIMAGE pimg = NULL); // 画填充椭圆扇形
void EGEAPI bar(int left, int top, int right, int bottom, PIMAGE pimg = NULL); // 画无边框填充矩形
void EGEAPI bar3d(int left, int top, int right, int bottom, int depth, int topflag, PIMAGE pimg = NULL); // 画有边框三维填充矩形
void EGEAPI fillrect(int left, int top, int right, int bottom, PIMAGE pimg = NULL); //画填充矩形
void EGEAPI fillroundrect(int left, int top, int right, int bottom, int xradius, int yradius, PIMAGE pimg = NULL); //画填充圆角矩形
void EGEAPI drawpoly(int numpoints, const int *polypoints, PIMAGE pimg = NULL); // 画多边形
void EGEAPI drawlines(int numlines, const int *polypoints, PIMAGE pimg = NULL); // 画多条不连续线(扩展函数)
void EGEAPI drawbezier(int numpoints, const int *polypoints, PIMAGE pimg = NULL); // 画bezier曲线(扩展函数)
void EGEAPI fillpoly(int numpoints, const int *polypoints, PIMAGE pimg = NULL); // 画填充的多边形
void EGEAPI fillpoly_gradient(int numpoints, const ege_colpoint* polypoints, PIMAGE pimg = NULL); // 画渐变填充的多边形
void EGEAPI floodfill(int x, int y, int border, PIMAGE pimg = NULL); // 按边界颜色填充区域
void EGEAPI floodfillsurface(int x, int y, color_t areacolor, PIMAGE pimg = NULL); // 按起始点颜色填充区域