OsmNogoPolygon extends OsmNodeNamed { public boolean intersects(int lon0, int lat0, int lon1, int lat1) { final Point p0 = new Point (lon0,lat0); final Point p1 = new Point (lon1,lat1); int i_last = points.size()-1; Point p2 = points.get(isClosed ? i_last : 0 ); for (int i = isClosed ? 0 : 1 ; i <= i_last; i++) { Point p3 = points.get(i); if (intersect2D_2Segments(p0,p1,p2,p3) > 0) { return true; } p2 = p3; } return false; } OsmNogoPolygon(boolean closed); final void addVertex(int lon, int lat); void calcBoundingCircle(); boolean intersects(int lon0, int lat0, int lon1, int lat1); boolean isOnPolyline( long px, long py ); static boolean isOnLine( long px, long py, long p0x, long p0y, long p1x, long p1y ); boolean isWithin(final long px, final long py); double distanceWithinPolygon(int lon1, int lat1, int lon2, int lat2); final List<Point> points; final boolean isClosed; }
@Test public void testIntersectsPolygon() { double[] p0lons = { 0.0, 1.0, -0.5, 0.5, 0.7, 0.7, 0.7, -1.5, -1.5, 0.0 }; double[] p0lats = { 0.0, 0.0, 0.5, 0.5, 0.5, 0.05, 0.05, -1.5, 0.2, 0.0 }; double[] p1lons = { 0.0, 1.0, 0.5, 1.0, 0.7, 0.7, 0.7, -0.5, -0.2, 0.5 }; double[] p1lats = { 0.0, 0.0, 0.5, 0.5, -0.5, -0.5, -0.05, -0.5, 1.5, -1.5 }; boolean[] within = { false, false, false, true, true, true, false, true, true, true }; for (int i=0; i<p0lons.length; i++) { assertEquals("("+p0lons[i]+","+p0lats[i]+")-("+p1lons[i]+","+p1lats[i]+")",within[i],polygon.intersects(toOsmLon(p0lons[i], OFFSET_X), toOsmLat(p0lats[i], OFFSET_Y), toOsmLon(p1lons[i], OFFSET_X), toOsmLat(p1lats[i], OFFSET_Y))); } }
@Test public void testIntersectsPolyline() { double[] p0lons = { 0.0, 1.0, -0.5, 0.5, 0.7, 0.7, 0.7, -1.5, -1.5, 0.0 }; double[] p0lats = { 0.0, 0.0, 0.5, 0.5, 0.5, 0.05, 0.05, -1.5, 0.2, 0.0 }; double[] p1lons = { 0.0, 1.0, 0.5, 1.0, 0.7, 0.7, 0.7, -0.5, -0.2, 0.5 }; double[] p1lats = { 0.0, 0.0, 0.5, 0.5, -0.5, -0.5, -0.05, -0.5, 1.5, -1.5 }; boolean[] within = { false, false, false, true, true, true, false, true, true, false }; for (int i=0; i<p0lons.length; i++) { assertEquals("("+p0lons[i]+","+p0lats[i]+")-("+p1lons[i]+","+p1lats[i]+")",within[i],polyline.intersects(toOsmLon(p0lons[i], OFFSET_X), toOsmLat(p0lats[i], OFFSET_Y), toOsmLon(p1lons[i], OFFSET_X), toOsmLat(p1lats[i], OFFSET_Y))); } }