Java Geometry Algorithm findDimension(Point2D[] pts)

Here you can find the source of findDimension(Point2D[] pts)

Description

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

License

Open Source License

Parameter

Parameter Description
pts an array of points ( null not permitted).

Return

The dimensions.

Declaration

public static Dimension findDimension(Point2D[] pts) 

Method Source Code


//package com.java2s;
/* ===========================================================
 * Orson Charts : a 3D chart library for the Java(tm) platform
 * ===========================================================
 * /*  w  w  w .j a  v  a2  s .  c  o  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
 * 
 */

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

  1. drag(Robot robot, Point startPoint, Point endPoint, int button)
  2. dx(Point a, Point b)
  3. edge(Collection points)
  4. extendLine(Point2D p0, Point2D p1, double toLength)
  5. findArea(Point2D p1, Point2D p2, Point2D p3)
  6. findDistancedPoint(double t, Point2D sp, Point2D c1, Point2D c2, Point2D ep)
  7. findIntersect(Point2D p1, Point2D p2, Point2D p3, Point2D p4)
  8. findIntersection(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3, Point2D.Double p4)
  9. findLineSegmentIntersection(Line2D one, Line2D two, Point2D intersection)