Finding Points contained in a Path in Android

前端 未结 4 2319
情深已故
情深已故 2020-12-13 02:51

Is there a reason that they decided not to add the contains method (for Path) in Android?

I\'m wanting to know what points I have in a Path and hoped it was easier t

4条回答
  •  伪装坚强ぢ
    2020-12-13 03:48

    Tried the other answer, but it gave an erroneous outcome for my case. Didn't bother to find the exact cause, but made my own direct translation from the algorithm on: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

    Now the code reads:

    /**
     * Minimum Polygon class for Android.
     */
    public class Polygon
    {
        // Polygon coodinates.
        private int[] polyY, polyX;
    
        // Number of sides in the polygon.
        private int polySides;
    
        /**
         * Default constructor.
         * @param px Polygon y coods.
         * @param py Polygon x coods.
         * @param ps Polygon sides count.
         */
        public Polygon( int[] px, int[] py, int ps )
        {
            polyX = px;
            polyY = py;
            polySides = ps;
        }
    
        /**
         * Checks if the Polygon contains a point.
         * @see "http://alienryderflex.com/polygon/"
         * @param x Point horizontal pos.
         * @param y Point vertical pos.
         * @return Point is in Poly flag.
         */
        public boolean contains( int x, int y )
        {
            boolean c = false;
            int i, j = 0;
            for (i = 0, j = polySides - 1; i < polySides; j = i++) {
                if (((polyY[i] > y) != (polyY[j] > y))
                    && (x < (polyX[j] - polyX[i]) * (y - polyY[i]) / (polyY[j] - polyY[i]) + polyX[i]))
                c = !c;
            }
            return c;
        }  
    }
    

提交回复
热议问题