Search This Blog

Loading...

C program to restrict mouse pointer in a circle

Below is the C program to restrict mouse pointer in a circle. Here we have used below functions to achieve it.

int initmouse() : function is used to intialize the mouse pointer. Which returns int value. Value 0 indicates Mouse support is not available.

void showmousepointer() : function is used to show mouse pointer. Which does not return any value.

void getmouseposition(int *button, int *x, int *y) : function to get the position of check the position of the mouse pointer. Also it sets the 'button' variable value to 1 or 2, which identifies the which mouse button is clicked.

int kbhit() : Function is used to identify that is key is pressed or not.

void circle (int x,int y,int radius) : circle function is used to draw a circle with center (x, y).

Void closegraph() : Function is used to deallocate memory of graphics system.

#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 showmousepointer()
{
   i.x.ax = 1;
   int86(0X33, &i, &o);
}

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

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

}

void movemousepointer(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, "C:\\TC\\BGI");

   if(!initmouse())
   {
      closegraph();
      exit(1);
   }

   midx = getmaxx()/2;
   midy = getmaxy()/2;

   showmousepointer();
   movemousepointer(midx, midy);
   circle(midx, midy, radius);

   x = tempx = midx;
   y = tempy = midy;

   while(!kbhit())
   {
      getmouseposition(&x, &y);

      if((pow(x-midx,2)+pow(y-midy,2)-pow(radius,2))>0)
      {
         movemousepointer(tempx, tempy);
         x = tempx;
         y = tempy;
      }

      tempx = x;
      tempy = y;
   }

   closegraph();
   return 0;
}

No comments:

Post a Comment