Software & Finance





Turbo C Graphics - drawpoly function





Given a collection x and y points, drawpoly will draw the polygon. fillpoly can is used to draw the polygon with with any color filled and / or style set by setfillstyle function.

 

 

Back to Turbo C Graphics Index



Source Code


 

    
    #include "graphics.h"
    #include "stdlib.h"
    #include "stdio.h"
    #include "conio.h"
    
    int InitGraphics()
    {
        int grd, grm;
        int gresult;
        // Detect the graphics driver and mode
        detectgraph(&grd,&grm);
        // initialize the graphics mode with initgraph
        initgraph(&grd, &grm, "");
    
        gresult = graphresult();
        if(gresult != grOk)
        {
            printf(grapherrormsg(gresult));
            getch();
            return -1;
        }
        // set the background color
        setbkcolor(BLUE);
        // set the foreground color
        setcolor(WHITE);
        // draw a white color border with rectangle
        rectangle(0,0,getmaxx(),getmaxy());
        return 1;
    }
    
    void simluate_polygon()
    {
       #define maxpoints  24
       int maxx, maxy;
       int poly[maxpoints];
    
       maxx = getmaxx() / 2;
       maxy = getmaxy() / 2;
    
       for(int i = 0; i < maxpoints - 2; i += 2)
       {
        poly[i] = (20 + rand()) % maxx;
        poly[i+1] = (20 + rand()) % maxy;
       }
       poly[maxpoints - 2] = poly[0];
       poly[maxpoints - 1] = poly[1];
    
       drawpoly(maxpoints / 2, poly);
       getch();
    
       setfillstyle(SOLID_FILL, RED);
       fillpoly(maxpoints / 2, poly);
       getch();
    
    }
    int main(void)
    {
    
       InitGraphics();
    
       for(int i = 0; i < 5; i++)
       {
          clearviewport();
          simluate_polygon();
    
       }
    
       closegraph();
       exit(0);
       return 0;
    }
    

    

Output