Tuesday, 24 January 2012

Mouse Pointer Restricted in Circle

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

union REGS i, o;

int initmouse()
{
    i.x.ax = 0;
    int86(0X33, &i, &o);
    return ( o.x.ax );
}

void showmouseptr()
{
    i.x.ax = 1;
    int86(0X33, &i, &o);
}

void hidemopuseptr()
{
    i.x.ax = 2;
    int86(0X33,&i,&o);
}

void getmousepos(int *x, int *y)
{
    i.x.ax = 3;
    int86(0X33, &i, &o);
    *x = o.x.cx;
    *y = o.x.dx;
}

void movemouseptr(int x, int y)
{
    i.x.ax = 4;
    i.x.cx = x;
    i.x.dx = y;
    int86(0X33, &i, &o);
}

main()
{
    int gd = DETECT, gm, midx, midy, radius, x, y, tempx, tempy;
    radius = 100;
    initgraph(&gd, &gm, "D:\\TC\\BGI");
    if(!initmouse())
    {
        closegraph();
        exit(1);
    }
    midx = getmaxx()/2;
    midy = getmaxy()/2;
    showmouseptr();
    movemouseptr(midx, midy);
    circle(midx, midy, radius);
    x = tempx = midx;
    y = tempy = midy;
    while(!kbhit())
    {
        getmousepos(&x, &y);
        if((pow(x-midx,2)+pow(y-midy,2)-pow(radius,2))>0)
        {
            movemouseptr(tempx, tempy);
            x = tempx;
            y = tempy;
        }
        tempx = x;
        tempy = y;
    }
    closegraph();
    return 0;
}

Complex Number Operation

#include <stdio.h>
#include <stdlib.h>

struct complex
{
    int real, img;
};

main()
{
    int choice, temp1, temp2, temp3;
    struct complex a, b, c;
    while(1)
    {
        printf("Press 1 to add two complex numbers.\n");
        printf("Press 2 to subtract two complex numbers.\n");
        printf("Press 3 to multiply two complex numbers.\n");
        printf("Press 4 to divide two complex numbers.\n");
        printf("Press 5 to exit.\n");
        printf("Enter your choice ");
        scanf("%d",&choice);
        if( choice == 5)
        exit(0);
        if(choice >= 1 && choice <= 4)
        {
            printf("Enter a and b where a + ib is the first complex number.");
            printf("\na = ");
            scanf("%d", &a.real);
            printf("b = ");
            scanf("%d", &a.img);
            printf("Enter c and d where c + id is the second complex number.");
            printf("\nc = ");
            scanf("%d", &b.real);
            printf("d = ");
            scanf("%d", &b.img);
        }
        if ( choice == 1 )
        {
            c.real = a.real + b.real;
            c.img = a.img + b.img;
            if ( c.img >= 0 )
            printf("Sum of two complex numbers = %d + %di",c.real,c.img);
            else
            printf("Sum of two complex numbers = %d %di",c.real,c.img);
        }
        else if ( choice == 2 )
        {
            c.real = a.real - b.real;
            c.img = a.img - b.img;
            if ( c.img >= 0 )
            printf("Difference of two complex numbers = %d + %di",c.real,c.img);
            else
            printf("Difference of two complex numbers = %d %di",c.real,c.img);
        }
        else if ( choice == 3 )
        {
            c.real = a.real*b.real - a.img*b.img;
            c.img = a.img*b.real + a.real*b.img;
            if ( c.img >= 0 )
            printf("Multiplication of two complex numbers = %d + %di",c.real,c.img);
            else
            printf("Multiplication of two complex numbers = %d %di",c.real,c.img);
        }
        else if ( choice == 4 )
        {
            if ( b.real == 0 && b.img == 0 )
            printf("Division by 0 + 0i is not allowed.");
            else
            {
                temp1 = a.real*b.real + a.img*b.img;
                temp2 = a.img*b.real - a.real*b.img;
                temp3 = b.real*b.real + b.img*b.img;
                if ( temp1%temp3 == 0 && temp2%temp3 == 0 )
                {
                    if ( temp2/temp3 >= 0)
                    printf("Division of two complex numbers = %d + %di",temp1/temp3,temp2/temp3);
                    else
                    printf("Division of two complex numbers = %d %di",temp1/temp3,temp2/temp3);
                }
                else if ( temp1%temp3 == 0 && temp2%temp3 != 0 )
                {
                    if ( temp2/temp3 >= 0)
                    printf("Division of two complex numbers = %d + %d/%di",temp1/temp3,temp2,temp3);
                    else
                    printf("Division of two complex numbers = %d %d/%di",temp1/temp3,temp2,temp3);
                }
                else if ( temp1%temp3 != 0 && temp2%temp3 == 0 )
                {
                    if ( temp2/temp3 >= 0)
                    printf("Division of two complex numbers = %d/%d + %di",temp1,temp3,temp2/temp3);
                    else
                    printf("Division of two complex numbers = %d %d/%di",temp1,temp3,temp2/temp3);
                }
                else
                {
                    if ( temp2/temp3 >= 0)
                    printf("Division of two complex numbers = %d/%d + %d/%di",temp1,temp3,temp2,temp3);
                    else
                    printf("Division of two complex numbers = %d/%d %d/%di",temp1,temp3,temp2,temp3);
                }

            }

        }
        else
        printf("Invalid choice.");
        printf("\nPress any key to enter choice again...\n");
    }
}

