We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › finding whether a mouse is over an image
Page Index Toggle Pages: 1
finding whether a mouse is over an image (Read 1002 times)
finding whether a mouse is over an image
Jan 27th, 2008, 3:02am
 
I'm trying to find out whether a mouse cursor (or any point) is over an image, but I'm having trouble since the image is rotating around. I can't just use if(x > corner1x && x < corner2x && y > corner1y && y < corner2y). How do I compensate for the rotation?
Re: finding whether a mouse is over an image
Reply #1 - Jan 27th, 2008, 9:57am
 
I guess you'll have to calculate the equation of each vertex, to determine either the cursor is inside or out.

But I think 2D-picking could help, too.
http://www.processinghacks.com/hacks/picking
Re: finding whether a mouse is over an image
Reply #2 - Jan 27th, 2008, 11:44am
 
If you're rotating a rect and you know the coordinates of its corners (say... you've rotated the points mathematically or did screenX/Y to get the coordinates back) you can use point-in-triangle detection to figure out if the mouse is inside the space.

Try looking up sedgewick's point in triangle routine, there are many solutions out there. Think of a rotated rect as two triangles and you should be fine.
Re: finding whether a mouse is over an image
Reply #3 - Jan 27th, 2008, 2:41pm
 
If you able to calculate the coordinates of the corners you can use the awt.Polygon class to check:
http://www.processinghacks.com/hacks/using-awt-s-polygon-class-to-test-for-insideness
Re: finding whether a mouse is over an image
Reply #4 - Jan 28th, 2008, 12:13am
 
Thanks for the responses! How would I go about finding the position of the corners in world space? I'm using translate() and rotate() to move the image around.
Re: finding whether a mouse is over an image
Reply #5 - Jan 28th, 2008, 9:40am
 
I would try screenX and screenY.
Re: finding whether a mouse is over an image
Reply #6 - Jan 29th, 2008, 1:00am
 
It worked! Thanks for all the help!
Re: finding whether a mouse is over an image
Reply #7 - Jan 29th, 2008, 5:35am
 
Here's a small applet with an algorithm to find whether a point is in a triangle.

http://www.davidbokser.com/private/pointInTriangle.pde
Page Index Toggle Pages: 1