/** * Fills the given {@link RectF} with the path bounds. * * @param bounds the RectF to be filled. */ public void fillBounds(RectF bounds) { Rectangle2D rect = mPath.getBounds2D(); bounds.left = (float) rect.getMinX(); bounds.right = (float) rect.getMaxX(); bounds.top = (float) rect.getMinY(); bounds.bottom = (float) rect.getMaxY(); }
/** * Workaround for RFE #4093999 ("Relax constraint on placement of this()/super() call in * constructors"). */ private static MathTransform getMathTransform( final Rectangle gridRange, final Rectangle2D userRange) { final double scaleX = userRange.getWidth() / gridRange.getWidth(); final double scaleY = userRange.getHeight() / gridRange.getHeight(); final double transX = userRange.getMinX() - gridRange.x * scaleX; final double transY = userRange.getMaxY() + gridRange.y * scaleY; final AffineTransform tr = new AffineTransform(scaleX, 0, 0, -scaleY, transX, transY); tr.translate(0.5, 0.5); // Maps to pixel center return ProjectiveTransform.create(tr); }
/** * Snaps a geographic point to be on the region grid. * * <p>Moves the point given by X and Y to be on the grid of the supplied region. * * @param x the easting of the arbitrary point. * @param y the northing of the arbitrary point. * @param region the active window from which to take the grid. * @return the snapped coordinate. */ public static Coordinate snapToNextHigherInRegionResolution( double x, double y, JGrassRegion region) { double minx = region.getRectangle().getBounds2D().getMinX(); double ewres = region.getWEResolution(); double xsnap = minx + (Math.ceil((x - minx) / ewres) * ewres); double miny = region.getRectangle().getBounds2D().getMinY(); double nsres = region.getNSResolution(); double ysnap = miny + (Math.ceil((y - miny) / nsres) * nsres); return new Coordinate(xsnap, ysnap); }
private void drawStraightLabelLine( java.awt.Shape outline, boolean drawingHalo, float thickness, float offset) { Rectangle2D bounds = outline.getBounds2D().getBounds(); double minX = bounds.getMinX(); double maxX = bounds.getMaxX(); if ((Math.abs(maxX - minX) < 0.0000001)) { // nothing to draw return; } // let's se if we are drawing the halo around the underline line if (drawingHalo) { // when drawing the halo we assume that the correct halo configuration has been set graphics.draw(new Line2D.Double(minX, offset, maxX, offset)); } else { // storing the current stroke and setting the stroke according to the specified // thickness Stroke currentStroke = graphics.getStroke(); graphics.setStroke(new BasicStroke(thickness)); // we draw a line with the same color of the text and a stroke of 2 graphics.draw(new Line2D.Double(minX, offset, maxX, offset)); // we need to restore the previous stroke graphics.setStroke(currentStroke); } }
private static Rectangle2D union(Rectangle2D src1, Rectangle2D src2) { double x1 = Math.min(src1.getMinX(), src2.getMinX()); double y1 = Math.min(src1.getMinY(), src2.getMinY()); double x2 = Math.max(src1.getMaxX(), src2.getMaxX()); double y2 = Math.max(src1.getMaxY(), src2.getMaxY()); Rectangle2D result = new Rectangle2D.Double(); result.setFrameFromDiagonal(x1, y1, x2, y2); return result; }
JGrassRegion activeRegion, double x, double y) { double minx = activeRegion.getRectangle().getBounds2D().getMinX(); double ewres = activeRegion.getWEResolution(); double snapx = minx + (Math.round((x - minx) / ewres) * ewres); double miny = activeRegion.getRectangle().getBounds2D().getMinY(); double nsres = activeRegion.getNSResolution(); double snapy = miny + (Math.round((y - miny) / nsres) * nsres);
Rectangle2D applyMargins(int[] margin, Rectangle2D bounds) { if (bounds != null) { double xmin = bounds.getMinX() - margin[3]; double ymin = bounds.getMinY() - margin[0]; double width = bounds.getWidth() + margin[1] + margin[3]; double height = bounds.getHeight() + margin[0] + margin[2]; return new Rectangle2D.Double(xmin, ymin, width, height); } else { return bounds; } }
static Point2D intersection(Rectangle2D rect, Point2D pt1, Point2D pt2) { Point2D p; p = intersection(new Point2D.Double(rect.getMinX(), rect.getMinY()), new Point2D.Double(rect.getMaxX(), rect.getMinY()), pt1, pt2); if (p != null) { return p; } p = intersection(new Point2D.Double(rect.getMinX(), rect.getMaxY()), new Point2D.Double(rect.getMaxX(), rect.getMaxY()), pt1, pt2); if (p != null) { return p; } p = intersection(new Point2D.Double(rect.getMinX(), rect.getMinY()), new Point2D.Double(rect.getMinX(), rect.getMaxY()), pt1, pt2); if (p != null) { return p; } p = intersection(new Point2D.Double(rect.getMaxX(), rect.getMinY()), new Point2D.Double(rect.getMaxX(), rect.getMaxY()), pt1, pt2); if (p != null) { return p; } return null; }
/** * Returns the explicitly defined bounds for this shape. If no bounds were explicitly set, it * delegates the call to the actual shape. * * @return the Rectangle representing the Shape's bounding box. * @see Shape */ public Rectangle getBounds() { if (bounds != null) return new Rectangle( (int) bounds.getMinX(), (int) bounds.getMinY(), (int) bounds.getWidth(), (int) bounds.getHeight()); return shape.getBounds(); }
public LineRectIntersection(Line2D line, Rectangle2D rect) { final Point2D p1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); final Point2D p2 = new Point2D.Double(rect.getMaxX(), rect.getMinY()); final Point2D p3 = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); final Point2D p4 = new Point2D.Double(rect.getMinX(), rect.getMaxY()); final Point2D inter1 = new LineSegmentIntersection(new Line2D.Double(p1, p2), line).getIntersection(); final Point2D inter2 = new LineSegmentIntersection(new Line2D.Double(p2, p3), line).getIntersection(); final Point2D inter3 = new LineSegmentIntersection(new Line2D.Double(p3, p4), line).getIntersection(); final Point2D inter4 = new LineSegmentIntersection(new Line2D.Double(p4, p1), line).getIntersection(); final Point2D o = line.getP1(); inter = getCloser(o, inter1, inter2, inter3, inter4); }
/** * Converts the Rectangle2D passed as parameter in a jts Geometry object * * @param r the rectangle to be converted * @return a geometry with the same vertices as the rectangle */ private Geometry rectangleToGeometry(Rectangle2D r) { return createRectangle(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight()); }
Point2D inter = null; if (in.getX() > rect.getMinX() && in.getX() < rect.getMaxX()) { if (in.getY() < rect.getMinY()) { inter = new Point2D.Double(in.getX(), rect.getMinY()); } else if (in.getY() > rect.getMaxY()) { inter = new Point2D.Double(in.getX(), rect.getMaxY()); } else { throw new IllegalArgumentException(); } else if (in.getY() > rect.getMinY() && in.getY() < rect.getMaxY()) { if (in.getX() < rect.getMinX()) { inter = new Point2D.Double(rect.getMinX(), in.getY()); } else if (in.getX() > rect.getMaxX()) { inter = new Point2D.Double(rect.getMaxX(), in.getY()); } else { throw new IllegalArgumentException(); final Point2D p1 = new Point2D.Double(rect.getMinX(), rect.getMinY()); final Point2D p2 = new Point2D.Double(rect.getMaxX(), rect.getMinY()); final Point2D p3 = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); final Point2D p4 = new Point2D.Double(rect.getMinX(), rect.getMaxY());
/** * Converts the Rectangle2D passed as parameter in a jts Geometry object * * @param r the rectangle to be converted * @return a geometry with the same vertices as the rectangle */ private Geometry rectangleToGeometry(Rectangle2D r) { return createRectangle(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight()); }
final double ww = bounds.getMaxX() - bounds.getMinX(); final double hh = bounds.getMaxY() - bounds.getMinY(); final Graphics2D gg = destination.createGraphics(); gg.scale(dpiFactor, dpiFactor); gg.translate(deltaShadow - bounds.getMinX(), deltaShadow - bounds.getMinY()); final boolean isLine = shape instanceof Line2D.Double; if (isLine) { final AffineTransform at = g2d.getTransform(); g2d.scale(1 / dpiFactor, 1 / dpiFactor); g2d.drawImage(destination, (int) (bounds.getMinX() * dpiFactor), (int) (bounds.getMinY() * dpiFactor), null); g2d.setTransform(at);
/** Returns {@code true} if the two rectangles are equals up to an epsilon value. */ public static boolean equalsEpsilon(final Rectangle2D rect1, final Rectangle2D rect2) { double dx = 0.5 * Math.abs(rect1.getWidth() + rect2.getWidth()); double dy = 0.5 * Math.abs(rect1.getHeight() + rect2.getHeight()); if (dx > 0) dx *= EPS; else dx = EPS; if (dy > 0) dy *= EPS; else dy = EPS; return equalsEpsilon(rect1.getMinX(), rect2.getMinX(), dx) && equalsEpsilon(rect1.getMinY(), rect2.getMinY(), dy) && equalsEpsilon(rect1.getMaxX(), rect2.getMaxX(), dx) && equalsEpsilon(rect1.getMaxY(), rect2.getMaxY(), dy); }