/** * Draws a Healpix pixel according to a coordinate system. * * <p>When the pixel area is "small", only the four points of the pixel are used to plot it. When * the pixel area is "large", intermediate points on boundaries pixel are automatically computed * to smooth the boundary of the pixel on the plot. * * @param graphic2D graph component to decore * @param healpix Healpix index * @param pix Pixel to draw * @param coordinateTransformation Coordinate transformation */ @Override protected final void drawHealpixPolygon( final Graphics2D graphic2D, final HealpixIndex healpix, final long pix, final CoordinateTransformation coordinateTransformation) { try { final int numberOfVectors = computeNumberPointsForPixel(getHealpixBase().getNside(), pix); final Vec3[] vectors = healpix.boundaries(pix, numberOfVectors); computeReferenceFrameTransformation(vectors, coordinateTransformation); final Coordinates[] shapes = splitHealpixPixelForDetectedBorder(vectors); final Polygon2D poly = new Polygon2D(); for (int i = 0; i < shapes.length; i++) { final Coordinates shape = shapes[i]; final List<Point2D.Double> pixels = shape.getPixelsFromProjection( this.getProjection(), getRange(), getPixelWidth(), getPixelHeight()); poly.setPoints(pixels); graphic2D.draw(poly); graphic2D.fill(poly); } } catch (Exception ex) { LOG.log(Level.SEVERE, null, ex); } }
/* * get the associated {@link Polygon2D}. * This method take care that may be the last point can * be equal to the first. In that case it must not be included in the Polygon, * as polygons declare their first point only once. */ public Polygon2D getPolygon2D() { Polygon2D pol = new Polygon2D(); for (int i = 0; i < npoints - 1; i++) pol.addPoint(xpoints[i], ypoints[i]); Point2D.Double p0 = new Point2D.Double(xpoints[0], ypoints[0]); Point2D.Double p1 = new Point2D.Double(xpoints[npoints - 1], ypoints[npoints - 1]); if (p0.distance(p1) > ASSUME_ZERO) pol.addPoint(xpoints[npoints - 1], ypoints[npoints - 1]); return pol; }
protected List<Vec2D> createInsidePoints(Polygon2D poly, Rect bounds) { List<Vec2D> points = new ArrayList<Vec2D>(); for (float y = bounds.y; y < bounds.getBottom(); y += res) { float yy = MathUtils.roundTo(y, res); for (float x = bounds.x; x < bounds.getRight(); x += res) { Vec2D p = new Vec2D(MathUtils.roundTo(x, res), yy); if (poly.containsPoint(p)) { points.add(p); } } } return points; }
/** * Redefines this polygon with a given list of vertices, in local coordinates, with clockwise * winding. * * <p>The given list of vertices should start with either the bottom-left or top-right vertex of * the rectangle, as this will determine the orientation of height vs. width. * * @param vertices An ordered list of vertices, relative to the rectangles's center point, * describing a rectangle with clockwise winding. */ @Override public void setVertices(List<Vector2D> vertices) { // check to make sure there is 4 vertices if (vertices.size() != 4) { throw new IllegalArgumentException( "Invalid vertices: A rectangle must " + "be defined with exactly 4 vertices. " + vertices.size() + " given."); } // For a polygon to be a rectangle // angle between adjacent edges must be 90 degrees // Make sure the lines are parallel Vector2D v1 = vertices.get(0); Vector2D v2 = vertices.get(1); Vector2D v3 = vertices.get(2); Vector2D v4 = vertices.get(3); // see if v1-v2 is perp to v2-v3 and perp to v1-v4 if ((v2.subtract(v1).dot(v3.subtract(v2))) != 0 || (v2.subtract(v1).dot(v4.subtract(v1))) != 0) { throw new IllegalArgumentException( "Invalid vertices: Given vertices " + "do not describe a rectangle:\n" + vertices); } // see if v3-v4 is perp to v4-v1 and perp to v2-v3 if ((v4.subtract(v3).dot(v4.subtract(v1))) != 0 || (v4.subtract(v3).dot(v3.subtract(v2))) != 0) { throw new IllegalArgumentException( "Invalid vertices: Given vertices " + "do not describe a rectangle:\n" + vertices); } // set vertices super.setVertices(vertices); // set height and width this.height = v1.distance(v2); this.width = v2.distance(v3); }
private void computePolygon() { polygon = new Polygon2D(); for (GPXPoint point : points) { polygon.addPoint((float) point.getLon(), (float) point.getLat()); } }
public boolean includes(GPXPoint p) { if (polygon == null) { computePolygon(); } return polygon.contains(p.getLon(), p.getLat()); }