/** * Point in polygon test, based on * * <p>http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html * * <p>by W. Randolph Franklin * * @param v Point to test * @return True when contained. */ public boolean containsPoint2D(Vector v) { assert (v.getDimensionality() == 2); final double testx = v.get(0); final double testy = v.get(1); boolean c = false; Iterator<Vector> it = points.iterator(); Vector pre = points.get(points.size() - 1); while (it.hasNext()) { final Vector cur = it.next(); final double curx = cur.get(0); final double cury = cur.get(1); final double prex = pre.get(0); final double prey = pre.get(1); if (((cury > testy) != (prey > testy))) { if ((testx < (prex - curx) * (testy - cury) / (prey - cury) + curx)) { c = !c; } } pre = cur; } return c; }
/** * Constructor. * * @param points Polygon points */ public Polygon(List<Vector> points) { super(); this.points = points; // Compute the bounds. if (points.size() > 0) { final Iterator<Vector> iter = points.iterator(); final Vector first = iter.next(); final int dim = first.getDimensionality(); min = first.getArrayCopy(); max = first.getArrayCopy(); while (iter.hasNext()) { Vector next = iter.next(); for (int i = 0; i < dim; i++) { final double cur = next.get(i); min[i] = Math.min(min[i], cur); max[i] = Math.max(max[i], cur); } } } }