C语言 fillpoly()用法及代码示例

您所在的位置:网站首页 fillC语言 C语言 fillpoly()用法及代码示例

C语言 fillpoly()用法及代码示例

2024-01-21 04:37| 来源: 网络整理| 查看: 265

头文件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.

例子:

Input:arr[] = {320, 150, 400, 250, 250, 350, 320, 150}; Output: Input:arr[] = {120, 250, 400, 250, 400, 350, 450, 200, 120, 250}; Output:

说明: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; }

输出:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3