Java Rectangle computeDifference(Rectangle rectA, Rectangle rectB)

Here you can find the source of computeDifference(Rectangle rectA, Rectangle rectB)

Description

Subtracts a rectangle from another and return the area as an array of rectangles.

License

Open Source License

Parameter

Parameter Description
rectA The first rectangle
rectB The rectangle to subtract from the first

Return

An array of rectangles representing the area in rectA not overlapped by rectB

Declaration

public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB) 

Method Source Code

//package com.java2s;
/* SwingUtilities.java --
   Copyright (C) 2002, 2004, 2005, 2006,  Free Software Foundation, Inc.
    /* w w  w  . j a  v a2  s . co m*/
This file is part of GNU Classpath.
    
GNU Classpath 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 2, or (at your option)
any later version.
    
GNU Classpath 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 GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
    
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
    
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

import java.awt.Rectangle;

public class Main {
    /**
     * Subtracts a rectangle from another and return the area as an array
     * of rectangles.
     * Returns the areas of rectA which are not covered by rectB.
     * If the rectangles do not overlap, or if either parameter is
     * <code>null</code>, a zero-size array is returned.
     * @param rectA The first rectangle
     * @param rectB The rectangle to subtract from the first
     * @return An array of rectangles representing the area in rectA
     * not overlapped by rectB
     */
    public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB) {
        if (rectA == null || rectB == null)
            return new Rectangle[0];

        Rectangle[] r = new Rectangle[4];
        int x1 = rectA.x;
        int y1 = rectA.y;
        int w1 = rectA.width;
        int h1 = rectA.height;
        int x2 = rectB.x;
        int y2 = rectB.y;
        int w2 = rectB.width;
        int h2 = rectB.height;

        // (outer box = rectA)
        // -------------
        // |_____0_____|
        // |  |rectB|  |
        // |_1|_____|_2|
        // |     3     |
        // -------------
        int H0 = (y2 > y1) ? y2 - y1 : 0; // height of box 0
        int H3 = (y2 + h2 < y1 + h1) ? y1 + h1 - y2 - h2 : 0; // height box 3
        int W1 = (x2 > x1) ? x2 - x1 : 0; // width box 1
        int W2 = (x1 + w1 > x2 + w2) ? x1 + w1 - x2 - w2 : 0; // w. box 2
        int H12 = (H0 + H3 < h1) ? h1 - H0 - H3 : 0; // height box 1 & 2

        if (H0 > 0)
            r[0] = new Rectangle(x1, y1, w1, H0);
        else
            r[0] = null;

        if (W1 > 0 && H12 > 0)
            r[1] = new Rectangle(x1, y1 + H0, W1, H12);
        else
            r[1] = null;

        if (W2 > 0 && H12 > 0)
            r[2] = new Rectangle(x2 + w2, y1 + H0, W2, H12);
        else
            r[2] = null;

        if (H3 > 0)
            r[3] = new Rectangle(x1, y1 + H0 + H12, w1, H3);
        else
            r[3] = null;

        // sort out null objects
        int n = 0;
        for (int i = 0; i < 4; i++)
            if (r[i] != null)
                n++;
        Rectangle[] out = new Rectangle[n];
        for (int i = 3; i >= 0; i--)
            if (r[i] != null)
                out[--n] = r[i];

        return out;
    }
}

Related

  1. addTo(Insets i1, Insets i2)
  2. alignComponentsBaseHorizontal(Component left, Component right)
  3. alignsVertically(Rectangle r1, Rectangle r2)
  4. clip(int x, int y, int w, int h)
  5. expand(final Rectangle rect, final int expansion)
  6. expand(Rectangle rect, int amount)
  7. expand(Rectangle rectangle, int expansion)
  8. firstSmallerThanSecond(java.awt.Rectangle first, java.awt.Rectangle second)