Traffic light Simulation

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

main()
{
    int gd = DETECT, gm, midx, midy;
    initgraph(&gd, &gm, "D:\\TC\\BGI");
    midx = getmaxx()/2;
    midy = getmaxy()/2;
    setcolor(RED);
    settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
    settextjustify(CENTER_TEXT, CENTER_TEXT);
    outtextxy(midx, midy-10, "Traffic Light Simulation");
    outtextxy(midx, midy+10, "Press any key to start");
    getch();
    cleardevice();
    setcolor(WHITE);
    settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
    rectangle(midx-30,midy-80,midx+30,midy+80);
    circle(midx, midy-50, 22);
    setfillstyle(SOLID_FILL,RED);
    floodfill(midx, midy-50,WHITE);
    setcolor(BLUE);
    outtextxy(midx,midy-50,"STOP");
    delay(2000);
    graphdefaults();
    cleardevice();
    setcolor(WHITE);
    rectangle(midx-30,midy-80,midx+30,midy+80);
    circle(midx, midy, 20);
    setfillstyle(SOLID_FILL,YELLOW);
    floodfill(midx, midy,WHITE);
    setcolor(BLUE);
    outtextxy(midx-18,midy-3,"READY");
    delay(2000);
    cleardevice();
    setcolor(WHITE);
    rectangle(midx-30,midy-80,midx+30,midy+80);
    circle(midx, midy+50, 22);
    setfillstyle(SOLID_FILL,GREEN);
    floodfill(midx, midy+50,WHITE);
    setcolor(BLUE);
    outtextxy(midx-7,midy+48,"GO");
    setcolor(RED);
    delay(2000);
    settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);
    outtextxy(midx-150, midy+100, "Press any key to exit...");
    getch();
    closegraph();
    return 0;
}

Press Me Button Game

You have to change the directory where your TC folder exist....
Change the directory from the red line which is shown below..

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

union REGS i, o;
int left = 265, top = 250;

void initialize_graphics_mode()
{
    int gd = DETECT, gm, error;
    initgraph(&gd,&gm,"D:\\TC\\BGI");
    error = graphresult();
    if( error != grOk )
    {
        perror("Error ");
        printf("Press any key to exit...\n");
        getch();
        exit(EXIT_FAILURE);
    }
}

void showmouseptr()
{
    i.x.ax = 1;
    int86(0x33,&i,&o);
}

void hidemouseptr()
{
    i.x.ax = 2;
    int86(0x33,&i,&o);
}

void getmousepos(int *x,int *y)
{
    i.x.ax = 3;
    int86(0x33,&i,&o);
    *x = o.x.cx;
    *y = o.x.dx;
}

void draw_bar()
{
    hidemouseptr();
    setfillstyle(SOLID_FILL,CYAN);
    bar(190,180,450,350);
    showmouseptr();
}

