Java examples for java.lang:Math Geometry Shape
point In Polygon Or On Boundary
import java.awt.geom.*; import java.util.*; public class Main{ public static int pointInPolygonOrOnBoundary(double[] point, double[][] polygon) { int nbrOfPoints = polygon.length; if (nbrOfPoints < 3) { return 0; }/*from ww w .j av a 2s . c o m*/ // find bounding box double w = Double.MAX_VALUE; double e = Double.MIN_VALUE; double s = Double.MAX_VALUE; double n = Double.MIN_VALUE; for (int i = 0; i < nbrOfPoints; i++) { if (polygon[i][0] < w) { w = polygon[i][0]; } if (polygon[i][0] > e) { e = polygon[i][0]; } if (polygon[i][0] < s) { s = polygon[i][1]; } if (polygon[i][0] > n) { n = polygon[i][1]; } } // test against bounding box if (point[0] < w || point[0] > e || point[1] < s || point[1] > n) { return 0; } /* Even-odd rule: Draw a line from the passed point to a point outside the path. Count the number of path segments that the line crosses. If the result is odd, the point is inside the path. If the result is even, the point is outside the path. */ double outY = n + 1000.; int nbrIntersections = 0; int lastPointID = nbrOfPoints - 1; for (int i = 0; i < lastPointID; i++) { // test intersection with a vertical line starting at the point to test int intersection = GeometryUtils.linesIntersect(point[0], point[1], point[0], outY, polygon[i][0], polygon[i][1], polygon[i + 1][0], polygon[i + 1][1]); /* GeometryUtils.linesIntersect may return the following values: 0 : no intersection 1 : legal intersection 2 : point on line 3 : point on point 4 : line on line */ if (intersection > 1) { return 2; // the point to test is laying on a line: return true } if (intersection == 1) { nbrIntersections++; // found a normal intersection } } return nbrIntersections % 2; } /** * Check if two lines intersect. * Line definition : * Line 1 (x0,y0)-(x1,y1) * Line 2 (x2,y2)-(x3,y3) * The return values depend on the intersection type: * 0: no intersection * 1: legal intersection * 2: point on line * 3: point on point * 4: line on line * @param x0 Start point of line 1. Horizontal coordinate. * @param y0 Start point of line 1. Vertical coordinate. * @param x1 End point of line 1. Horizontal coordinate. * @param y1 End point of line 1. Vertical coordinate. * @param x2 Start point of line 2. Horizontal coordinate. * @param y2 Start point of line 2. Vertical coordinate. * @param x3 End point of line 2. Horizontal coordinate. * @param y4 End point of line 2. Vertical coordinate. * @return 0 if no intersection; 1 if intersection; 2 if point on line; * 3 if point on point; 4 if line on line. */ public static int linesIntersect(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3) { int k03_01, k01_02, k20_23, k23_21; int pos, neg, nul; k03_01 = SIGNTEST((x3 - x0) * (y1 - y0) - (y3 - y0) * (x1 - x0)); k01_02 = SIGNTEST((x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0)); k20_23 = SIGNTEST((x0 - x2) * (y3 - y2) - (y0 - y2) * (x3 - x2)); k23_21 = SIGNTEST((x3 - x2) * (y1 - y2) - (y3 - y2) * (x1 - x2)); pos = neg = nul = 0; if (k03_01 < 0) { neg++; } else if (k03_01 > 0) { pos++; } else { nul++; } if (k01_02 < 0) { neg++; } else if (k01_02 > 0) { pos++; } else { nul++; } if (k20_23 < 0) { neg++; } else if (k20_23 > 0) { pos++; } else { nul++; } if (k23_21 < 0) { neg++; } else if (k23_21 > 0) { pos++; } else { nul++; } if (nul == 0) { if (neg == 4 || pos == 4) { return 1; } // legal intersection else { return 0; } // no intersection } else { if (neg == 3 || pos == 3) { return 2; } // point on line else if (neg == 2 || pos == 2) { return 3; } // point on point else { return 4; } // line on line } } private static int SIGNTEST(double a) { return ((a) > 0.) ? 1 : ((a) < 0.) ? -1 : 0; } }