Returns the dimensions of the smallest rectangle that could contain the supplied points. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Returns the dimensions of the smallest rectangle that could contain the supplied points.

Demo Code

/* --------------------------------------------------------===
 * Orson Charts : a 3D chart library for the Java(tm) platform
 * --------------------------------------------------------===
 * /*from   www .j a v  a2s.co m*/
 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
 * 
 * http://www.object-refinery.com/orsoncharts/index.html
 * 
 * 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/>.
 * 
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.]
 * 
 * If you do not wish to be bound by the terms of the GPL, an alternative
 * commercial license can be purchased.  For details, please see visit the
 * Orson Charts home page:
 * 
 * http://www.object-refinery.com/orsoncharts/index.html
 * 
 */
//package com.java2s;
import java.awt.Dimension;

import java.awt.geom.Point2D;

public class Main {
    /**
     * Returns the dimensions of the smallest rectangle that could contain
     * the supplied points.
     * 
     * @param pts  an array of points ({@code null} not permitted).
     * 
     * @return The dimensions.
     */
    public static Dimension findDimension(Point2D[] pts) {
        double minx = Double.POSITIVE_INFINITY;
        double maxx = Double.NEGATIVE_INFINITY;
        double miny = Double.POSITIVE_INFINITY;
        double maxy = Double.NEGATIVE_INFINITY;
        for (Point2D pt : pts) {
            minx = Math.min(minx, pt.getX());
            maxx = Math.max(maxx, pt.getX());
            miny = Math.min(miny, pt.getY());
            maxy = Math.max(maxy, pt.getY());
        }
        return new Dimension((int) (maxx - minx), (int) (maxy - miny));
    }
}

Related Tutorials