is Point Inside Polygon - Android java.lang

Android examples for java.lang:Math Geometry

Description

is Point Inside Polygon

Demo Code


//package com.java2s;
import java.util.Vector;
import android.graphics.PointF;

public class Main {
    /**// w  w  w.  ja  v  a 2s . co m
     * // http://www.visibone.com/inpoly/
     * 
     * @param points
     * @param offset
     * @param count
     * @param xt
     * @param yt
     * @return si un punto est? dentro de un pol?gono
     */
    public static boolean isPointInsidePolygon(Vector<PointF> points,
            int offset, int count, float xt, float yt) {
        float xnew, ynew;
        float xold, yold;
        float x1, y1;
        float x2, y2;
        int i;
        boolean inside = false;

        if (count < 3) {
            return (false);
        }
        PointF pOld = points.elementAt(count - 1);
        xold = pOld.x;
        yold = pOld.y;
        for (i = offset; i < count; i++) {
            PointF pNew = points.elementAt(i);
            xnew = pNew.x;
            ynew = pNew.y;
            if (xnew > xold) {
                x1 = xold;
                x2 = xnew;
                y1 = yold;
                y2 = ynew;
            } else {
                x1 = xnew;
                x2 = xold;
                y1 = ynew;
                y2 = yold;
            }
            if ((xnew < xt) == (xt <= xold) /* edge "open" at one end */
                    && (yt - y1) * (x2 - x1) < (y2 - y1) * (xt - x1)) {
                inside = !inside;
            }
            xold = xnew;
            yold = ynew;
        }
        return (inside);
    }
}

Related Tutorials