OsmNogoPolygon extends OsmNodeNamed { public void calcBoundingCircle() { int cxmin, cxmax, cymin, cymax; cxmin = cymin = Integer.MAX_VALUE; cxmax = cymax = Integer.MIN_VALUE; for (int i = 0; i < points.size(); i++) { final Point p = points.get(i); if (p.x < cxmin) { cxmin = p.x; } if (p.x > cxmax) { cxmax = p.x; } if (p.y < cymin) { cymin = p.y; } if (p.y > cymax) { cymax = p.y; } } int cx = (cxmax+cxmin) / 2; int cy = (cymax+cymin) / 2; double[] lonlat2m = CheapRuler.getLonLatToMeterScales( cy ); double dlon2m = lonlat2m[0]; double dlat2m = lonlat2m[1]; double rad = 0; double dmax = 0; int i_max = -1; do { for (int i = 0; i < points.size(); i++) { final Point p = points.get(i); final double dist = CheapRuler.distance(p.x, p.y, (int) cx, (int) cy); if (dist <= rad) { continue; } if (dist > dmax) { dmax = dist; i_max = i; } } if (i_max < 0) { break; } final double dd = 0.5 * (1 - rad / dmax); final Point p = points.get(i_max); cx += (int)(dd * (p.x - cx) + 0.5); cy += (int)(dd * (p.y - cy) + 0.5); dmax = rad = CheapRuler.distance(p.x, p.y, (int) cx, (int) cy); i_max = -1; } while (true); ilon = cx; ilat = cy; radius = rad; return; } 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 testCalcBoundingCircle() { double[] lonlat2m = CheapRuler.getLonLatToMeterScales( polygon.ilat ); double dlon2m = lonlat2m[0]; double dlat2m = lonlat2m[1]; polygon.calcBoundingCircle(); double r = polygon.radius; for (int i=0; i<lons.length; i++) { double dpx = (toOsmLon(lons[i], OFFSET_X) - polygon.ilon) * dlon2m; double dpy = (toOsmLat(lats[i], OFFSET_Y) - polygon.ilat) * dlat2m; double r1 = Math.sqrt(dpx * dpx + dpy * dpy); double diff = r-r1; assertTrue("i: "+i+" r("+r+") >= r1("+r1+")", diff >= 0); } polyline.calcBoundingCircle(); r = polyline.radius; for (int i=0; i<lons.length; i++) { double dpx = (toOsmLon(lons[i], OFFSET_X) - polyline.ilon) * dlon2m; double dpy = (toOsmLat(lats[i], OFFSET_Y) - polyline.ilat) * dlat2m; double r1 = Math.sqrt(dpx * dpx + dpy * dpy); double diff = r-r1; assertTrue("i: "+i+" r("+r+") >= r1("+r1+")", diff >= 0); } }