void draw_button(int x, int y)
{
    hidemouseptr();
    setfillstyle(SOLID_FILL,MAGENTA);
    bar(x,y,x+100,y+30);
    moveto(x+5,y);
    setcolor(YELLOW);
    outtext("Press me");
    showmouseptr();
}

void draw()
{
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
    outtextxy(155,451,"dipanjan1990.blogspot.com");
    setcolor(BLUE);
    rectangle(0,0,639,450);
    setcolor(RED);
    outtextxy(160,25,"Try to press the \"Press me\" button");
    outtextxy(210,50,"Press escape key to exit");
    setfillstyle(XHATCH_FILL,GREEN);
    setcolor(BLUE);
    bar(1,1,75,449);
    bar(565,1,638,449);
    showmouseptr();
    draw_bar();
    draw_button(left,top);
}

void initialize()
{
    initialize_graphics_mode();
    if( !initmouse() )
    {
        closegraph();
        printf("Unable to initialize the mouse");
        printf("Press any key to exit...\n");
        getch();
        exit(EXIT_SUCCESS);
    }
    draw();
}

int initmouse()
{
    i.x.ax = 0;
    int86(0x33,&i,&o);
    return ( o.x.ax );
}

void get_input()
{
    int x, y;
    while(1)
    {
        getmousepos(&x,&y);
        /* mouse pointer in left of button */
        if( x >= (left-3) && y >= (top-3) && y <= (top+30+3) && x < left )
        {
            draw_bar();
            left = left + 4;
            if( left > 350 )
            left = 190;
            draw_button(left,top);
        }
        /* mouse pointer in right of button */
        else if( x <= (left+100+3) && y >= (top-3) && y <= (top+30+3) && x>(left+100))
        {
            draw_bar();
            left = left - 4;
            if( left < 190 )
            left = 350;
            draw_button(left,top);
        }
        /* mouse pointer above button */
        else if( x >= (left-3) && y >= (top-3) && y < (top) && x<=(left+100+3))
        {
            draw_bar();
            top = top + 4;
            if( top > 320 )
            top = 180;
            draw_button(left,top);
        }
        /* mouse pointer below button */
        else if( x >= (left-3) && y > (top+30) && y <= (top+30+3) && x<=(left+100+3))
        {
            draw_bar();
            top = top - 4;
            if( top < 180 )
            top = 320;
            draw_button(left,top);
        }
        if(kbhit())
        {
            if( getkey() == 1 )
            exit(EXIT_SUCCESS);
        }
    }
}

int getkey()
{
    i.h.ah = 0;
    int86(22,&i,&o);
    return( o.h.ah );
}

main()
{
    initialize();
    get_input();
    return 0;
}

Paint program

You have to change the directory where your TC folder exist....
Change the directory from the red line which is shown below..

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

union REGS i,o;
int leftcolor[15];

int get_key()
{
    union REGS i,o;
    i.h.ah = 0;
    int86(22,&i,&o);
    return ( o.h.ah );
}

void draw_color_panel()
{
    int left, top, c, color;
    left = 100;
    top = 436;
    color = getcolor();
    setcolor(GREEN);
    rectangle(4,431,635,457);
    setcolor(RED);
    settextstyle(TRIPLEX_FONT,0,2);
    outtextxy(10,431,"Colors : ");
    for( c = 1 ; c <= 15 ; c++ )
    {
        setfillstyle(SOLID_FILL, c);
        bar(left, top, left+16, top+16);
        leftcolor[c-1] = left;
        left += 26;
    }
    setcolor(color);
}

void draw_shape_panel()
{
    int left, top, c, color;
    left = 529;
    top = 45;
    color = getcolor();
    setcolor(GREEN);
    rectangle(525,40,633,255);
    for( c = 1 ; c <= 7 ; c++ )
    {
        rectangle(left, top, left+100, top+25);
        top += 30;
    }
    setcolor(RED);
    outtextxy(530,45,"Bar");
    outtextxy(530,75,"Line");
    outtextxy(530,105,"Pixel");
    outtextxy(530,135,"Ellipse");
    outtextxy(530,165,"Freehand");
    outtextxy(530,195,"Rectangle");
    outtextxy(530,225,"Clear");
    setcolor(color);
}

