@Test public void testIsWithin() { double[] plons = { 0.0, 0.5, 1.0, -1.5, -0.5, 1.0, 1.0, 0.5, 0.5, 0.5, }; double[] plats = { 0.0, 1.5, 0.0, 0.5, -1.5, -1.0, -0.1, -0.1, 0.0, 0.1, }; boolean[] within = { true, false, false, false, false, true, true, true, true, true, }; for (int i=0; i<plons.length; i++) { assertEquals("("+plons[i]+","+plats[i]+")",within[i],polygon.isWithin(toOsmLon(plons[i], OFFSET_X), toOsmLat(plats[i], OFFSET_Y))); } }
public boolean isWithin(final long px, final long py) { int wn = 0; final int i_last = points.size()-1; final Point p0 = points.get(isClosed ? i_last : 0); long p0x = p0.x; long p0y = p0.y; for (int i = isClosed ? 0 : 1; i <= i_last; i++) { final Point p1 = points.get(i); final long p1x = p1.x; final long p1y = p1.y; if (OsmNogoPolygon.isOnLine(px, py, p0x, p0y, p1x, p1y)) { return true; } if (p0y <= py) { if (p1y > py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) > 0) { ++wn; } } } else { if (p1y <= py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) < 0) { --wn; } } } p0x = p1x; p0y = p1y; } return wn != 0; }
OsmNogoPolygon extends OsmNodeNamed { public boolean isWithin(final long px, final long py) { int wn = 0; final int i_last = points.size()-1; final Point p0 = points.get(isClosed ? i_last : 0); long p0x = p0.x; long p0y = p0.y; for (int i = isClosed ? 0 : 1; i <= i_last; i++) { final Point p1 = points.get(i); final long p1x = p1.x; final long p1y = p1.y; if (OsmNogoPolygon.isOnLine(px, py, p0x, p0y, p1x, p1y)) { return true; } if (p0y <= py) { if (p1y > py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) > 0) { ++wn; } } } else { if (p1y <= py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) < 0) { --wn; } } } p0x = p1x; p0y = p1y; } return wn != 0; } }
OsmNogoPolygon extends OsmNodeNamed { public boolean isWithin(final long px, final long py) { int wn = 0; final int i_last = points.size()-1; final Point p0 = points.get(isClosed ? i_last : 0); long p0x = p0.x; long p0y = p0.y; for (int i = isClosed ? 0 : 1; i <= i_last; i++) { final Point p1 = points.get(i); final long p1x = p1.x; final long p1y = p1.y; if (OsmNogoPolygon.isOnLine(px, py, p0x, p0y, p1x, p1y)) { return true; } if (p0y <= py) { if (p1y > py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) > 0) { ++wn; } } } else { if (p1y <= py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) < 0) { --wn; } } } p0x = p1x; p0y = p1y; } return wn != 0; } OsmNogoPolygon(boolean closed); }
OsmNogoPolygon extends OsmNodeNamed { public boolean isWithin(final long px, final long py) { int wn = 0; final int i_last = points.size()-1; final Point p0 = points.get(isClosed ? i_last : 0); long p0x = p0.x; long p0y = p0.y; for (int i = isClosed ? 0 : 1; i <= i_last; i++) { final Point p1 = points.get(i); final long p1x = p1.x; final long p1y = p1.y; if (OsmNogoPolygon.isOnLine(px, py, p0x, p0y, p1x, p1y)) { return true; } if (p0y <= py) { if (p1y > py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) > 0) { ++wn; } } } else { if (p1y <= py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) < 0) { --wn; } } } p0x = p1x; p0y = p1y; } return wn != 0; } 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); }
OsmNogoPolygon extends OsmNodeNamed { public boolean isWithin(final long px, final long py) { int wn = 0; final int i_last = points.size()-1; final Point p0 = points.get(isClosed ? i_last : 0); long p0x = p0.x; long p0y = p0.y; for (int i = isClosed ? 0 : 1; i <= i_last; i++) { final Point p1 = points.get(i); final long p1x = p1.x; final long p1y = p1.y; if (OsmNogoPolygon.isOnLine(px, py, p0x, p0y, p1x, p1y)) { return true; } if (p0y <= py) { if (p1y > py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) > 0) { ++wn; } } } else { if (p1y <= py) { if (((p1x - p0x) * (py - p0y) - (px - p0x) * (p1y - p0y)) < 0) { --wn; } } } p0x = p1x; p0y = p1y; } return wn != 0; } 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; }