is Polygon Contain Point - Android Graphics

Android examples for Graphics:Path Point

Description

is Polygon Contain Point

Demo Code


//package com.java2s;
import android.graphics.PointF;

public class Main {

    public static boolean isPolygonContainPoint(PointF point,
            PointF[] vertexPoints) {//from  w  w  w  . j  a  va 2s .  c  o m
        int nCross = 0;
        for (int i = 0; i < vertexPoints.length; i++) {
            PointF p1 = vertexPoints[i];
            PointF p2 = vertexPoints[(i + 1) % vertexPoints.length];
            if (p1.y == p2.y)
                continue;
            if (point.y < Math.min(p1.y, p2.y))
                continue;
            if (point.y >= Math.max(p1.y, p2.y))
                continue;
            double x = (double) (point.y - p1.y) * (double) (p2.x - p1.x)
                    / (double) (p2.y - p1.y) + p1.x;
            if (x > point.x)
                nCross++;
        }
        return (nCross % 2 == 1);
    }
}

Related Tutorials