On Apr 4, 2008, at 5:54 PM, Eric Cooper wrote:

FWIW, I decided to write my own hit test using dot products for concave polygons. I will need to do something a bit more involved for convex polygons, but I don't need that
immediately.

Give this a shot. This is from my AS3 boolean library I'm working on. I originally wrote the library for AS2 but recently decided to revisit it for a possible project.

- jon


/**
 *      Point inside polygon using winding number method
 *      q               Point2d         A point 2d structure with x,y properties
 *      p               Polygon         A polygon as an array of points in 
clockwise order
* returns Boolean Point is inside polygon if winding (w) number is != 0
 */
private function pointInsidePolygon(q:Point2d, p:Polygon):Boolean
{
        var n:int = p.length;
        var w:int = 0;

        for (i=0; i<n; i++) {
                var ip:int = (i+1)%n;
                if ( ( p[i].y < q.y ) != ( p[ip].y < q.y ) ) {
                        if ( p[i].x >= q.x ) {
                                if ( p[ip].x > q.x ) {
                                        w += 2 * ( p[ip].y > p[i].y ) - 1;
                                }
                                else {
                                        if ( ( (p[i].x-q.x)*(p[ip].y-q.y) -
                                                                 
(p[ip].x-q.x)*(p[i].y-q.y) > 0 ) ==
                                                         ( p[ip].y > p[i].y ) ) 
{
                                                w += 2 * ( p[ip].y > p[i].y ) - 
1;
                                        }
                                }
                        }
                        else {
                                if ( p[ip].x > q.x ) {
                                        if ( ( (p[i].x-q.x)*(p[ip].y-q.y) -
                                                                 
(p[ip].x-q.x)*(p[i].y-q.y) > 0 ) ==
                                                         ( p[ip].y > p[i].y ) ) 
{
                                                w += 2 * ( p[ip].y > p[i].y ) - 
1;
                                        }
                                }
                        }
                }
        }
        return (w!=0) ? true:false;
}


Reply via email to