void change_color(int x, int y)
{
    int c;
    for( c = 0 ; c <= 13 ; c++ )
    {
        if( x > leftcolor[c] && x < leftcolor[c+1] && y > 437 && y < 453 )
        setcolor(c+1);
        if( x > leftcolor[14] && x < 505 && y > 437 && y < 453 )
        setcolor(WHITE);
    }
}

char change_shape(int x, int y)
{
    if ( x > 529 && x < 625 && y > 45 && y < 70 )
    return 'b';
    else if ( x > 529 && x < 625 && y > 75 && y < 100 )
    return 'l';
    else if ( x > 529 && x < 625 && y > 105 && y < 130 )
    return 'p';
    else if ( x > 529 && x < 625 && y > 135 && y < 160 )
    return 'e';
    else if ( x > 529 && x < 625 && y > 165 && y < 190 )
    return 'f';
    else if ( x > 529 && x < 625 && y > 195 && y < 220 )
    return 'r';
    else if ( x > 529 && x < 625 && y > 225 && y < 250 )
    return 'c';
}

void showmouseptr()
{
    i.x.ax = 1;
    int86(0x33,&i,&o);
}

void hidemouseptr()
{
    i.x.ax = 2;
    int86(0x33,&i,&o);
}

void restrictmouseptr( int x1, int y1, int x2, int y2)
{
    i.x.ax = 7;
    i.x.cx = x1;
    i.x.dx = x2;
    int86(0x33,&i,&o);
    i.x.ax = 8;
    i.x.cx = y1;
    i.x.dx = y2;
    int86(0x33,&i,&o);
}

void getmousepos(int *button,int *x,int *y)
{
    i.x.ax = 3;
    int86(0x33,&i,&o);
    *button = o.x.bx;
    *x = o.x.cx;
    *y = o.x.dx;
}

main()
{
    int gd = DETECT,gm;
    int maxx,maxy,x,y,button,prevx,prevy,temp1,temp2,key,color;
    char ch = 'f' ;            // default free-hand drawing
    initgraph(&gd,&gm,"D:\\TC\\BGI");
    maxx = getmaxx();
    maxy = getmaxy();
    setcolor(BLUE);
    rectangle(0,0,maxx,maxy);
    setcolor(WHITE);
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
    outtextxy(maxx/2-180,maxy-28,"Dipanjan Roy Chowdhury");
    draw_color_panel();
    draw_shape_panel();
    setviewport(1,1,maxx-1,maxy-1,1);
    restrictmouseptr(1,1,maxx-1,maxy-1);
    showmouseptr();
    rectangle(2,2,518,427);
    setviewport(1,1,519,428,1);
    while(1)
    {
        if(kbhit())
        {
            key = get_key();
            if( key == 1 )
            {
                closegraph();
                exit(0);
            }
        }
        getmousepos(&button,&x,&y);
        if( button == 1 )
        {
            if( x > 4 && x < 635 && y > 431 && y < 457 )
            change_color( x, y );
            else if(x>529 && x<625 && y>40 && y<250)
            ch = change_shape( x, y );
            temp1 = x ;
            temp2 = y ;
            if ( ch == 'f' )
            {
                hidemouseptr();
                while( button == 1 )
                {
                    line(temp1,temp2,x,y);
                    temp1 = x;
                    temp2 = y;
                    getmousepos(&button,&x,&y);
                }
                showmouseptr();
            }
            while( button == 1)
            getmousepos(&button,&x,&y);
            /* to avoid interference of mouse while drawing */
            hidemouseptr();
            if( ch == 'p')
            putpixel(x,y,getcolor());
            else if ( ch == 'b' )
            {
                setfillstyle(SOLID_FILL,getcolor());
                bar(temp1,temp2,x,y);
            }
            else if ( ch == 'l')
            line(temp1,temp2,x,y);
            else if ( ch == 'e')
            ellipse(temp1,temp2,0,360,abs(x-temp1),abs(y-temp2));
            else if ( ch == 'r' )
            rectangle(temp1,temp2,x,y);
            else if ( ch == 'c' )
            {
                ch = 'f';   // setting to freehand drawing
                clearviewport();
                color = getcolor();
                setcolor(WHITE);
                rectangle(2,2,518,427);
                setcolor(color);
            }
            showmouseptr();
        }
    }
}