This blog is under construction

Tuesday, 10 September 2013

drawpoly example in c

Header file:
    graphics.h

Synopsis:
     void drawpoly(int num, int *points);
     num - number of points
     *points - pointer to an array of intergers(num X 2) and each 
                    pair of points gives a coordinate(x,y) of polygon

Description:
      drawpoly() draws a polygon using the given points.


drawpoly function in c graphics



  #include <graphics.h>
  #include <stdlib.h>
  #include <stdio.h>
  #include <conio.h>
  #include <dos.h>

  int main(void) {
        /* request auto detection */
        int graphicDriver = DETECT, graphicMode;
        int err, poly[12];

        /* initialize graphics and local variables */
        initgraph(&graphicDriver,
                &graphicMode, "C:/TURBOC3/BGI");

        /* read result of initialization */
        err = graphresult();
        if (err != grOk) {
                /* an error occurred */
                printf("Graphic Error: %s\n",
                                grapherrormsg(err));
                return 0;
        }

        /* 1st coordinate of the polygon */
        poly[0] = getmaxx() / 2;
        poly[1] = 0;

        /* 2nd coordinate of the polygon */
        poly[2] = (getmaxx() / 2) - 200;
        poly[3] = getmaxy() - 100;

        /* 3rd coordinate of the polygon */
        poly[4] = (getmaxx() / 2) + 200;
        poly[5] = 100;

        /* 4th coordinate of the polygon */
        poly[6] = (getmaxx() / 2) - 200;
        poly[7] = 100;

        /* 5th coordinate of the polygon */
        poly[8] = (getmaxx() / 2) + 200;
        poly[9] = getmaxy() - 100;

        /*
         * drawpoly doesn't automatically close the
         * polygon, so we need to close it explicitly
         */
        poly[10] = (getmaxx() / 2);
        poly[11] = 0;

        /* draw the polygon */
        drawpoly(6, poly);

        /* clean up */
        getch();
        closegraph();
        return 0;
  }




Output: (drawpoly built-in function in c graphics.h)


No comments:

Post a Comment