當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。