c programming Coding

c program to restrict mouse pointer in a circle

c program to restrict mouse pointer in a circle - c programming - When you bring mouse pointer outside circle, mouse pointer is moved to previous location

This program restricts mouse pointer in a circle i.e you can’t move mouse out of a circle. When you try to bring mouse pointer outside the circle, mouse pointer is moved to it’s previous location which is inside the circle. Code to restrict mouse in circle is given below :-

C programming code

#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, "C:\\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;
}

 

See also  C++ Programming - Given a matrix of ‘O’ and ‘X’ and find the largest subsquare surrounded by ‘X’

About the author

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment