/** * Replaces this region with the intersection of itself and {@code other} * @param other The region with which to intersect. */ public void intersect(Region other) { // If there's no intersection set this as the Empty region. if (!isIntersected(other)) { makeEmpty(); return; } // The regions intersect. So let's first find the left & top values int otherLeft = other.getLeft(); int otherTop = other.getTop(); int intersectionLeft = (left >= otherLeft) ? left : otherLeft; int intersectionTop = (top >= otherTop) ? top : otherTop; // Now the width and height of the intersect int right = left + width; int otherRight = otherLeft + other.getWidth(); int intersectionRight = (right <= otherRight) ? right : otherRight; int intersectionWidth = intersectionRight - intersectionLeft; int bottom = top + height; int otherBottom = otherTop + other.getHeight(); int intersectionBottom = (bottom <= otherBottom) ? bottom : otherBottom; int intersectionHeight = intersectionBottom - intersectionTop; left = intersectionLeft; top = intersectionTop; width = intersectionWidth; height = intersectionHeight; }
/** * Replaces this region with the intersection of itself and * {@code other} * @param other The region with which to intersect. */ public void intersect(Region other) { // If there's no intersection set this as the Empty region. if (!isIntersected(other)) { makeEmpty(); return; } // The regions intersect. So let's first find the left & top values int otherLeft = other.getLeft(); int otherTop = other.getTop(); int intersectionLeft = (left >= otherLeft) ? left : otherLeft; int intersectionTop = (top >= otherTop) ? top : otherTop; // Now the width and height of the intersect int right = left + width; int otherRight = otherLeft + other.getWidth(); int intersectionRight = (right <= otherRight) ? right : otherRight; int intersectionWidth = intersectionRight - intersectionLeft; int bottom = top + height; int otherBottom = otherTop + other.getHeight(); int intersectionBottom = (bottom <= otherBottom) ? bottom : otherBottom; int intersectionHeight = intersectionBottom - intersectionTop; left = intersectionLeft; top = intersectionTop; width = intersectionWidth; height = intersectionHeight; }