public bool intersects(Rect rr) { if (rr == null) return false; if (rr.max_x() < this.x) return false; if (rr.x > this.max_x()) return false; if (rr.max_y() < this.y) return false; if (rr.y > this.max_y()) return false; return true; }
public Rect overlapRect(Rect rr) { if (!this.intersects(rr)) return null; // Determine the x origin of the intersection: int new_xx; if (rr.x < x) new_xx = x; else new_xx = rr.x; // Determine the y origin of the intersection: int new_yy; if (rr.y < y) new_yy = y; else new_yy = rr.y; // Determine the maximum x of the intersection: int new_max_x; if (rr.max_x() < max_x()) new_max_x = rr.max_x(); else new_max_x = max_x(); // Determine the maximum y of the intersection: int new_max_y; if (rr.max_y() < max_y()) new_max_y = rr.max_y(); else new_max_y = max_y(); // Determine width and height: int new_ww = new_max_x - new_xx; int new_hh = new_max_y - new_yy; Rect new_rect = new Rect(new_xx, new_yy, new_ww, new_hh); return new_rect; }