locked
C-Graphics Programming RRS feed

  • Question

  • Guys Varun requested a C-Graphics programming thread, and here it is Smile lets try to teach others graphics programming in c.
    Monday, April 2, 2007 4:38 PM

Answers

  • Yes man, waiting eagerly to learn how to do graphics in C.. As C is the mother of all languages, graphics must have some history attached with C, So lets start the tutorials
    Thursday, April 5, 2007 5:11 PM


  • //a program to draw a rectangle and 2 lines

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT,gmode;
        initgraph(&gdriver,&gmode,"");
        rectangle(100,100,200,200);
        line(100,100,200,200);
        line(100,200,200,100);


    }


    Thursday, April 5, 2007 5:34 PM

  • Program to draw Circle, rectangle, lines, and with color 

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT,gmode;
        initgraph(&gdriver,&gmode,"");
        setcolor(RED);
        circle(320,240,100);
        line(320,140,320,340);
        line(220,240,420,240);
        rectangle(220,140,420,340);
        line(220,140,420,340);

    }

    Thursday, April 5, 2007 5:34 PM

  • //Good Design

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT,gmode;
        int midx,midy;
        initgraph(&gdriver,&gmode,"");
        midx=getmaxx()/2;
        midy = getmaxy()/2;
        circle(midx,midy,200);
        rectangle(midx-200,midy-200,midx+200,midy+200);
        line(midx-174,midy+100,midx+174,midy+100);     //triangle _    / \
        line(midx-174,midy+100,midx,midy-200);        //triangle /   /   \
        line(midx,midy-200,midx+174,midy+100);        //triangle \  /_____\
        ellipse(midx,midy-37,0,360,50,137);
        pieslice(midx-174,midy+100,0,60,100);
        pieslice(midx+174,midy+100,120,180,100);

        getch();
    }
    Thursday, April 5, 2007 5:35 PM
  • Harshil which compiler to use to execute this codes?? I tried VC++ and Turbo C 3, but the code didnt worked in any of them... also please explain a liitle about what you did in this codes??
    Thursday, April 5, 2007 5:50 PM
  • I have created them in TC....
    the settings that you need to do is....

    1. Options > Linker > Libraries > Enable Graphics Library.

    Doing this should work properly most of the time, but if it dont work then do 2nd step.

    2.   Change the 3rd parameter of the initgraph(&gdriver,&gmode,""); function to the path that contains the graphics drivers (*.BGI) it will probably be in C:\TC\BGI.
    change it to initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

    Now it should work Smile



    More ifo about the 3rd parameter
    pathtodriver │ Specifies the directory path where initgraph looks for
                       │ graphics drivers (*.BGI) first.
                       │  ■ If they're not there, initgraph looks in the current
                       │    directory.
                       │  ■ If pathtodriver is null, the driver files must be in
                       │    the current directory.


    Thursday, April 5, 2007 6:04 PM
  • Well I activated the graphical libraries in tc, but still its not working.. I tried searching or the initgraph file but was unable to locate it.. what next should i do??
    Thursday, April 5, 2007 6:50 PM
  • is it possible to develop a graphical application in c as in VB ....
    Tuesday, April 10, 2007 8:12 PM
  • As I understand, VB is made from c and c++, ie, The graphics that we define in vb, have their prewritten code in side VB, which are from c and c++, Its just that we dont have to write their code, and so we can concentrate more on the program rather than the subsidery part of graphics... So it is possible to define graphics in C and C++ as we do in VB, its just that it will get too tiresome to do that
    Wednesday, April 11, 2007 3:20 AM
  • Yes m8 there are two ways to do graphial things like VB in C/C++
    1. Write the code on your own
    2. Use windows API that is VC++
    Wednesday, April 11, 2007 3:05 PM
  • how to open an image using C language.
    Sunday, April 15, 2007 6:57 PM
  • You can open any file in C, the main problem is how to display the file that has an image onto the screen Stick out tongue

    I guess may be someone more wise in this area can spread some light on to it for us learners.
    Monday, April 16, 2007 2:59 PM
  • IMO, displaying generic image in c using BGI graphics is not possible because BGI only supports 16 color palette and nowadays our images are of at least 256 color palette.
    Tuesday, April 17, 2007 1:42 AM
  • any other idea how to display graphics in C ?
    Tuesday, April 17, 2007 4:23 AM
  • Generate a line using Digital Differential Analyzer (DDA) Algorithm.

    (Origin is considered as Screen Origin, means (0, 0) refers to top of
    screen).

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

    #define ROUND(val) ((int)(val + 0.05))

    void DDA(int xa, int ya, int xb, int yb)
    {
        int temp, dx, dy, steps, ctr;
        float xIncrement, yIncrement, x = xa, y = ya;

        if(xb < xa)
        {
            temp = xa;
            xa = xb;
            xb = temp;

            temp = ya;
            ya = yb;
            yb = temp;
        }

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dx) > abs(dy))
        {
            steps = abs(dx);
        }
        else
        {
            steps = abs(dy);
        }

        xIncrement = dx / (float)steps;
        yIncrement = dy / (float)steps;

        putpixel(ROUND(x), ROUND(y), RED);

        for(ctr = 0 ; ctr < steps ; ctr++)
        {
            x += xIncrement;
            y += yIncrement;

            putpixel(ROUND(x), ROUND(y), RED);
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        DDA(xa, ya, xb, yb);

        getch();
    }

    /*..............................End Of Program.............................*/



    Sunday, April 22, 2007 4:34 PM

  • Generate a line using Digital Differential Analyzer (DDA) Algorithm.

    (Origin is considered at center of screen, means (0, 0) refers to center
    of screen).

    Note :
    ------
    In this program, as we are moving center to the center of the screen, the
    values on Y axis are in opposite order as they are on physical graph paper.
    This means that the Y-axis is plotted as -ve to +ve vertically on the screen.
    There is no specific issue with X-axis.

    To resolve the trouble stated above, the values of Y-axes arguments are
    negated in the function.


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

    #define ROUND(val) ((int)(val + 0.05))

    void DDA(int xa, int ya, int xb, int yb, int color)
    {
        int temp, dx, dy, steps, ctr;
        float xIncrement, yIncrement, x, y;

        ya = -ya;
        yb = -yb;

        if(xb < xa)
        {
            temp = xa;
            xa = xb;
            xb = temp;

            temp = ya;
            ya = yb;
            yb = temp;
        }

        x = (getmaxx() / 2) + xa;
        y = (getmaxy() / 2) + ya;

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dx) > abs(dy))
        {
            steps = abs(dx);
        }
        else
        {
            steps = abs(dy);
        }

        xIncrement = dx / (float)steps;
        yIncrement = dy / (float)steps;

        putpixel(ROUND(x), ROUND(y), color);

        for(ctr = 0 ; ctr < steps ; ctr++)
        {
            x += xIncrement;
            y += yIncrement;

            putpixel(ROUND(x), ROUND(y), color);
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        DDA(0, -(getmaxy() / 2), 0, getmaxy() / 2, WHITE);
        DDA(-(getmaxx() / 2), 0, getmaxx() / 2, 0, WHITE);
        DDA(xa, ya, xb, yb, GREEN);

        getch();
    }

    /*..............................End Of Program.............................*/

    Sunday, April 22, 2007 4:38 PM

  • Generate a line using Improved DDA Algorithm (h-value Algorithm).

    (Origin is considered as Screen Origin, means (0, 0) refers to top of
    screen).

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

    void ImprovedDDA(int xa, int ya, int xb, int yb)
    {
        int dx, dy, temp, ctr, r, multiplier = 1;
        float m, h;

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dy) <= abs(dx) && dx != 0)
        {
            m = dy / (float)dx;

            if(dx < 0)
            {
                temp = xb;
                xb = xa;
                xa = temp;

                temp = yb;
                yb = ya;
                ya = temp;
            }

            ctr = xa;

            r = (int)(ya + 0.5);

            h = m;

            if(h < 0)
            {
                multiplier = -1;
            }

            while(ctr <= xb)
            {
                putpixel(ctr, r, WHITE);

                ctr = ctr + 1;

                if(h >= 0.5)
                {
                    r = r + (multiplier) * 1;
                    h = h + (multiplier) * m - 1;
                }
                else
                {
                    h = h + (multiplier) * m;
                }
            }
        }
        else if(abs(dx) < abs(dy) && dy != 0)
        {
            m = dx / (float)dy;

            if(dy < 0)
            {
                temp = yb;
                yb = ya;
                ya = temp;

                temp = xb;
                xb = xa;
                xa = temp;
            }

            ctr = ya;

            r = (int)(xa + 0.5);

            h = m;

            if(h < 0)
            {
                multiplier = -1;
            }

            while(ctr <= yb)
            {
                putpixel(r, ctr, WHITE);

                ctr = ctr + 1;

                if(h >= 0.5)
                {
                    r = r + (multiplier) * 1;
                    h = h + (multiplier) * m - 1;
                }
                else
                {
                    h = h + (multiplier) * m;
                }
            }
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        ImprovedDDA(xa, ya, xb, yb);

        getch();
    }

    Sunday, April 22, 2007 4:43 PM

  • Generate a line using Improved DDA Algorithm (h-value Algorithm).

    (Origin is considered at center of screen, means (0, 0) refers to center
    of screen).

    Note :
    ------
    In this program, as we are moving center to the center of the screen, the
    values on Y axis are in opposite order as they are on physical graph paper.
    This means that the Y-axis is plotted as -ve to +ve vertically on the screen.
    There is no specific issue with X-axis.

    To resolve the trouble stated above, the values of Y-axes arguments are
    negated in the function.

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

    void ImprovedDDA(int xa, int ya, int xb, int yb, int color)
    {
        int dx, dy, temp, ctr, r;
        int x, y, multiplier = 1;
        float m, h;

        x = getmaxx() / 2;
        y = getmaxy() / 2;

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dy) <= abs(dx) && dx != 0)
        {
            m = dy / (float)dx;

            if(dx < 0)
            {
                temp = xb;
                xb = xa;
                xa = temp;

                temp = yb;
                yb = ya;
                ya = temp;
            }

            ctr = xa;

            r = (int)(ya + 0.5);

            h = m;

            if(h < 0)
            {
                multiplier = -1;
            }

            while(ctr <= xb)
            {
                putpixel(x + ctr, y - r, color);

                ctr = ctr + 1;

                if(h >= 0.5)
                {
                    r = r + (multiplier) * 1;
                    h = h + (multiplier) * m - 1;
                }
                else
                {
                    h = h + (multiplier) * m;
                }
            }
        }
        else if(abs(dx) < abs(dy) && dy != 0)
        {
            m = dx / (float)dy;

            if(dy < 0)
            {
                temp = yb;
                yb = ya;
                ya = temp;

                temp = xb;
                xb = xa;
                xa = temp;
            }

            ctr = ya;

            r = (int)(xa + 0.5);

            h = m;

            if(h < 0)
            {
                multiplier = -1;
            }

            while(ctr <= yb)
            {
                putpixel(x + r, y - ctr, color);

                ctr = ctr + 1;

                if(h >= 0.5)
                {
                    r = r + (multiplier) * 1;
                    h = h + (multiplier) * m - 1;
                }
                else
                {
                    h = h + (multiplier) * m;
                }
            }
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        ImprovedDDA(0, -(getmaxy() / 2), 0, getmaxy() / 2, WHITE);
        ImprovedDDA(-(getmaxx() / 2), 0, getmaxx() / 2, 0, WHITE);
        ImprovedDDA(xa, ya, xb, yb, GREEN);

        getch();
    }

    Sunday, April 22, 2007 4:46 PM

  • Generate a line using Bresenham Algorithm.

    (Origin is considered as Screen Origin, means (0, 0) refers to top of
    screen).


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

    void Bresenham(int xa, int ya, int xb, int yb)
    {
        int temp, dx, dy, p, increment1, increment2, x, y, otherEnd;
        int multiplier = 1;

        if(xb < xa)
        {
            temp = xb;
            xb = xa;
            xa = temp;

            temp = yb;
            yb = ya;
            ya = temp;
        }

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dy) <= abs(dx))
        {
            if(dy / (float)dx < 0)
            {
                multiplier = -1;
            }

            p = multiplier * 2 * dy - dx;
            increment1 = multiplier * 2 * dy;
            increment2 = 2 * ((multiplier * dy) - dx);

            if(xa > xb)
            {
                x = xb;
                y = yb;
                otherEnd = xa;
            }
            else
            {
                x = xa;
                y = ya;
                otherEnd = xb;
            }

            putpixel(x, y, WHITE);

            while(x < otherEnd)
            {
                x++;

                if(p < 0)
                {
                    p += increment1;
                }
                else
                {
                    y += multiplier;
                    p += increment2;
                }

                putpixel(x, y, WHITE);
            }
        }
        else
        {
            if(dx / (float)dy < 0)
            {
                multiplier = -1;
            }

            p = multiplier * 2 * dx - dy;
            increment1 = multiplier * 2 * dx;
            increment2 = 2 * ((multiplier * dx) - dy);

            if(ya > yb)
            {
                y = yb;
                x = xb;
                otherEnd = ya;
            }
            else
            {
                y = ya;
                x = xa;
                otherEnd = yb;
            }

            putpixel(x, y, WHITE);

            while(y < otherEnd)
            {
                y++;

                if(p > 0)
                {
                    p += increment1;
                }
                else
                {
                    x += multiplier;
                    p += increment2;
                }

                putpixel(x, y, WHITE);
            }
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        Bresenham(xa, ya, xb, yb);

        getch();
    }


    Sunday, April 22, 2007 4:57 PM
  • Thanks Sanket, you have been very very supportive here. I have computer graphics in the next semester, all these programs will be a great help for me during the next sem Smile you ROX m8. You are the ROXtar Smile
    Monday, April 23, 2007 9:56 AM
  • Thanks Sanket, It is very helpful code here.
    Tuesday, April 24, 2007 3:06 PM

