/** * If the rectangle specified by left,top,right,bottom intersects this * rectangle, return true and set this rectangle to that intersection, * otherwise return false and do not change this rectangle. No check is * performed to see if either rectangle is empty. Note: To just test for * intersection, use {@link #intersects(Rect, Rect)}. * * @param left The left side of the rectangle being intersected with this * rectangle * @param top The top of the rectangle being intersected with this rectangle * @param right The right side of the rectangle being intersected with this * rectangle. * @param bottom The bottom of the rectangle being intersected with this * rectangle. * @return true if the specified rectangle and this rectangle intersect * (and this rectangle is then set to that intersection) else * return false and do not change this rectangle. */ @CheckResult public boolean intersect(int left, int top, int right, int bottom) { if (this.left < right && left < this.right && this.top < bottom && top < this.bottom) { if (this.left < left) this.left = left; if (this.top < top) this.top = top; if (this.right > right) this.right = right; if (this.bottom > bottom) this.bottom = bottom; return true; } return false; }
/** * If rectangles a and b intersect, return true and set this rectangle to * that intersection, otherwise return false and do not change this * rectangle. No check is performed to see if either rectangle is empty. * To just test for intersection, use intersects() * * @param a The first rectangle being intersected with * @param b The second rectangle being intersected with * @return true iff the two specified rectangles intersect. If they do, set * this rectangle to that intersection. If they do not, return * false and do not change this rectangle. */ @CheckResult public boolean setIntersect(Rect a, Rect b) { if (a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom) { left = Math.max(a.left, b.left); top = Math.max(a.top, b.top); right = Math.min(a.right, b.right); bottom = Math.min(a.bottom, b.bottom); return true; } return false; }
/** * If the specified rectangle intersects this rectangle, return true and set * this rectangle to that intersection, otherwise return false and do not * change this rectangle. No check is performed to see if either rectangle * is empty. To just test for intersection, use intersects() * * @param r The rectangle being intersected with this rectangle. * @return true if the specified rectangle and this rectangle intersect * (and this rectangle is then set to that intersection) else * return false and do not change this rectangle. */ @CheckResult public boolean intersect(Rect r) { return intersect(r.left, r.top, r.right, r.bottom); }