Java Geometry Algorithm smallestBoundingBox( java.awt.Point.Double[] points)

Here you can find the source of smallestBoundingBox( java.awt.Point.Double[] points)

Description

Returns the smalles BoundingBox, which contains a number of Poins

License

Open Source License

Parameter

Parameter Description
points a parameter

Declaration

public static Rectangle2D smallestBoundingBox(
        java.awt.Point.Double[] points) 

Method Source Code

//package com.java2s;
/**//from   w  w  w  . java 2 s .com
 * This file is part of VisiCut.
 * Copyright (C) 2011 - 2013 Thomas Oster <thomas.oster@rwth-aachen.de>
 * RWTH Aachen University - 52062 Aachen, Germany
 *
 *     VisiCut is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     VisiCut 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 Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with VisiCut.  If not, see <http://www.gnu.org/licenses/>.
 **/

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;

import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * Returns the smalles BoundingBox, which contains a number of Poins
     * @param points
     * @return
     */
    public static Rectangle2D smallestBoundingBox(
            java.awt.Point.Double[] points) {
        double minX = points[0].x;
        double minY = points[0].y;
        double maxX = points[0].x;
        double maxY = points[0].y;
        for (java.awt.Point.Double p : points) {
            if (p.x < minX) {
                minX = p.x;
            }
            if (p.y < minY) {
                minY = p.y;
            }
            if (p.x > maxX) {
                maxX = p.x;
            }
            if (p.y > maxY) {
                maxY = p.y;
            }
        }
        return new Rectangle.Double(minX, minY, maxX - minX, maxY - minY);
    }

    public static Rectangle2D smallestBoundingBox(Shape s, AffineTransform t) {
        double minX = 0;
        double maxX = 0;
        double minY = 0;
        double maxY = 0;
        PathIterator pi = s.getPathIterator(t, 1);
        double[] last = null;
        boolean first = true;
        while (!pi.isDone()) {
            double[] d = new double[8];
            switch (pi.currentSegment(d)) {
            case PathIterator.SEG_LINETO: {
                if (last != null) {
                    if (first) {
                        minX = last[0];
                        maxX = last[0];
                        minY = last[1];
                        maxY = last[1];
                        first = false;
                    } else {
                        if (last[0] < minX) {
                            minX = last[0];
                        }
                        if (last[0] > maxX) {
                            maxX = last[0];
                        }
                        if (last[1] < minY) {
                            minY = last[1];
                        }
                        if (last[1] > maxY) {
                            maxY = last[1];
                        }
                    }
                }
                if (first) {
                    minX = d[0];
                    maxX = d[0];
                    minY = d[1];
                    maxY = d[1];
                    first = false;
                } else {
                    if (d[0] < minX) {
                        minX = d[0];
                    }
                    if (d[0] > maxX) {
                        maxX = d[0];
                    }
                    if (d[1] < minY) {
                        minY = d[1];
                    }
                    if (d[1] > maxY) {
                        maxY = d[1];
                    }
                }
                break;
            }
            case PathIterator.SEG_MOVETO: {
                last = d;
                break;
            }
            }
            pi.next();
        }
        return new Rectangle.Double(minX, minY, maxX - minX, maxY - minY);
    }
}

Related

  1. resample(Vector points, int n, Vector newPoints)
  2. setPathAnchor(Shape s, Point2D pt)
  3. setPathControlPoint(Shape s, int i, Point2D pt)
  4. setRevisePoint(Point2D.Double revisePoint, Point2D.Double startPoint)
  5. slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2)
  6. snapToGrid(Point original, double gridSpacing)
  7. starRunner(int[][] screen, int x, int y, List blueList)
  8. te static double t(Point2D.Double original, Point2D.Double endpt1, Point2D.Double endpt2)
  9. vecLength(final Point2D v)