This blog is under construction

Sunday, 8 September 2013

Nested Polygons - Animation using C graphics

Implement nested polygons using C graphics


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

  int main() {
        /* request auto detection */
        int gdriver = DETECT, gmode, err;
        int i, midx, midy, x[10];

        /* initialize graphic mode */
        initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
        err = graphresult();

        if (err != grOk) {
                /* error occurred */
                printf("Graphics Error: %s\n",
                grapherrormsg(err));
                return 0;
        }

        /* mid position in x and y-axis */
        midx = getmaxx() / 2;
        midy = getmaxy() / 2;

        /* set the position of outermost polygon */
        x[0] = midx, x[1] = midy - 100;
        x[2] = midx + 60,  x[3] = midy - 50;
        x[4] = midx + 40,  x[5] = midy + 50;
        x[6] = midx - 40,  x[7] = midy + 50;
        x[8] = midx - 60,  x[9] = midy - 50;

        x[0] = midx, x[1] = midy - 100 - 125;
        x[2] = midx + 60 + 150,  x[3] = midy - 50 - 100;
        x[4] = midx + 40 + 150,  x[5] = midy + 50 + 150;
        x[6] = midx - 40 - 150,  x[7] = midy + 50 + 150;
        x[8] = midx - 60 - 150,  x[9] = midy - 50 - 100;

        /* drawing nested polygons from out to in */
        for (i = 1; i <= 15; i++) {
                setcolor(i);
                setfillstyle(SOLID_FILL, i);
                fillpoly(5, x);
                x[1] = x[1] + 7;
                x[2] = x[2] - 10;
                x[3] = x[3] + 10;
                x[4] = x[4] - 10;
                x[5] = x[5] - 10;
                x[6] = x[6] + 10;
                x[7] = x[7] - 10;
                x[8] = x[8] + 10;
                x[9] = x[9] + 10;
                sleep(1);
        }

        getch();

        /* deallocate memory allocated for graphic screen */
        closegraph();
        return 0;
  }





Output: (Animate nested polygons using c graphics)


No comments:

Post a Comment