All replies

  • Yes man, waiting eagerly to learn how to do graphics in C.. As C is the mother of all languages, graphics must have some history attached with C, So lets start the tutorials
    Thursday, April 5, 2007 5:11 PM


  • //a program to draw a rectangle and 2 lines

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT,gmode;
        initgraph(&gdriver,&gmode,"");
        rectangle(100,100,200,200);
        line(100,100,200,200);
        line(100,200,200,100);


    }


    Thursday, April 5, 2007 5:34 PM

  • Program to draw Circle, rectangle, lines, and with color 

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT,gmode;
        initgraph(&gdriver,&gmode,"");
        setcolor(RED);
        circle(320,240,100);
        line(320,140,320,340);
        line(220,240,420,240);
        rectangle(220,140,420,340);
        line(220,140,420,340);

    }

    Thursday, April 5, 2007 5:34 PM

  • //Good Design

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT,gmode;
        int midx,midy;
        initgraph(&gdriver,&gmode,"");
        midx=getmaxx()/2;
        midy = getmaxy()/2;
        circle(midx,midy,200);
        rectangle(midx-200,midy-200,midx+200,midy+200);
        line(midx-174,midy+100,midx+174,midy+100);     //triangle _    / \
        line(midx-174,midy+100,midx,midy-200);        //triangle /   /   \
        line(midx,midy-200,midx+174,midy+100);        //triangle \  /_____\
        ellipse(midx,midy-37,0,360,50,137);
        pieslice(midx-174,midy+100,0,60,100);
        pieslice(midx+174,midy+100,120,180,100);

        getch();
    }
    Thursday, April 5, 2007 5:35 PM
  • Harshil which compiler to use to execute this codes?? I tried VC++ and Turbo C 3, but the code didnt worked in any of them... also please explain a liitle about what you did in this codes??
    Thursday, April 5, 2007 5:50 PM
  • I have created them in TC....
    the settings that you need to do is....

    1. Options > Linker > Libraries > Enable Graphics Library.

    Doing this should work properly most of the time, but if it dont work then do 2nd step.

    2.   Change the 3rd parameter of the initgraph(&gdriver,&gmode,""); function to the path that contains the graphics drivers (*.BGI) it will probably be in C:\TC\BGI.
    change it to initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

    Now it should work Smile



    More ifo about the 3rd parameter
    pathtodriver │ Specifies the directory path where initgraph looks for
                       │ graphics drivers (*.BGI) first.
                       │  ■ If they're not there, initgraph looks in the current
                       │    directory.
                       │  ■ If pathtodriver is null, the driver files must be in
                       │    the current directory.


    Thursday, April 5, 2007 6:04 PM
  • Well I activated the graphical libraries in tc, but still its not working.. I tried searching or the initgraph file but was unable to locate it.. what next should i do??
    Thursday, April 5, 2007 6:50 PM
  • is it possible to develop a graphical application in c as in VB ....
    Tuesday, April 10, 2007 8:12 PM
  • As I understand, VB is made from c and c++, ie, The graphics that we define in vb, have their prewritten code in side VB, which are from c and c++, Its just that we dont have to write their code, and so we can concentrate more on the program rather than the subsidery part of graphics... So it is possible to define graphics in C and C++ as we do in VB, its just that it will get too tiresome to do that
    Wednesday, April 11, 2007 3:20 AM
  • Yes m8 there are two ways to do graphial things like VB in C/C++
    1. Write the code on your own
    2. Use windows API that is VC++
    Wednesday, April 11, 2007 3:05 PM
  • how to open an image using C language.
    Sunday, April 15, 2007 6:57 PM
  • You can open any file in C, the main problem is how to display the file that has an image onto the screen Stick out tongue

    I guess may be someone more wise in this area can spread some light on to it for us learners.
    Monday, April 16, 2007 2:59 PM
  • IMO, displaying generic image in c using BGI graphics is not possible because BGI only supports 16 color palette and nowadays our images are of at least 256 color palette.
    Tuesday, April 17, 2007 1:42 AM
  • any other idea how to display graphics in C ?
    Tuesday, April 17, 2007 4:23 AM
  • Generate a line using Digital Differential Analyzer (DDA) Algorithm.

    (Origin is considered as Screen Origin, means (0, 0) refers to top of
    screen).

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

    #define ROUND(val) ((int)(val + 0.05))

    void DDA(int xa, int ya, int xb, int yb)
    {
        int temp, dx, dy, steps, ctr;
        float xIncrement, yIncrement, x = xa, y = ya;

        if(xb < xa)
        {
            temp = xa;
            xa = xb;
            xb = temp;

            temp = ya;
            ya = yb;
            yb = temp;
        }

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dx) > abs(dy))
        {
            steps = abs(dx);
        }
        else
        {
            steps = abs(dy);
        }

        xIncrement = dx / (float)steps;
        yIncrement = dy / (float)steps;

        putpixel(ROUND(x), ROUND(y), RED);

        for(ctr = 0 ; ctr < steps ; ctr++)
        {
            x += xIncrement;
            y += yIncrement;

            putpixel(ROUND(x), ROUND(y), RED);
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        DDA(xa, ya, xb, yb);

        getch();
    }

    /*..............................End Of Program.............................*/



    Sunday, April 22, 2007 4:34 PM

  • Generate a line using Digital Differential Analyzer (DDA) Algorithm.

    (Origin is considered at center of screen, means (0, 0) refers to center
    of screen).

    Note :
    ------
    In this program, as we are moving center to the center of the screen, the
    values on Y axis are in opposite order as they are on physical graph paper.
    This means that the Y-axis is plotted as -ve to +ve vertically on the screen.
    There is no specific issue with X-axis.

    To resolve the trouble stated above, the values of Y-axes arguments are
    negated in the function.


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

    #define ROUND(val) ((int)(val + 0.05))

    void DDA(int xa, int ya, int xb, int yb, int color)
    {
        int temp, dx, dy, steps, ctr;
        float xIncrement, yIncrement, x, y;

        ya = -ya;
        yb = -yb;

        if(xb < xa)
        {
            temp = xa;
            xa = xb;
            xb = temp;

            temp = ya;
            ya = yb;
            yb = temp;
        }

        x = (getmaxx() / 2) + xa;
        y = (getmaxy() / 2) + ya;

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dx) > abs(dy))
        {
            steps = abs(dx);
        }
        else
        {
            steps = abs(dy);
        }

        xIncrement = dx / (float)steps;
        yIncrement = dy / (float)steps;

        putpixel(ROUND(x), ROUND(y), color);

        for(ctr = 0 ; ctr < steps ; ctr++)
        {
            x += xIncrement;
            y += yIncrement;

            putpixel(ROUND(x), ROUND(y), color);
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        DDA(0, -(getmaxy() / 2), 0, getmaxy() / 2, WHITE);
        DDA(-(getmaxx() / 2), 0, getmaxx() / 2, 0, WHITE);
        DDA(xa, ya, xb, yb, GREEN);

        getch();
    }

    /*..............................End Of Program.............................*/

    Sunday, April 22, 2007 4:38 PM

  • Generate a line using Improved DDA Algorithm (h-value Algorithm).

    (Origin is considered as Screen Origin, means (0, 0) refers to top of
    screen).

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

    void ImprovedDDA(int xa, int ya, int xb, int yb)
    {
        int dx, dy, temp, ctr, r, multiplier = 1;
        float m, h;

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dy) <= abs(dx) && dx != 0)
        {
            m = dy / (float)dx;

            if(dx < 0)
            {
                temp = xb;
                xb = xa;
                xa = temp;

                temp = yb;
                yb = ya;
                ya = temp;
            }

            ctr = xa;

            r = (int)(ya + 0.5);

            h = m;

            if(h < 0)
            {
                multiplier = -1;
            }

            while(ctr <= xb)
            {
                putpixel(ctr, r, WHITE);

                ctr = ctr + 1;

                if(h >= 0.5)
                {
                    r = r + (multiplier) * 1;
                    h = h + (multiplier) * m - 1;
                }
                else
                {
                    h = h + (multiplier) * m;
                }
            }
        }
        else if(abs(dx) < abs(dy) && dy != 0)
        {
            m = dx / (float)dy;

            if(dy < 0)
            {
                temp = yb;
                yb = ya;
                ya = temp;

                temp = xb;
                xb = xa;
                xa = temp;
            }

            ctr = ya;

            r = (int)(xa + 0.5);

            h = m;

            if(h < 0)
            {
                multiplier = -1;
            }

            while(ctr <= yb)
            {
                putpixel(r, ctr, WHITE);

                ctr = ctr + 1;

                if(h >= 0.5)
                {
                    r = r + (multiplier) * 1;
                    h = h + (multiplier) * m - 1;
                }
                else
                {
                    h = h + (multiplier) * m;
                }
            }
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        ImprovedDDA(xa, ya, xb, yb);

        getch();
    }

    Sunday, April 22, 2007 4:43 PM

  • Generate a line using Improved DDA Algorithm (h-value Algorithm).

    (Origin is considered at center of screen, means (0, 0) refers to center
    of screen).

    Note :
    ------
    In this program, as we are moving center to the center of the screen, the
    values on Y axis are in opposite order as they are on physical graph paper.
    This means that the Y-axis is plotted as -ve to +ve vertically on the screen.
    There is no specific issue with X-axis.

    To resolve the trouble stated above, the values of Y-axes arguments are
    negated in the function.

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

    void ImprovedDDA(int xa, int ya, int xb, int yb, int color)
    {
        int dx, dy, temp, ctr, r;
        int x, y, multiplier = 1;
        float m, h;

        x = getmaxx() / 2;
        y = getmaxy() / 2;

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dy) <= abs(dx) && dx != 0)
        {
            m = dy / (float)dx;

            if(dx < 0)
            {
                temp = xb;
                xb = xa;
                xa = temp;

                temp = yb;
                yb = ya;
                ya = temp;
            }

            ctr = xa;

            r = (int)(ya + 0.5);

            h = m;

            if(h < 0)
            {
                multiplier = -1;
            }

            while(ctr <= xb)
            {
                putpixel(x + ctr, y - r, color);

                ctr = ctr + 1;

                if(h >= 0.5)
                {
                    r = r + (multiplier) * 1;
                    h = h + (multiplier) * m - 1;
                }
                else
                {
                    h = h + (multiplier) * m;
                }
            }
        }
        else if(abs(dx) < abs(dy) && dy != 0)
        {
            m = dx / (float)dy;

            if(dy < 0)
            {
                temp = yb;
                yb = ya;
                ya = temp;

                temp = xb;
                xb = xa;
                xa = temp;
            }

            ctr = ya;

            r = (int)(xa + 0.5);

            h = m;

            if(h < 0)
            {
                multiplier = -1;
            }

            while(ctr <= yb)
            {
                putpixel(x + r, y - ctr, color);

                ctr = ctr + 1;

                if(h >= 0.5)
                {
                    r = r + (multiplier) * 1;
                    h = h + (multiplier) * m - 1;
                }
                else
                {
                    h = h + (multiplier) * m;
                }
            }
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        ImprovedDDA(0, -(getmaxy() / 2), 0, getmaxy() / 2, WHITE);
        ImprovedDDA(-(getmaxx() / 2), 0, getmaxx() / 2, 0, WHITE);
        ImprovedDDA(xa, ya, xb, yb, GREEN);

        getch();
    }

    Sunday, April 22, 2007 4:46 PM

  • Generate a line using Bresenham Algorithm.

    (Origin is considered as Screen Origin, means (0, 0) refers to top of
    screen).


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

    void Bresenham(int xa, int ya, int xb, int yb)
    {
        int temp, dx, dy, p, increment1, increment2, x, y, otherEnd;
        int multiplier = 1;

        if(xb < xa)
        {
            temp = xb;
            xb = xa;
            xa = temp;

            temp = yb;
            yb = ya;
            ya = temp;
        }

        dx = xb - xa;
        dy = yb - ya;

        if(abs(dy) <= abs(dx))
        {
            if(dy / (float)dx < 0)
            {
                multiplier = -1;
            }

            p = multiplier * 2 * dy - dx;
            increment1 = multiplier * 2 * dy;
            increment2 = 2 * ((multiplier * dy) - dx);

            if(xa > xb)
            {
                x = xb;
                y = yb;
                otherEnd = xa;
            }
            else
            {
                x = xa;
                y = ya;
                otherEnd = xb;
            }

            putpixel(x, y, WHITE);

            while(x < otherEnd)
            {
                x++;

                if(p < 0)
                {
                    p += increment1;
                }
                else
                {
                    y += multiplier;
                    p += increment2;
                }

                putpixel(x, y, WHITE);
            }
        }
        else
        {
            if(dx / (float)dy < 0)
            {
                multiplier = -1;
            }

            p = multiplier * 2 * dx - dy;
            increment1 = multiplier * 2 * dx;
            increment2 = 2 * ((multiplier * dx) - dy);

            if(ya > yb)
            {
                y = yb;
                x = xb;
                otherEnd = ya;
            }
            else
            {
                y = ya;
                x = xa;
                otherEnd = yb;
            }

            putpixel(x, y, WHITE);

            while(y < otherEnd)
            {
                y++;

                if(p > 0)
                {
                    p += increment1;
                }
                else
                {
                    x += multiplier;
                    p += increment2;
                }

                putpixel(x, y, WHITE);
            }
        }
    }

    void main(void)
    {
        int xa, ya, xb, yb;
        int gdriver = DETECT, gmode;

        clrscr();

        printf("Enter value for xa : ");
        scanf("%d", &xa);
        printf("Enter value for ya : ");
        scanf("%d", &ya);

        printf("Enter value for xb : ");
        scanf("%d", &xb);
        printf("Enter value for yb : ");
        scanf("%d", &yb);

        getch();

        clrscr();

        initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

        Bresenham(xa, ya, xb, yb);

        getch();
    }


    Sunday, April 22, 2007 4:57 PM
  • Thanks Sanket, you have been very very supportive here. I have computer graphics in the next semester, all these programs will be a great help for me during the next sem Smile you ROX m8. You are the ROXtar Smile
    Monday, April 23, 2007 9:56 AM
  • Thanks Sanket, It is very helpful code here.
    Tuesday, April 24, 2007 3:06 PM

  • Code Snippet

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

    void main()
    {
        int gdriver=DETECT, gmode;
        int i=0,x1,y1,x2,y2;
        x1 = 100;
        y1 = 120;
        x2 = 100;
        y2 = 300;
        initgraph(&gdriver,&gmode,"");
        line(x1,y1,x2,y2);
        delay(1000);
        getch();
        while(!kbhit() && (y1<=y2))
        {
            setcolor(WHITE);
            line(x1,y1,x2,y2);
            delay(20);
            setcolor(getbkcolor());
            line(x1++,y1++,x2++,y2--);
            x1+=2;
        }
        setcolor(WHITE);
        line(x1-2,y1-1,x2-1,y2+1);
        getch();
        closegraph();
    }

    Sunday, April 29, 2007 4:54 PM

  • Code Snippet

    #include<graphics.h>

    #define UP_ARR 72
    #define DN_ARR 80
    #define LT_ARR 75
    #define RT_ARR 77
    #define CTRL_UP_ARR 141
    #define CTRL_DN_ARR 145
    #define CTRL_LT_ARR 115
    #define CTRL_RT_ARR 116

    void main()
    {
        int gdriver=DETECT,gmode;
        int ch='0',cnt=0;
        int x1=100,y1=200,x2=300,y2=400;
        initgraph(&gdriver,&gmode,"");
        printf("Press ESC to QUIT");
        while(ch!=27 && ch!=13)
        {
            if (cnt==0)
            {
                setcolor((rand()%12)+1);
                rectangle(x1,y1,x2,y2);
                ch = getch();
                //printf("%d\n",ch);
                if (ch==UP_ARR)
                {
                    y1--; y2--;
                }
                if (ch==DN_ARR)
                {
                    y1++ ; y2++;
                }
                if (ch==LT_ARR)
                {
                    x1--; x2--;
                }
                if (ch==RT_ARR)
                {
                    x1++; x2++;
                }

                if (ch==CTRL_UP_ARR)
                {
                    y2-=2;
                }
                if (ch==CTRL_DN_ARR)
                {
                    y2+=2;
                }
                if (ch==CTRL_LT_ARR)
                {
                    x2-=2;
                }
                if (ch==CTRL_RT_ARR)
                {
                    x2+=2;
                }
                cleardevice();
            }

        }
    }

    Sunday, April 29, 2007 4:55 PM
  • #include<graphics.h>

    void pacman(int x,int y)
    {
        delay(100);
        cleardevice();
        pieslice(x,y,20,340,25);
        delay(100);
        pieslice(x,y,0,360,25);
    }


    void main()
    {
        int gdriver=DETECT,gmode;
        int x,y,stangle1,eangle1,stangle2,endangle2,r;
        int flagx=0,flagy=0;
        x = 640/2;
        y = 480/2;
        initgraph(&gdriver,&gmode,"");
        while(!kbhit())
        {
            pacman(x,y);
            if ((x+r)>=0 && (x+r)<=640 && flagx==0)
            {
                x++;
            }
            else
            {    flagx=1;
                x--;
                if ((x-r)<=0) flagx=0;
            }
            if ((y+r)>=0 && (y+r)<=480 && flagy==0)
            {
                y++;
            }
            else
            {
                flagy =1;
                y--;
                if ((y-r)<=0)  flagy=0;
            }

        }
        getch();
    }
    Sunday, April 29, 2007 4:55 PM

  • Code Snippet

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT,gmode;
        int r=20,x=100,y=150,stangle=0,eangle=0,flagx=0,flagy=0;
        x = rand()%640;
        y = rand()%480;

        initgraph(&gdriver,&gmode,"");
        while(!kbhit())
        {
            setcolor(rand());
            arc(x,y,stangle,eangle,r);
            setcolor(getbkcolor());
            delay(5);
            arc(x,y,stangle,eangle,r);
            eangle+=2;
            stangle++;
            if ((x+r)>=0 && (x+r)<=640 && flagx==0)
            {
                x++;
            }
            else
            {    flagx=1;
                x--;
                if ((x-r)<=0) flagx=0;
            }
            if ((y+r)>=0 && (y+r)<=480 && flagy==0)
            {
                y++;
            }
            else
            {
                flagy =1;
                y--;
                if ((y-r)<=0)  flagy=0;
            }
        }
        getch();
        closegraph();
    }
    Sunday, April 29, 2007 4:56 PM

  • Code Snippet

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT, gmode;
        int a=0,b=
        275;
        initgraph(&gdriver,&gmode,"");
        while(!kbhit())
        {
            setcolor(WHITE);
            ellipse(640/2,480/2,a,b,50,50);
            delay(1);
            setcolor(getbkcolor());
            ellipse(640/2,480/2,a,b,50,50);
            a++; b++;
        }
        getch();
        closegraph();
    }
    Sunday, April 29, 2007 4:56 PM

  • Code Snippet

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT, gmode;
        int x,y,xr,yr,flag=0;
        int midx,midy;
        x = 640/2;
        y = 480/2;
        xr = 100;
        yr = 50;
        initgraph(&gdriver,&gmode,"");
        while(!kbhit())
        {
            setcolor(WHITE);
            ellipse(x,y,0,360,xr,yr);
            delay(30);
            setcolor(getbkcolor());
            ellipse(x,y,0,360,xr,yr);
            if (yr <= xr && flag == 0)
            {
                yr++;
            }
            else
            {
                flag = 1;
                yr--;
                if (yr==0)
                {
                    flag = 0;
                }
            }
            if (yr <=0)
            {
                yr++;
            }
        }
        getch();
    }
    Sunday, April 29, 2007 4:56 PM

  • Code Snippet

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

    void main()
    {
        int gdriver=DETECT, gmode;
        int i=0,x1,y1,x2,y2;
        x1 = 100;
        y1 = 120;
        x2 = 100;
        y2 = 300;
        initgraph(&gdriver,&gmode,"");
        line(x1,y1,x2,y2);
        delay(1000);
        while(!kbhit() && (y1<=y2))
        {
            setcolor(WHITE);
            line(x1,y1,x2,y2);
            delay(5);
            setcolor(0);
            line(x1++,y1++,x2,y2--);
            x1+=2; x2++;
        }
        setcolor(WHITE);
        line(x1-2,y1-1,x2-1,y2+1);
        getch();
        closegraph();
    }

    Sunday, April 29, 2007 4:57 PM

  • Code Snippet

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

    void main()
    {
        int gdriver = DETECT, gmode;
        int i=200,j=200,k=0,del=10;
        int x1,y1,x2,y2,col=1;
        int a,b,c,d,size,r=0;
        printf("Enter LEFT (X-axis) : ");
        scanf("%d",&a);
        printf("Enter TOP  (Y-axis) : ");
        scanf("%d",&b);
        printf("Enter SIZE of the Square : ");
        scanf("%d",&size);

        a=a; //200
        b=b; //100
        c=a+size; //400
        d=b+size; //300

        initgraph(&gdriver,&gmode,"");

        rectangle(a,b,c,d);
        getch();
        x1 = a; //200;
        y1 = b; //100;
        x2 = c-size; //200;
        y2 = b+(size/2); //200;
        setcolor(col++);
        while(!kbhit())
        {
            delay(del);
            line(x1++,y1,x2,y2--); // upper left
            if (y1>=y2)
            {
                line(0,y1,640,y2);
                i--;
                x1 = x1; //300;
                y1 = y1; //100
                x2 = x2+size; //400;
                y2 = y2; //100;
                setcolor(col++);
                while(!kbhit())
                {
                    delay(del);
                    line(x1++,y1,x2,y2++); //lower right
                    if (x1 >=x2)
                    {
                        line(x1,0,x2,480);
                        x1 = x2; //400;
                        y1 = y2; //200;
                        x2 = x2; //400;
                        y2 = d; //300;
                        setcolor(col++);
                        while(!kbhit())
                        {
                            delay(del);
                            line(x1,y1++,x2--,y2);
                            if (y1>=y2)
                            {
                                line(0,y1,640,y2);
                                x1 = a; //200;
                                y1 = d; //300;
                                x2 = x2; //300;
                                y2 = y2; //300;
                                setcolor(col++);
                                while(!kbhit())
                                {
                                    delay(del);
                                    line(x1,y1--,x2--,y2);
                                    if (x1 >=x2)
                                    {
                                        line(x1,0,x2,480);
                                        setcolor(col++);
                                        while(!kbhit() && r<=(size/2))
                                        {
                                            if(r%2==0)
                                            {
                                                setcolor(col);
                                                circle(a+(size/2),b+(size/2),r++);
                                            }
                                            else
                                            {
                                                delay(del);
                                                setcolor(0);
                                                circle(a+(size/2),b+(size/2),r-1);
                                            }

                                        }

                                        break;
                                    }
                                }
                                break;
                            }

                        }
                        break;
                    }
                }
                break;
            }
        }
        getch();
    }
    Sunday, April 29, 2007 4:57 PM

  • Code Snippet

    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h>
    void main()
    {
        int gdriver = DETECT,gmode;
        long double i=0;
        initgraph(&gdriver,&gmode,"");
        while (!kbhit())
        {
            putpixel(rand()%640,rand()%480,rand());
            i++;
            if (i>=500000)
            {
                closegraph();
                clrscr();
                initgraph(&gdriver,&gmode,"");
                i=0;
            }




        }
        getch();
    }

    Sunday, April 29, 2007 4:57 PM

  • Code Snippet

    #include <graphics.h>

    void main()
    {          //        circle
        /* requst auto detection */
        int gdriver = DETECT, gmode;
        int maxx=639,maxy=479;
        initgraph(&gdriver, &gmode, "");
        line(0,0,maxx,0);
        line(0,0,0,maxy);
        line(maxx,0,maxx,maxy);
        line(0,maxy,maxx,maxy);
        rectangle((maxx/2)-200,(maxy/2)-200,(maxx/2)+200,(maxy/2)+(200));
        ellipse(maxx/2,maxy/2,0,360,200,100);
        ellipse(maxx/2,maxy/2,0,360,100,200);
        circle(maxx/2,maxy/2,100);
        circle(maxx/2,maxy/2,200);
        circle(maxx/2,maxy/2,50);
        line(maxx/2,(maxy/2)-200,maxx/2,(maxy/2)+200);
        line((maxx/2)-200,maxy/2,(maxx/2)+200,maxy/2);
        line((maxx/2)-(100/2)-(100/5),(maxy/2)-(100/2)-(100/5),(maxx/2)+(100/2)+(100/5),(maxy/2)+(100/2)+(100/5));
        line((maxx/2)-(100/2)-(100/5),(maxy/2)+(100/2)+(100/5),(maxx/2)+(100/2)+(100/5),(maxy/2)+(100/2)+(100/5));
        line((maxx/2)-(100/2)-(100/5),(maxy/2)+(100/2)+(100/5),(maxx/2)+(100/2)+(100/5),(maxy/2)-(100/2)-(100/5));
        line((maxx/2)-(100/2)-(100/5),(maxy/2)-(100/2)-(100/5),(maxx/2)+(100/2)+(100/5),(maxy/2)-(100/2)-(100/5));
        line((maxx/2)-(100/2)-(100/5),(maxy/2)-(100/2)-(100/5),(maxx/2)-(100/2)-(100/5),(maxy/2)+(100/2)+(100/5));
        line((maxx/2)+(100/2)+(100/5),(maxy/2)-(100/2)-(100/5),(maxx/2)+(100/2)+(100/5),(maxy/2)+(100/2)+(100/5));
        getch();
    }

    Sunday, April 29, 2007 4:57 PM
  • Code Snippet

    #include<graphics.h>

    //<-90,60,30,0/360,330,300,270,240,210,180,150,120,90->
    void main()
    {
        int gdriver=DETECT,gmode;
        int midx,midy,a=88,b=92,r=200;
    //    int p[12]={88,58,28,2,328,298,268,208,209, <--- baki hahaha
        initgraph(&gdriver,&gmode,"");
        midx = getmaxx()/2;
        midy = getmaxy()/2;
        circle(midx,midy,r);
        while(!kbhit())
        {
            setcolor(WHITE);
            circle(midx,midy,r);
            pieslice(midx,midy,b,a,r-10);
            delay(1000);
            cleardevice();
            a = a-30;
            b = b-30;

        }
        getch();
    }


    Sunday, April 29, 2007 4:58 PM

  • Code Snippet

    #include<graphics.h>

    void main()
    {
        int gdriver=DETECT, gmode;
        int x,y,xr,yr,flag=0;
        int midx,midy;
        x = 640/2;
        y = 480/2;
        xr = 100;
        yr = 50;
        initgraph(&gdriver,&gmode,"");
        while(!kbhit())
        {
            setcolor(WHITE);
    //        setfillpattern(1,5);
            ellipse(x,y,0,360,xr,yr);
            delay(30);
            setcolor(getbkcolor());
            ellipse(x,y,0,360,xr,yr);
            if (yr <= xr && flag == 0)
            {
                yr++;
            }
            else
            {
                flag = 1;
                yr--;
                if (yr==0) flag = 0;
            }
            if (yr <=0)
            {
                yr++;
            }
        }
        getch();
    }
    Sunday, April 29, 2007 4:58 PM

  • //This is CPP program for the TOWERS OF HANOI problem... Try it in TC and suggest for improvements...


     


    #include<stdio.h>
    #include<math.h>
    #include<graphics.h>
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>

    int list[3][15],start=1;
    int r,r1=5,nD,nn,timer,code1=1;
    int offset=50,step_number=0;

    void spindle(int n,int list[])
    {
     setcolor(BLACK);
     setlinestyle(SOLID_LINE,1,3);

     int n1=n;

     if(n!=0)
      r=(102/n);
     else
      n=nn;
     int w=(n*r);
     setcolor(WHITE);
     moveto(offset-10,300);
     lineto(offset+w+10,300);
     moveto(offset+w/2,300);

     int h=(n*r)+50;

     lineto(offset+w/2,300-h);

     int mf=n-8;

     if(n<=11)
      mf=20;
     int i,x;
     if(nD>11)
      x=r+n-mf-13;
     else
      x=r+n-mf-7;

     int bottom=299,right,left,top,y=300-r1-5;
     int color=1;

     for(i=0;i<n1;i++)
     {
      setfillstyle(SOLID_FILL , color);
      setcolor(color);
      right=offset+w-x,left=offset+x,top=y;
      if(list[i]!=0)
      {
       bar(left,top,right,bottom);
       bottom=top-1;
       y-=r1+5;
      }
      x+=15-n;
      color++;
      if(color==7)
       color++;
     }
     offset+=210;
    }


    void animate(int n)
    {
     clrscr();
     if(code1==1)
      cout<<"To exit press 'e',  else press any key to continue\n ";
     cout<<"Step number : "<<step_number++;
     offset=50;
     spindle(n,list[0]);
     spindle(n,list[1]);
     spindle(n,list[2]);
     if(start==1)
     {
      cout<<"\n\nPress any key to start ";
      getch();
      start++;
     }

     char ch;

     for(timer=0;timer<15000;timer++)
      printf(" \b");
     if(code1==1)
      ch=getch();
     if(ch=='e'||ch=='E')
      exit(0);
    }

    void solve(int n,int source,int dest,int temp)
    {
     if(n==2)
     {
      list[source-1][nD-n+1]=0;
      list[temp-1][nD-n+1]=1;
      animate(nD);
      list[source-1][nD-n]=0;
      list[dest-1][nD-n]=1;
      animate(nD);
      list[temp-1][nD-n+1]=0;
      list[dest-1][nD-n+1]=1;
      animate(nD);
     }
     else
     {
      solve((n-1),source,temp,dest);
      list[source-1][nD-n]=0;
      list[dest-1][nD-n]=1;
      animate(nD);
      solve((n-1),temp,dest,source);
     }
     return;
    }


    void main()
    {
     int n,i,j;
    start:
     int g_driver,g_mode;
     step_number=0;
     r1=5;
     start=1;
     code1=1;
     offset=50;
     g_driver=DETECT;
    // g_mode=EGAHI;
     setgraphmode(EGAHI);
     initgraph(&g_driver,&g_mode,"C:\\tc\\bin\\");
     setbkcolor(BLUE);
     clrscr();
     cout<<"Enter the number of disks : ";
     cin>>n;
     nD=nn=n;
     for(i=0;i<15;i++)
      list[0][i]=list[1][i]=list[2][i]=0;
     for(i=0,j=n;i<n;i++,j--)
      list[0][i]=n;
     if(n<2||n>14)
     {
      cout<<"Enter a number in the range 2 to 14";
      fflush(stdin);
      getch();
      return;
     }
     clrscr();
     spindle(n,list[0]);
     spindle(n,list[1]);
     spindle(n,list[2]);
     offset=50;
        codestart:
     clrscr();
     cout<<"\nTotal number of steps = "<<pow(2,n)-1;
     cout<<"\n1.To view as animation Press 'a'\n2.To view as single move steps Press 'n'\nEnter a code(a,n): ";
     char codec;
     cin>>codec;
     if(codec!='a'&&codec!='A'&&codec!='n'&&codec!='N')
     {
      goto codestart;
     }
     if(codec=='n' || codec =='N')
      code1=1;
     else
      code1=2;
     animate(nD);
     solve(n,1,3,2);
     printf("\n\t\t\t\tEND OF MOVES");
     gotoxy(20,20);
     printf("Do you want to continue ?:");
     codec=getch();
     if(codec=='Y'||codec=='y')
      goto start;
    }


    Monday, May 21, 2007 1:04 AM
  • sir

             i am an computer engineering student i am very interested to study cgraphics.so please sent me coding for opening image using c.

     

    Sunday, November 18, 2007 3:15 AM
  • //Rotating a Wheel

    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h>
    #include<math.h>
    #define PI 3.1415

    void rotate_wheel(int xc,int yc,int t)
    {
     int x,y;
     for(t=t;t<180;t=t+60)
     {
      x=50*cos

    (t*PI/180);
      y=50*sin

    (t*PI/180);
      line

    (xc+x,yc+y,xc-x,yc-y);
     }
     circle(xc,yc,50);
     circle(xc,yc,52);
    }

    main()
    {
     int d=0,m=0,x;
     initgraph(&d,&m,"c:\\tc");

     for(x=0;x<640;x++)
     {
      setcolor(BLUE);
      rotate_wheel

    (x,240,x%60);
      delay(5);
      cleardevice();
      rotate_wheel

    (x,240,x%60);
     }
     getch();
     closegraph();
    }

    Wednesday, November 21, 2007 9:51 AM
  • //Clipping a Line
    #include<graphics.h>
    #include<conio.h>
    #include<iostream.h>
    void main()
    {int gd=DETECT,gm;
     initgraph(&gd,&gm,"C:\\TC\\BGI");
     int x1,y1,x2,y2,xmin=100,xmax=500,ymin=100,ymax=300;
     cout<<"Enter points "; cin>>x1>>y1>>x2>>y2;
     setcolor(12); float m=1;
     if(x1!=x2)  m=(y2-y1)/(x2-x1);
     rectangle(xmin,ymin,xmax,ymax);
     floodfill(150,150,12);
     if(x2==x1)
       {if(y1>ymax)  y1=ymax-1;
        if(y1<ymin)  y1=ymin+1;
        if(y2>ymax)  y2=ymax-1;
        if(y2<ymin)  y2=ymin+1;
       }
     if(x1<xmin)
       {y1=y1+m*(xmin-x1);
        x1=xmin;
       }
     if(x2<xmin)
       {y2=y2-m*(xmin-x2);
        x2=xmin;
       }
     if(x1>xmax)
       {y1=y1+m*(x1-xmax);
        x1=xmax;
       }
     if(x2>xmax)
       {y2=y2-m*(x2-xmax);
        x2=xmax;
       }
     if(y1<ymin)
       {x1=x1+int((ymin-y1)/m);
        y1=ymin;
       }
     if(y2<ymin)
       {x2=x2-int((ymin-y2)/m);
        y2=ymin;
       }
     if(y1>ymax)
       {x1=x1+int((y1-ymax)/m);
        y1=ymax;
       }
     if(y2>ymax)
       {x2=x2-int((y2-ymax)/m);
        y2=ymax;
       }        setcolor(0);
     line(x1,y1,x2,y2);
     getch();
     closegraph();
     restorecrtmode();
    }

    Wednesday, November 21, 2007 9:53 AM
  • //Flood Fill Algorithm.

     

     # include <iostream.h>
     # include <graphics.h>
     # include    <conio.h>
     # include     <math.h>

     

     void show_screen( );

     void Flood_fill(const int,const int,const int,const int);

     void Circle(const int,const int,const int);
     void Triangle(const int,const int,const int,const int,const int,const int);
     void Rectangle(const int,const int,const int,const int);
     void Polygon(const int,const int []);
     void Line(const int,const int,const int,const int);

     

     int main( )
        {
           int gd=DETECT,gm;

           initgraph(&gd,&gm,"..\\Bgi");

           show_screen( );

           setcolor(15);
      Circle(175,175,40);

      Flood_fill(175,175,10,0);

           setcolor(15);
           settextstyle(0,0,1);
      outtextxy(150,225,"Circle");

           setcolor(15);
      Rectangle(375,145,475,205);

      Flood_fill(400,175,9,0);

           setcolor(15);
           settextstyle(0,0,1);
      outtextxy(390,215,"Rectangle");

           setcolor(15);
      Triangle(135,360,215,360,175,290);

      Flood_fill(175,325,8,0);

           setcolor(15);
           settextstyle(0,0,1);
      outtextxy(145,370,"Triangle");

           int polygon_points[14]={ 365,325, 400,290, 450,290, 485,325,
          450,360, 400,360, 365,325 };

           setcolor(15);
      Polygon(7,polygon_points);

      Flood_fill(425,325,12,0);

           setcolor(15);
           settextstyle(0,0,1);
      outtextxy(395,370,"Polygon");

           getch( );
           return 0;
        }

     

     void Flood_fill(const int x,const int y,
           const int fill_color,const int old_color)
        {
           if(getpixel(x,y)==old_color)
       {
          putpixel(x,y,fill_color);

          Flood_fill((x+1),y,fill_color,old_color);
          Flood_fill((x-1),y,fill_color,old_color);
          Flood_fill(x,(y+1),fill_color,old_color);
          Flood_fill(x,(y-1),fill_color,old_color);
       }
        }

     

     void Circle(const int h,const int k,const int r)
        {
           int color=getcolor( );

           int x=0;
           int y=r;
           int p=(1-r);

           do
       {
          putpixel((h+x),(k+y),color);
          putpixel((h+y),(k+x),color);
          putpixel((h+y),(k-x),color);
          putpixel((h+x),(k-y),color);
          putpixel((h-x),(k-y),color);
          putpixel((h-y),(k-x),color);
          putpixel((h-y),(k+x),color);
          putpixel((h-x),(k+y),color);

          x++;

          if(p<0)
      p+=((2*x)+1);

          else
      {
         y--;
         p+=((2*(x-y))+1);
      }
       }
           while(x<=y);
        }

     

     void Triangle(const int x_1,const int y_1,const int x_2,const int y_2,
          const int x_3,const int y_3)
        {
           Line(x_1,y_1,x_2,y_2);
           Line(x_2,y_2,x_3,y_3);
           Line(x_3,y_3,x_1,y_1);
        }

     

     

     void Rectangle(const int x_1,const int y_1,const int x_2,const int y_2)
        {
           Line(x_1,y_1,x_2,y_1);
           Line(x_2,y_1,x_2,y_2);
           Line(x_2,y_2,x_1,y_2);
           Line(x_1,y_2,x_1,y_1);
        }

     

     

     void Polygon(const int n,const int coordinates[])
        {
           if(n>=2)
       {
          Line(coordinates[0],coordinates[1],
              coordinates[2],coordinates[3]);

          for(int count=1;count<(n-1);count++)
      Line(coordinates[(count*2)],coordinates[((count*2)+1)],
             coordinates[((count+1)*2)],
             coordinates[(((count+1)*2)+1)]);
       }
        }

     

     

     void Line(const int x_1,const int y_1,const int x_2,const int y_2)
        {
           int color=getcolor( );

           int x1=x_1;
           int y1=y_1;

           int x2=x_2;
           int y2=y_2;

           if(x_1>x_2)
       {
          x1=x_2;
          y1=y_2;

          x2=x_1;
          y2=y_1;
       }

           int dx=abs(x2-x1);
           int dy=abs(y2-y1);
           int inc_dec=((y2>=y1)?1:-1);

           if(dx>dy)
       {
          int two_dy=(2*dy);
          int two_dy_dx=(2*(dy-dx));
          int p=((2*dy)-dx);

          int x=x1;
          int y=y1;

          putpixel(x,y,color);

          while(x<x2)
      {
         x++;

         if(p<0)
            p+=two_dy;

         else
            {
        y+=inc_dec;
        p+=two_dy_dx;
            }

         putpixel(x,y,color);
      }
       }

           else
       {
          int two_dx=(2*dx);
          int two_dx_dy=(2*(dx-dy));
          int p=((2*dx)-dy);

          int x=x1;
          int y=y1;

          putpixel(x,y,color);

          while(y!=y2)
      {
         y+=inc_dec;

         if(p<0)
            p+=two_dx;

         else
            {
        x++;
        p+=two_dx_dy;
            }

         putpixel(x,y,color);
      }
       }
        }

     

     void show_screen( )
        {
           setfillstyle(1,1);
      bar(230,26,405,38);

           settextstyle(0,0,1);
      setcolor(15);
        outtextxy(5,5,"******************************************************************************");
        outtextxy(5,17,"*-**************************************************************************-*");
        outtextxy(5,29,"*--------------------------                        --------------------------*");
        outtextxy(5,41,"*-**************************************************************************-*");
        outtextxy(5,53,"*-**************************************************************************-*");

      setcolor(11);
        outtextxy(237,29,"Flood Fill Algorithm");

      setcolor(15);

        for(int count=0;count<=30;count++)
           outtextxy(5,(65+(count*12)),"*-*                                                                        *-*");

        outtextxy(5,438,"*-**************************************************************************-*");
        outtextxy(5,450,"*-------------------------                          -------------------------*");
        outtextxy(5,462,"******************************************************************************");

      setcolor(12);
        outtextxy(229,450,"Press any Key to exit.");
        }

    Wednesday, November 21, 2007 9:58 AM