Checks if rectangle r1 is inside rectangle r2 - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Checks if rectangle r1 is inside rectangle r2

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*from  w w w .j  av a  2  s  .  c o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {


    /**
     * Checks if rectangle r1 is inside rectangle r2
     * 
     * @param r1X
     * @param r1Y
     * @param r1Width
     * @param r1Height
     * @param r2X
     * @param r2Y
     * @param r2Width
     * @param r2Height
     * @return
     */
    public static boolean isInside(final int r1X, final int r1Y,
            int r1Width, int r1Height, int r2X, int r2Y, int r2Width,
            int r2Height) {
        if ((r2Width | r2Height | r1Width | r1Height) < 0) {
            return false;
        }
        if ((r1X < r2X) || (r1Y < r2Y)) {
            return false;
        }
        r2Width += r2X;
        r1Width += r1X;
        if (r1Width <= r1X) {
            if ((r2Width >= r2X) || (r1Width > r2Width)) {
                return false;
            }
        } else if ((r2Width >= r2X) && (r1Width > r2Width)) {
            return false;
        }
        r2Height += r2Y;
        r1Height += r1Y;
        if (r1Height <= r1Y) {
            if ((r2Height >= r2Y) || (r1Height > r2Height)) {
                return false;
            }
        } else if ((r2Height >= r2Y) && (r1Height > r2Height)) {
            return false;
        }
        return true;
    }
}

Related Tutorials