当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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


头文件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 <graphics.h> 
  
// 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 <graphics.h> 
  
// 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; 
}

输出:




相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 fillpoly() function in C。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。