Search in sources :

Example 86 with Path2D

use of java.awt.geom.Path2D in project imagingbook-common by imagingbook.

the class RoiUtils method makePolygon.

@Deprecated
public static Roi makePolygon(Point2D[] points, double strokeWidth, Color color) {
    Path2D poly = new Path2D.Double();
    if (points.length > 0) {
        poly.moveTo(points[0].getX(), points[0].getY());
        for (int i = 1; i < points.length; i++) {
            poly.lineTo(points[i].getX(), points[i].getY());
        }
        poly.closePath();
    }
    Roi shapeRoi = new ShapeRoi(poly);
    shapeRoi.setStrokeWidth(strokeWidth);
    shapeRoi.setStrokeColor(color);
    return shapeRoi;
}
Also used : ShapeRoi(ij.gui.ShapeRoi) Path2D(java.awt.geom.Path2D) ShapeRoi(ij.gui.ShapeRoi) Roi(ij.gui.Roi)

Example 87 with Path2D

use of java.awt.geom.Path2D in project sis by apache.

the class Java2D method tryMergePolylines.

/**
 * Merges a sequence of points or paths if the first instance is an implementation of this library.
 *
 * @throws ClassCastException if an element in the iterator is not a {@link Shape} or a {@link Point2D}.
 */
@Override
final Shape tryMergePolylines(Object next, final Iterator<?> polylines) {
    if (!(next instanceof Shape || next instanceof Point2D)) {
        return null;
    }
    final Path2D path = new Path2D.Double();
    boolean lineTo = false;
    for (; ; next = polylines.next()) {
        if (next != null) {
            if (next instanceof Point2D) {
                final double x = ((Point2D) next).getX();
                final double y = ((Point2D) next).getY();
                if (Double.isNaN(x) || Double.isNaN(y)) {
                    lineTo = false;
                } else if (lineTo) {
                    path.lineTo(x, y);
                } else {
                    path.moveTo(x, y);
                    lineTo = true;
                }
            } else {
                path.append((Shape) next, false);
                lineTo = false;
            }
        }
        if (!polylines.hasNext()) {
            // to skip this condition during the first iteration.
            break;
        }
    }
    return ShapeUtilities.toPrimitive(path);
}
Also used : Shape(java.awt.Shape) Point2D(java.awt.geom.Point2D) Path2D(java.awt.geom.Path2D)

Example 88 with Path2D

use of java.awt.geom.Path2D in project sis by apache.

the class Java2D method createPolyline.

/**
 * Creates a path from the given ordinate values.
 * Each {@link Double#NaN} ordinate value start a new path.
 * The implementation returned by this method must be an instance of {@link #rootClass}.
 */
@Override
public Shape createPolyline(final int dimension, final Vector... ordinates) {
    if (dimension != 2) {
        throw unsupported(dimension);
    }
    /*
         * Computes the total length of all vectors and verifies if all values
         * can be casted to float without precision lost.
         */
    int length = 0;
    boolean isFloat = true;
    for (final Vector v : ordinates) {
        if (v != null) {
            length = Math.addExact(length, v.size());
            if (isFloat) {
                for (int i = v.size(); --i >= 0; ) {
                    final double value = v.doubleValue(i);
                    if (Double.doubleToRawLongBits(value) != Double.doubleToRawLongBits((float) value)) {
                        isFloat = false;
                        break;
                    }
                }
            }
        }
    }
    /*
         * Note: Point2D is not an instance of Shape, so we can not make a special case for it.
         */
    length /= 2;
    if (length == 2 && ordinates.length == 1) {
        final Vector v = ordinates[0];
        final double x1, y1, x2, y2;
        if (!Double.isNaN(x1 = v.doubleValue(0)) && !Double.isNaN(y1 = v.doubleValue(1)) && !Double.isNaN(x2 = v.doubleValue(2)) && !Double.isNaN(y2 = v.doubleValue(3))) {
            final Line2D path = isFloat ? new Line2D.Float() : new Line2D.Double();
            path.setLine(x1, y1, x2, y2);
            return path;
        }
    }
    final Path2D path = isFloat ? new Path2D.Float(Path2D.WIND_NON_ZERO, length) : new Path2D.Double(Path2D.WIND_NON_ZERO, length);
    boolean lineTo = false;
    for (final Vector v : ordinates) {
        final int size = v.size();
        for (int i = 0; i < size; ) {
            final double x = v.doubleValue(i++);
            final double y = v.doubleValue(i++);
            if (Double.isNaN(x) || Double.isNaN(y)) {
                lineTo = false;
            } else if (lineTo) {
                path.lineTo(x, y);
            } else {
                path.moveTo(x, y);
                lineTo = true;
            }
        }
    }
    return ShapeUtilities.toPrimitive(path);
}
Also used : Path2D(java.awt.geom.Path2D) Vector(org.apache.sis.math.Vector) Line2D(java.awt.geom.Line2D)

Example 89 with Path2D

use of java.awt.geom.Path2D in project sis by apache.

the class LatLonPointRadius method getRectangularRegionApproximation.

