This blog is under construction

Thursday 12 September 2013

rectangle example in c

Header file:
    graphics.h

Synopsis:
      void rectangle (int left, int top, int right, int bottom);
     (left, top) - top left corner of the rectangle
     (right, bottom) - bottom right corner of the rectangle

Description:
     rectangle() draws a rectangle using current drawing color.


rectangle function using c graphics



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

  int main(void) {
        /* request auto detection */
        int gdriver = DETECT, gmode, errorcode;
        int i, left, top, right, bottom;

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

        /* read result of initialization */
        errorcode = graphresult();

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

        left = 0;
        /* y-axis maximum value */
        top = getmaxy() / 2;


        right = 150;
        /* x-axis maximum value */
        bottom = (getmaxy() / 2) + 50;

        for (i = 0; i < 400; i++) {
                /* setting drawing color as white */
                setcolor(getmaxcolor());
                if (i % 2 == 0) {
                        setfillstyle(SLASH_FILL, 15);
                } else {
                        setfillstyle(BKSLASH_FILL, 15);
                }

                /* draw a rectangle */
                rectangle(left + i, top, right + i, bottom);
                rectangle(100 + i, top - 50, right + i, bottom - 50);

                /* draw a circle */
                circle(37 + i, bottom + 10, 20);

                /* fill the bounded region with current fill color */
                floodfill(37 + i, bottom + 10, 15);
                floodfill(37 + i, bottom + 10 - 19, 15);

                circle(112 + i, bottom + 10, 20);

                floodfill(112 + i, bottom + 10, 15);
                floodfill(112 + i, bottom + 10 - 19, 15);
                /* sleep for 100 milliseconds */
                delay(100);

                /* clears graphic screen */
                cleardevice();
        }

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





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


No comments:

Post a Comment