Returns rectangle containing all specified points. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Returns rectangle containing all specified points.

Demo Code

/*//  w ww. j av a 2  s . com
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library 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.
 *
 * WebLookAndFeel library 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 WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

public class Main {
    /**
     * Returns rectangle containing all specified points.
     *
     * @param points
     *            points to process
     * @return rectangle containing all specified points
     */
    public static Rectangle getContainingRect(final List<Point> points) {
        return points != null && points.size() > 0 ? getContainingRect(points
                .toArray(new Point[points.size()])) : null;
    }

    /**
     * Returns rectangle containing all specified points.
     *
     * @param points
     *            points to process
     * @return rectangle containing all specified points
     */
    public static Rectangle getContainingRect(final Point... points) {
        if (points != null && points.length > 0) {
            final Rectangle rect = new Rectangle(points[0], new Dimension(
                    0, 0));
            int i = 1;
            while (i < points.length) {
                final Point p = points[i];
                if (p.x < rect.x) {
                    final int diff = rect.x - p.x;
                    rect.x = p.x;
                    rect.width += diff;
                } else if (rect.x + rect.width < p.x) {
                    rect.width = p.x - rect.x;
                }
                if (p.y < rect.y) {
                    final int diff = rect.y - p.y;
                    rect.y = p.y;
                    rect.height += diff;
                } else if (rect.y + rect.height < p.y) {
                    rect.height = p.y - rect.y;
                }
                i++;
            }
            if (rect.width == 0) {
                rect.width = 1;
            }
            if (rect.height == 0) {
                rect.height = 1;
            }
            return rect;
        } else {
            return null;
        }
    }

    /**
     * Returns rectangle containing all specified rectangles.
     *
     * @param rects
     *            rectangles to process
     * @return rectangle containing all specified rectangles
     */
    public static Rectangle getContainingRect(final Rectangle... rects) {
        if (rects != null && rects.length > 0) {
            Rectangle rect = rects[0];
            int i = 1;
            while (i < rects.length) {
                rect = getContainingRect(rect, rects[i]);
                i++;
            }
            return rect;
        } else {
            return null;
        }
    }

    /**
     * Returns rectangle containing two others.
     *
     * @param r1
     *            first rectangle
     * @param r2
     *            second rectangle
     * @return rectangle containing two others or null if both rectangles are
     *         null
     */
    public static Rectangle getContainingRect(final Rectangle r1,
            final Rectangle r2) {
        if (r1 == null && r2 != null) {
            return r2;
        } else if (r2 == null && r1 != null) {
            return r1;
        } else if (r1 == null && r2 == null) {
            return null;
        }

        final int minX = Math.min(r1.x, r2.x);
        final int minY = Math.min(r1.y, r2.y);
        final int maxX = Math.max(r1.x + r1.width, r2.x + r2.width);
        final int maxY = Math.max(r1.y + r1.height, r2.y + r2.height);
        return new Rectangle(minX, minY, maxX - minX, maxY - minY);
    }
}

Related Tutorials