List of usage examples for org.apache.commons.math3.geometry.euclidean.twod.hull MonotoneChain MonotoneChain
public MonotoneChain()
From source file:gdsc.core.utils.ConvexHull.java
/** * Create a new convex hull from the given coordinates. * * @param xbase/* w ww.j a v a2 s .c o m*/ * the x base coordinate (origin) * @param ybase * the y base coordinate (origin) * @param xCoordinates * the x coordinates * @param yCoordinates * the y coordinates * @param n * the number of coordinates * @return the convex hull * @throws NullPointerException * if the inputs are null * @throws ArrayIndexOutOfBoundsException * if the yCoordinates are smaller than the xCoordinates */ public static ConvexHull create(float xbase, float ybase, float[] xCoordinates, float[] yCoordinates, int n) { // This algorithm is not suitable for floating point coords //return createGiftWrap(xbase, ybase, xCoordinates, yCoordinates, n); // Use Apache Math to do this MonotoneChain chain = new MonotoneChain(); TurboList<Vector2D> points = new TurboList<Vector2D>(n); for (int i = 0; i < n; i++) points.add(new Vector2D(xbase + xCoordinates[i], ybase + yCoordinates[i])); ConvexHull2D hull = null; try { hull = chain.generate(points); } catch (ConvergenceException e) { } if (hull == null) return null; Vector2D[] v = hull.getVertices(); if (v.length == 0) return null; int size = v.length; float[] xx = new float[size]; float[] yy = new float[size]; int n2 = 0; for (int i = 0; i < size; i++) { xx[n2] = (float) v[i].getX(); yy[n2] = (float) v[i].getY(); n2++; } return new ConvexHull(0, 0, xx, yy); }