Coder's Guild Mailing List

Re: Need algorithm help

Posted by Frank Hale on 1999-08-29

> >Okay what I am doing is designing a menu. What I
> need
> >help with is determining when the pointer is over
> an
> >item.
> 
> // GIVEN THE FOLLOWING INFORMATION....
> int pointer_x, pointer_y;
> int item_x, item_y;
> int item_width, item_height;
> 
> // DEFINE THE RECTANGULAR SPACE FOR THE ITEM....
> int item_xpos2, item_ypos2
> item_xpos2 = item_xpos + item_width;
> item_ypos2 = item_ypos + item_height;
> 
> // NOW, JUST CHECK FOR POINTER BEING INSIDE....
> if ( pointer_xpos >= item_xpos )
>   if ( pointer_xpos < item_xpos2 )
>     if ( pointer_ypos >= item_ypos )
>       if ( pointer_ypos < item_ypos2 )
>         printf("COLLISION HERE!!/n");
> 
> 
> You almost had it.  You were not taking into account
> the width/height plus
> base position.
> 
> Hope it helps.

Yes it helped out alot.

This is what I have now. I had to tweak it a little

BaseMenuItem *BaseMenu::findMenuItem(int x, int y)
{
	int item_xpos2=0, item_ypos2=0;
	
	if(mi->count()) 
	{
		LinkedListIterator<BaseMenuItem> it(mi);
		for(; it.current(); it++)
		{
			item_xpos2 = it.current()->item_x + item_width;
			item_ypos2 = it.current()->item_y + item_height;
	
			if ( x >= it.current()->item_x && 
			     x < item_xpos2 + menu.width &&
			     y >= it.current()->item_y &&  
			     y < item_ypos2 - (item_height - 6)
			   ) 
			    {
			            return it.current();
			    }
		}	
	}

	return NULL;
}

===
From: Frank Hale
ICQ: 7205161
Webpage: http://jade.netpedia.net/sapphire
Webpage: http://www.linuxstart.com/~frankhale

"Sapphire - a new window manager for X11"
__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com