Rect { public double distance(Rect otherRect) { if (overlaps(otherRect)) { return 0; } int dLat1, dLat2; int dLon1, dLon2; if (this.maxLatitudeE6 < otherRect.minLatitudeE6) { dLat1 = this.maxLatitudeE6; dLat2 = otherRect.minLatitudeE6; } else if (this.minLatitudeE6 > otherRect.maxLatitudeE6) { dLat1 = this.minLatitudeE6; dLat2 = otherRect.maxLatitudeE6; } else { dLat1 = Math.min(this.maxLatitudeE6, otherRect.maxLatitudeE6); dLat2 = Math.min(this.maxLatitudeE6, otherRect.maxLatitudeE6); } if (this.maxLongitudeE6 < otherRect.minLongitudeE6) { dLon1 = this.maxLongitudeE6; dLon2 = otherRect.minLongitudeE6; } else if (this.minLongitudeE6 > otherRect.maxLongitudeE6) { dLon1 = this.minLongitudeE6; dLon2 = otherRect.maxLongitudeE6; } else { dLon1 = Math.min(this.maxLongitudeE6, otherRect.maxLongitudeE6); dLon2 = Math.min(this.maxLongitudeE6, otherRect.maxLongitudeE6); } return GeoCoordinate.sphericalDistance(dLon1, dLat1, dLon2, dLat2); } Rect(GeoCoordinate gc);  Rect(int minLongitudeE6, int maxLongitudeE6, int minLatitudeE6, int maxLatitudeE6);  Rect(double minLongitude, double maxLongitude, double minLatitude, double maxLatitude);  Rect(Rect rect); static boolean overlaps(int minLon1, int maxLon1, int minLat1, int maxLat1, int minLon2, int maxLon2,
			int minLat2, int maxLat2); static Rect fromString(String rectString); int getMinLongitudeE6(); int getMaxLongitudeE6(); int getMinLatitudeE6(); int getMaxLatitudeE6(); GeoCoordinate center(); double distance(Rect otherRect); @Override boolean equals(Object obj); void expandToInclude(GeoCoordinate gc); void expandToInclude(int latE6, int lonE6); void expandToInclude(Rect otherRect); GeoCoordinate getCenter(); @Override int hashCode(); boolean includes(GeoCoordinate c); boolean includes(int latitudeE6, int longitudeE6); boolean overlaps(Rect r); @Override String toString(); public int maxLatitudeE6; public int maxLongitudeE6; public int minLatitudeE6; public int minLongitudeE6; }
@Test public void testDistance() { Rect r1 = new Rect(0, 1000000, 50000000, 51000000); Rect r2 = new Rect(0, 1000000, 50000000, 51000000); Rect r3 = new Rect(-1000000, 0, 45000000, 55000000); Rect r4 = new Rect(-1000000, 2000000, 45000000, 55000000); Rect r5 = new Rect(2000000, 3000000, 52000000, 53000000); Rect r6 = new Rect(-2000000, -1000000, 48000000, 49000000); Rect r7 = new Rect(-2000000, 2000000, 48000000, 49000000); assertEquals(0, r1.distance(r2), 0); assertEquals(0, r1.distance(r3), 0); assertEquals(0, r1.distance(r4), 0); assertEquals(GeoCoordinate.sphericalDistance(1000000, 51000000, 2000000, 52000000), r1.distance(r5), 0); assertEquals(GeoCoordinate.sphericalDistance(0, 50000000, -1000000, 49000000), r1.distance(r6), 0); assertEquals(GeoCoordinate.sphericalDistance(0, 50000000, 0, 49000000), r1.distance(r7), 0); }