Page 1 of 1

polygon coords "x1,y1,x2,y2,..." suggestions [RESOLVED]

Posted: Fri Dec 03, 2010 5:49 am
by houston
hi; I want to have an image on the screen and have relative regions within the image that are clickable hot-spots, with the following region types:

circle coords = "x,y,r"
rect coords = "x1,y1, x2,y2"
polygon coords = "x1,y1, ... "

how would I work out the polygon regions efficiently for mouse coords, the polygons can have many coords where do you start? I just can't get my head around this lol
If you need to understand better what I'm talking about it has to work the same way as HTML Image Maps..

I guess if anybody has done anything similar with flood filling a polygon shape then they could give pointers!!

Sorry if none of this seems worded properly, lack of sleep!

houston.
found a good method here if anybody is interested..

http://www.faqs.org/faqs/graphics/algorithms-faq/

houston

Re: polygon coords "x1,y1,x2,y2,..." suggestions

Posted: Fri Dec 03, 2010 5:57 am
by RyanPridgeon
So are you talking about testing the mouse position against those clickable areas? Or drawing the shapes to the screen? For drawing it depends on your API, for example, some will have built in methods for drawing those shapes.

Re: polygon coords "x1,y1,x2,y2,..." suggestions

Posted: Fri Dec 03, 2010 6:22 am
by houston
RyanPridgeon wrote:So are you talking about testing the mouse position against those clickable areas? Or drawing the shapes to the screen? For drawing it depends on your API, for example, some will have built in methods for drawing those shapes.
I knew I'd worded that badly lol.. Yes testing mouse position within the polygon!


houston

Re: polygon coords "x1,y1,x2,y2,..." suggestions [RESOLVED]

Posted: Fri Dec 03, 2010 11:51 am
by RyanPridgeon
For the n polygon test, you may find this helpful;

http://erich.realtimerendering.com/ptinpoly/

For the rectangle test, you can just do something like

if (point.x >= rect.x1 && point.x =< rect.x2
&& point.y >= rect.y1 && point.y <= rect.y2)
{
collision();
}

For the circle test, just check the distance between the point and the center of the circle.

distance = Math.sqrt( Math.pow(point.x - circle.x, 2) + Math.pow(point.y - circle.y, 2) ) // use pythag to find the distance
if ( distance < circle.radius )
{
collision();
}