Thread: do it by mouse

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    Question do it by mouse

    I succesfuly do it ;But i want to do it by mouse what can i do?
    Code:
    /*- Line clipping using cohen sutherland algo -*/
    /*------------------------------------------------------------*/
    
    	#include<stdio.h>
    	#include<graphics.h>
    
    	typedef unsigned int outcode;
    	enum { 	TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
    
    	void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
    	float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
    	{
    
    	   int gd,gm;
    	   outcode code0,code1,codeout;
    	   int accept = 0, done=0;
    
    	   code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
    	   code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
    
    	   do{
    		if(!(code0 | code1))
    		{	accept =1 ; done =1; }
    		else
    		 if(code0 & code1)     done = 1;
    		 else
    		 {
    		    float x,y;
    		    codeout = code0 ? code0 : code1;
    		    if(codeout & TOP)
    		    {
    			x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
    			y = ywmax;
    		    }
    		    else
    		      if( codeout & BOTTOM)
    		      {
    			 x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
    			 y = ywmin;
    		      }
    		      else
    			if ( codeout & RIGHT)
    			{
    			   y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
    			   x = xwmax;
    			}
    			else
    			{
    			    y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
    			    x = xwmin;
    			 }
    		 if( codeout == code0)
    		 {
    		     x0 = x;  y0 = y;
    		     code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
    		 }
    		 else
    		 {
    		    x1 = x;   y1 = y;
    		    code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
    		 }
    	      }
    	   } while( done == 0);
    
    	   if(accept)  line(x0,y0,x1,y1);
    
    	   rectangle(xwmin,ywmin,xwmax,ywmax);
    
    	   getch();
    
    }
    /*--------------------------------------------------------------------*/
    
     int  calcode (x,y,xwmin,ywmin,xwmax,ywmax)
     float  x,y,xwmin,ywmin,xwmax,ywmax;
     {
        int  code =0;
    
        if(y> ywmax)
    	code |=TOP;
        else if( y<ywmin)
    	code |= BOTTOM;
        else if(x > xwmax)
    	code |= RIGHT;
        else if ( x< xwmin)
    	code |= LEFT;
    
        return(code);
    }
    
    /*-------------------------------------------------*/
    
    main()
    {
    
    	float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
    	int gd,gm;
    
    	detectgraph(&gd,&gm);
    	initgraph(&gd,&gm,"C:\\TC\\BGI");
    
    	printf("\n\n\tEnter the co-ordinates of Line :");
    
    	printf("\n\n\tX1  Y1  :  ");
    	scanf("%f %f",&x1,&y1);
    
    	printf("\n\n\tX2  Y2  :  ");
    	scanf("%f %f",&x2,&y2);
    
    
    	printf("\n\tEnter the co_ordinates of window  :\n ");
    	printf("\n\txwmin , ywmin  : ");
    	scanf("%f %f",&xwmin,&ywmin);
    	printf("\n\txwmax , ywmax  : ");
    	scanf("%f %f",&xwmax,&ywmax);
    
    	line(x1,y1,x2,y2);
    	rectangle(xwmin,ywmin,xwmax,ywmax);
    	getch();
    	cleardevice();
    
    	lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
    	getch();
    	closegraph();
    
    }
    
    /***************** COHEN-SUTHERLAND LINE CLIPPING ***************/
    AbHHinaay

  2. #2
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    what are you tryign to do?
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Do what by mouse? Clip lines?

    My guess is you are creating a program that allows you to draw lines - clipping them to a viewport or specified drawing region.

    For mouse support you only need to implement about 4 of the many sub-functions available on interrupt 33h.


    Code:
    struct tagMouse
    {
       int x;
       int y;
       int button_down;
    }Mouse;
    
    
    void ResetMouse(void)
    {
      asm {
        mov     ax,0000h
        int       33h
      }
      Mouse.x=0;
      Mouse.y=0;
      Mouse.button_down=0;
    }
    
    void ShowMouse(void)
    {
      asm {
        mov      ax,0001h
        int        33h
      }
    }
    
    void HideMouse(void)
    {
       asm {
         mov     ax,0002h
         int       33h
       }
    }
    
    void PutMouse(int x,int y)
    {
      asm {
        mov    ax,0004h
        mov    cx,[x]
        mov    dx,[y]
        int      33h
      }
      Mouse.x=x;
      Mouse.y=y;
    }
    
    void GetMouse(void)
    {
        asm  {
          mov    ax,0005h
          int      33h
        }
        Mouse.x=_CX;   //Divide by 2 if in 320x200x256 mode
        Mouse.y=_DX;
        Mouse.button_down=_BX;
    }
    That's really all there is to using the MS mouse. Just call the function you need to accomplish what you are trying to do.

  4. #4
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    I know all that function ,But i can't use them properly.
    The line is not drawn from that function.
    AbHHinaay

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    struct point2d
    {
       int x;
       int y;
    };
    
    point2d pt1;
    point2d pt2;
    
    if (mouse.button==1)
    {
      //...save x and y coords.
      pt1.x=mouse.x;
      pt1.y=mouse.y;
    }
    
    do
    {
      pt2.x=mouse.x;
      pt2.y=mouse.y
    
      Line(pt1.x,pt1.y,pt2.x,pt2.y);
      //Either double buffer here or erase the previous line
    
    } while (mouse.button==1);
    This code will get the first coords, save them, and will draw a line to the current mouse position as long as the first button or LMB is held down. When the mouse button is released the line is permanent.

    To make the line permanent you need to store the starting and ending coords of the line and redraw that every frame.

    For a drawing program you could store the lines in a linked list and simply iterate through the linked list every frame. This would 'save' the lines that the user is drawing. When the mouse button is released, add the new starting and ending coords to the list and the line will become permanent.

    This is usually how paint programs will have you draw lines - click, hold and drag, release.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need Help in Mouse Pointer
    By obaid in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2006, 03:33 AM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  3. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 02:10 AM
  4. Making a mouse hover button, API style
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2004, 06:17 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM