Polygon2D.Double convexHull = new Polygon2D.Double(); for (Point2D.Double p : getConvexHull2D(points.toArray(new Point2D.Double[points.size()]))) { convexHull.add(p);
@Override public Rectangle2D getBounds2D() { if (npoints == 0) { return new Rectangle2D.Double(); } if (bounds == null) { calculateBounds(xpoints, ypoints, npoints); } return (Rectangle2D) bounds.clone(); }
@Override public void addPoint(double x, double y) { if (npoints >= xpoints.length || npoints >= ypoints.length) { int newLength = npoints * 2; // Make sure that newLength will be greater than MIN_LENGTH and // aligned to the power of 2 if (newLength < MIN_LENGTH) { newLength = MIN_LENGTH; } else if ((newLength & (newLength - 1)) != 0) { newLength = Integer.highestOneBit(newLength); } double[] helper = new double[newLength]; System.arraycopy(xpoints, 0, helper, 0, npoints); xpoints = helper; helper = new double[newLength]; System.arraycopy(ypoints, 0, helper, 0, npoints); ypoints = helper; } xpoints[npoints] = x; ypoints[npoints] = y; npoints++; if (bounds != null) { updateBounds(x, y); } }
@Override public boolean contains(double x, double y) { if (npoints <= 2 || !getBounds2D().contains(x, y)) { return false;
@Override public boolean contains(double x, double y, double w, double h) { if (npoints <= 0 || !getBounds2D().intersects(x, y, w, h)) { return false; } Crossings cross = getCrossings(x, y, x + w, y + h); return (cross != null && cross.covers(y, y + h)); }
@Override public boolean intersects(double x, double y, double w, double h) { if (npoints <= 0 || !getBounds2D().intersects(x, y, w, h)) { return false; } Crossings cross = getCrossings(x, y, x + w, y + h); return (cross == null || !cross.isEmpty()); }
/** * Computes the convex hull from a set of points. * * @param points * @return convex hull of the points as a Polygon2D object. */ public static Polygon2D.Double getConvexHullPath2D(List<Point2D.Double> points) { Polygon2D.Double convexHull = new Polygon2D.Double(); for (Point p : getConvexHull(points.toArray(new Point[points.size()]))) { convexHull.add(p); } return convexHull; }