头文件graphics.h包含fillpoly()函数,该函数用于绘制和填充多边形,即三角形,矩形,五边形,六边形等。它需要与drawpoly()相同的参数。
句法 :
void fillpoly( int number, int *polypoints );
where,
number indicates (n + 1) number of points
where, n is the number of vertices in a
polygon. polypoints points to a sequence
of (n*2) integers.
例子 :
输入:arr [] = {320,150,400,250,250,350,320,150};输出 : 输入:arr [] = {120,250,400,250,400,350,450,200,120,250};输出 :
说明: fillpoly()的声明包含两个参数:number表示(n + 1)点数,其中n是多边形中的顶点数。第二个参数,即polypoints指向(n * 2)的序列整数。每对整数给出多边形上一个点的x和y坐标。我们指定(n + 1)个点是因为第一个点的坐标应等于第(n + 1)个以绘制完整的图形。
示例1:使用fillpoly绘制三角形。
int arr [] = {320,150,400,250,250,350,320,150};
数组arr包含三角形的坐标,分别是(320,150),(400,250)和(250,350)。请注意,数组中的最后一个点(320,150)与第一个相同。
以下是fillpoly()函数。
// C Implementation for fillpoly()
#include
// driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
// coordinates for polygon
int arr[] = {320, 150, 400, 250,
250, 350, 320, 150};
// initgraph initializes the
// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");
// fillpoly function
fillpoly(4, arr);
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();
return 0;
}
输出 :
注意: fillpoly()使用当前的填充图案和颜色填充,可以使用setfillstyle进行更改。
下面是使用setfillstyle()填充多边形的程序。
// C Implementation for fillpoly()
// using setfillstyle()
#include
// driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
// coordinates of polygon
int arr[] = {320, 150, 400, 250,
250, 350, 320, 150};
// initgraph initializes the
// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");
// setfillstyle function sets the
// current fill pattern and fill color.
setfillstyle(XHATCH_FILL, RED);
// fillpoly function
fillpoly(4, arr);
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();
return 0;
}
输出 :
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。