inside Polygon - Java 2D Graphics

Java examples for 2D Graphics:Polygon

Description

inside Polygon

Demo Code


//package com.java2s;

public class Main {
    public static boolean insidePolygon(double[][] vertices, double x,
            double y) {
        int i, j;
        boolean c = false;
        for (i = 0, j = vertices.length - 1; i < vertices.length; j = i++) {
            double vix = vertices[i][0];
            double viy = vertices[i][1];
            double vjx = vertices[j][0];
            double vjy = vertices[j][1];
            if (((viy > y) != (vjy > y))
                    && (x < (vjx - vix) * (y - viy) / (vjy - viy) + vix)) {
                c = !c;//from   w ww. j  a  va 2 s . c o m
            }
        }
        return c;
    }
}

Related Tutorials