/* * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package org.activityinfo.legacy.shared.reports.model; /* * #%L * ActivityInfo Server * %% * Copyright (C) 2009 - 2013 UNICEF * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ public class Rectangle { private int x; private int y; private int width; private int height; public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } /** * Determines whether or not this <code>Rectangle</code> and the specified * <code>Rectangle</code> intersect. Two rectangles intersect if their * intersection is nonempty. * * @param r the specified <code>Rectangle</code> * @return <code>true</code> if the specified <code>Rectangle</code> and * this <code>Rectangle</code> intersect; <code>false</code> * otherwise. */ public boolean intersects(Rectangle r) { int tw = this.width; int th = this.height; int rw = r.width; int rh = r.height; if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) { return false; } int tx = this.x; int ty = this.y; int rx = r.x; int ry = r.y; rw += rx; rh += ry; tw += tx; th += ty; // overflow || intersect return ((rw < rx || rw > tx) && (rh < ry || rh > ty) && (tw < tx || tw > rx) && (th < ty || th > ry)); } /** * Computes the intersection of this <code>Rectangle</code> with the * specified <code>Rectangle</code>. Returns a new <code>Rectangle</code> * that represents the intersection of the two rectangles. If the two * rectangles do not intersect, the result will be an empty rectangle. * * @param r the specified <code>Rectangle</code> * @return the largest <code>Rectangle</code> contained in both the * specified <code>Rectangle</code> and in this * <code>Rectangle</code>; or if the rectangles do not intersect, an * empty rectangle. */ public Rectangle intersection(Rectangle r) { int tx1 = this.x; int ty1 = this.y; int rx1 = r.x; int ry1 = r.y; long tx2 = tx1; tx2 += this.width; long ty2 = ty1; ty2 += this.height; long rx2 = rx1; rx2 += r.width; long ry2 = ry1; ry2 += r.height; if (tx1 < rx1) { tx1 = rx1; } if (ty1 < ry1) { ty1 = ry1; } if (tx2 > rx2) { tx2 = rx2; } if (ty2 > ry2) { ty2 = ry2; } tx2 -= tx1; ty2 -= ty1; // tx2,ty2 will never overflow (they will never be // larger than the smallest of the two source w,h) // they might underflow, though... if (tx2 < Integer.MIN_VALUE) { tx2 = Integer.MIN_VALUE; } if (ty2 < Integer.MIN_VALUE) { ty2 = Integer.MIN_VALUE; } return new Rectangle(tx1, ty1, (int) tx2, (int) ty2); } }