/**
 * Calculates the rectangular region enclosing the circular search region.
 *
 * @param numberOfPoints
 *          the number of points used to estimate the circular search region
 * @return Java Rectangle2D object that bounds the circlar search region
 */
public Rectangle2D getRectangularRegionApproximation(int numberOfPoints) {
    if (radius >= DistanceUtils.HALF_EARTH_CIRCUMFERENCE) {
        return new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0);
    }
    int numberOfCrossOvers = 0;
    Path2D path = new Path2D.Double();
    DirectPosition2D initPT = DistanceUtils.getPointOnGreatCircle(center.getOrdinate(1), center.getOrdinate(0), radius, 0);
    path.moveTo(initPT.x + 180.0, initPT.y + 90.0);
    DirectPosition2D currPT = initPT;
    for (int i = 1; i < 360; i++) {
        DirectPosition2D pt = DistanceUtils.getPointOnGreatCircle(center.getOrdinate(1), center.getOrdinate(0), radius, i);
        path.lineTo(pt.x + 180.0, pt.y + 90.0);
        if (dateLineCrossOver(Longitude.normalize(currPT.x), Longitude.normalize(pt.x))) {
            numberOfCrossOvers++;
        }
        currPT = pt;
    }
    if (dateLineCrossOver(Longitude.normalize(initPT.x), Longitude.normalize(currPT.x))) {
        numberOfCrossOvers++;
    }
    /**
     * If the path crosses the dateline once, it's a special case, so take care
     * of it differently. It will need to include areas around the pole.
     */
    if (numberOfCrossOvers == 1) {
        Rectangle2D r = path.getBounds2D();
        Rectangle2D lowerHalf = new Rectangle2D.Double(0.0, 0.0, 360.0, r.getMaxY());
        if (lowerHalf.contains(center.getOrdinate(0) + 180.0, center.getOrdinate(1) + 90.0)) {
            return lowerHalf;
        } else {
            return new Rectangle2D.Double(0.0, r.getMinY(), 360.0, 180.0 - r.getMinY());
        }
    }
    if (path.contains(center.getOrdinate(0) + 180.0, center.getOrdinate(1) + 90.0)) {
        Rectangle2D r = path.getBounds2D();
        if ((r.getMaxX() - r.getMinX()) > 359.0) {
            return new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0);
        } else if (r.getMinX() < 0 || r.getMaxX() > 360.0) {
            /**
             * For circles that crosses the dateline instead of splitting in half
             * and having to go down the tree twice, for first version span
             * longitude 360.0 and use the exact height of the box
             */
            return new Rectangle2D.Double(0.0, r.getY(), 360.0, r.getHeight());
        } else {
            return path.getBounds2D();
        }
    } else {
        Area pathArea = new Area(path);
        Area wholeMap = new Area(new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0));
        wholeMap.subtract(pathArea);
        return wholeMap.getBounds2D();
    }
}
Also used : Area(java.awt.geom.Area) Path2D(java.awt.geom.Path2D) Rectangle2D(java.awt.geom.Rectangle2D) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D)

Example 90 with Path2D

use of java.awt.geom.Path2D in project flutter-intellij by flutter.

the class StackLayout method paintComponent.

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    final Rectangle bounds = getBounds();
    final int height = bounds.height;
    if (height <= 20) {
        return;
    }
    final Graphics2D g2 = (Graphics2D) g;
    final float msPerPixel = (2.0f * 1000000.0f / 60.0f) / height;
    final float y = FlutterFramesMonitor.microsPerFrame / msPerPixel;
    final Stroke oldStroke = g2.getStroke();
    try {
        g2.setStroke(STROKE);
        final Path2D path = new Path2D.Float();
        path.moveTo(0, height - y);
        path.lineTo(bounds.width, height - y);
        g2.draw(path);
    } finally {
        g2.setStroke(oldStroke);
    }
}
Also used : Path2D(java.awt.geom.Path2D)

Aggregations

Path2D (java.awt.geom.Path2D)126 Point2D (java.awt.geom.Point2D)20 Area (java.awt.geom.Area)16 Rectangle2D (java.awt.geom.Rectangle2D)13 Shape (java.awt.Shape)9 Point (java.awt.Point)8 Line2D (java.awt.geom.Line2D)8 PathIterator (java.awt.geom.PathIterator)8 ArrayList (java.util.ArrayList)8 AffineTransform (java.awt.geom.AffineTransform)7 GeneralPath (java.awt.geom.GeneralPath)7 Color (java.awt.Color)6 Graphics2D (java.awt.Graphics2D)6 Paint (java.awt.Paint)6 ShapeRoi (ij.gui.ShapeRoi)5 BasicStroke (java.awt.BasicStroke)4 RadialGradientPaint (java.awt.RadialGradientPaint)4 Point2D_F64 (georegression.struct.point.Point2D_F64)3 RoundRectangle2D (java.awt.geom.RoundRectangle2D)3 Vector2D (de.gurkenlabs.litiengine.util.geom.Vector2D)2