Bound extends Entity implements Comparable<Bound> { public Bound intersect(Bound intersectingBound) { String newOrigin; double newRight = 0.0, newLeft = 0.0, newTop, newBottom; boolean intersect180, this180; if (intersectingBound == null) { return null; } newTop = Math.min(this.getTop(), intersectingBound.getTop()); newBottom = Math.max(this.getBottom(), intersectingBound.getBottom()); if (Double.compare(newBottom, newTop) >= 0) { return null; } intersect180 = (Double.compare(intersectingBound.getLeft(), intersectingBound.getRight()) > 0); this180 = (Double.compare(this.getLeft(), this.getRight()) > 0); if ((intersect180 && this180) || !(intersect180 || this180)) { newRight = Math.min(this.getRight(), intersectingBound.getRight()); newLeft = Math.max(this.getLeft(), intersectingBound.getLeft()); if (!(intersect180 || this180) && (Double.compare(newLeft, newRight) >= 0)) { return null; } } else { Bound b1, b2; if (intersect180 && !this180) { b1 = this; b2 = intersectingBound; } else { b1 = intersectingBound; b2 = this; } if (Double.compare(b1.getRight(), b2.getLeft()) > 0 && Double.compare(b1.getLeft(), b2.getRight()) < 0) { Double diff1 = b1.getRight() - b1.getLeft(); Double diff2 = b2.getRight() - MIN_LONGITUDE + MAX_LONGITUDE - b2.getLeft(); if (Double.compare(diff1, diff2) <= 0) { newRight = b1.getRight(); newLeft = b1.getLeft(); } else { newRight = b2.getRight(); newLeft = b2.getLeft(); } } else if (Double.compare(b1.getRight(), b2.getLeft()) > 0) { newRight = b1.getRight(); newLeft = b2.getLeft(); } else if (Double.compare(b1.getLeft(), b2.getRight()) < 0) { newRight = b2.getRight(); newLeft = b1.getLeft(); } } if (Double.compare(newRight, newLeft) == 0) { return null; } if (origin != "") { newOrigin = origin; } else { newOrigin = intersectingBound.origin; } return new Bound(newRight, newLeft, newTop, newBottom, newOrigin); } Bound(String origin);  Bound(double right, double left, double top, double bottom, String origin);  Bound(StoreReader sr, StoreClassRegister scr); @Override void store(StoreWriter sw, StoreClassRegister scr); @Override EntityType getType(); double getRight(); double getLeft(); double getTop(); double getBottom(); String getOrigin(); Bound intersect(Bound intersectingBound); Bound union(Bound unionBound); Iterable<Bound> toSimpleBound(); int compareTo(Bound comparisonBound); @Override boolean equals(Object o); @Override int hashCode(); @Override Bound getWriteableInstance(); @Override String toString();  }
@Test public final void testIntersect2() { final double right1 = 20.0; final double left1 = 10.0; final double top1 = 40.0; final double bottom1 = 30.0; final double right2 = 30.0; final double left2 = 15.0; final double top2 = 25.0; final double bottom2 = 20.0; Bound b = new Bound(right1, left1, top1, bottom1, "").intersect(new Bound( right2, left2, top2, bottom2, "")); assertNull(b); }
@Test public final void testIntersect3() { final double right1 = 20.0; final double left1 = 10.0; final double top1 = 40.0; final double bottom1 = 30.0; final double right2 = 30.0; final double left2 = 21.0; final double top2 = 45.0; final double bottom2 = 20.0; Bound b = new Bound(right1, left1, top1, bottom1, "").intersect(new Bound( right2, left2, top2, bottom2, "")); assertNull(b); }