shankar101854 / flashcanvas

Automatically exported from code.google.com/p/flashcanvas
0 stars 0 forks source link

Fix for isPointInPath #12

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
This works for polygons. Will probably not work if there are transformations 
etc. applied:

    this.pointInPoly = function pointInPoly(pos, poly) {
        /* This code is patterned after [Franklin, 2000]
        http://www.geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm
        Tells us if the point is in this polygon */
        cn = 0
        pts = poly.slice();
        pts.push([poly[0][0], poly[0][1]]);
        for (var i=0; i<poly.length; i++)
            if (((pts[i][1] <= pos[1]) && (pts[i+1][1] > pos[1])) || ((pts[i][1] > pos[1]) && (pts[i+1][1] <= pos[1])))
                if (pos[0] < pts[i][0] + (pos[1] - pts[i][1]) / (pts[i+1][1] - pts[i][1]) * (pts[i+1][0] - pts[i][0]))
        cn += 1
        return cn % 2
    }

Original issue reported on code.google.com by mccor...@gmail.com on 18 Mar 2011 at 3:12

GoogleCodeExporter commented 8 years ago
Thank you for your contribution.

FlashCanvas Pro and fxCanvas have already implemented isPointInPath() in 
different ways. I'd like to examine which implementation is fast and accurate.

By the way, the code you wrote is an implementation for the even-odd rule, 
isn't it? According to the Canvas specification, an implementation for the 
non-zero winding number rule is desired.
http://www.w3.org/TR/2dcontext/#dom-context-2d-ispointinpath

Original comment by revu...@gmail.com on 19 Mar 2011 at 1:47

GoogleCodeExporter commented 8 years ago
Hi,

Yes, you are quite right. I found this page discussing the difference:

http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm

Original comment by mccor...@gmail.com on 19 Mar 2011 at 9:57