Example usage for java.awt.geom PathIterator next

List of usage examples for java.awt.geom PathIterator next

Introduction

In this page you can find the example usage for java.awt.geom PathIterator next.

Prototype

public void next();

Source Link

Document

Moves the iterator to the next segment of the path forwards along the primary direction of traversal as long as there are more points in that direction.

Usage

From source file:it.unibo.alchemist.model.implementations.linkingrules.ConnectionBeam.java

private boolean projectedBeamOvercomesObstacle(final Position pos1, final Position pos2) {
    final double p1x = pos1.getCoordinate(0);
    final double p1y = pos1.getCoordinate(1);
    final double p2x = pos2.getCoordinate(0);
    final double p2y = pos2.getCoordinate(1);
    final double x = p2x - p1x;
    final double y = p2y - p1y;
    /*/*  w  w w .  j ava2 s  . com*/
     * Compute the angle
     */
    final double angle = atan2(y, x);
    /*
     * Deduce surrounding beam vertices
     */
    final double dx = range * cos(PI / 2 + angle);
    final double dy = range * sin(PI / 2 + angle);
    /*
     * Enlarge the beam
     */
    final double cx = range * cos(angle);
    final double cy = range * sin(angle);
    /*
     * Create the beam
     */
    final Path2D.Double beamShape = new Path2D.Double();
    beamShape.moveTo(p1x + dx - cx, p1y + dy - cy);
    beamShape.lineTo(p1x - dx - cx, p1y - dy - cy);
    beamShape.lineTo(p2x - dx + cx, p2y - dy + cy);
    beamShape.lineTo(p2x + dx + cx, p2y + dy + cy);
    beamShape.closePath();
    final Area beam = new Area(beamShape);
    /*
     * Perform subtraction
     */
    beam.subtract(obstacles);
    /*
     * Rebuild single areas
     */
    final List<Path2D.Double> subareas = new ArrayList<>();
    Path2D.Double curpath = new Path2D.Double();
    final PathIterator pi = beam.getPathIterator(null);
    final double[] coords = new double[COORDS];
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            curpath = new Path2D.Double();
            curpath.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            curpath.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_CLOSE:
            curpath.closePath();
            subareas.add(curpath);
            break;
        default:
            throw new IllegalArgumentException();
        }
        pi.next();
    }
    /*
     * At least one area must contain both points
     */
    for (final Path2D.Double p : subareas) {
        if (p.contains(p1x, p1y) && p.contains(p2x, p2y)) {
            return true;
        }
    }
    return false;
}

From source file:ExtendedGeneralPath.java

/**
 * Delegates to the enclosed <code>GeneralPath</code>.
 *//*from ww w. jav a  2s  . c o m*/
public void append(PathIterator pi, boolean connect) {
    double[] vals = new double[6];

    while (!pi.isDone()) {
        Arrays.fill(vals, 0);
        int type = pi.currentSegment(vals);
        pi.next();
        if (connect && (numVals != 0)) {
            if (type == PathIterator.SEG_MOVETO) {
                double x = vals[0];
                double y = vals[1];
                if ((x != cx) || (y != cy)) {
                    // Change MOVETO to LINETO.
                    type = PathIterator.SEG_LINETO;
                } else {
                    // Redundent segment (move to current loc) drop it...
                    if (pi.isDone())
                        break; // Nothing interesting
                    type = pi.currentSegment(vals);
                    pi.next();
                }
            }
            connect = false;
        }

        switch (type) {
        case PathIterator.SEG_CLOSE:
            closePath();
            break;
        case PathIterator.SEG_MOVETO:
            moveTo((float) vals[0], (float) vals[1]);
            break;
        case PathIterator.SEG_LINETO:
            lineTo((float) vals[0], (float) vals[1]);
            break;
        case PathIterator.SEG_QUADTO:
            quadTo((float) vals[0], (float) vals[1], (float) vals[2], (float) vals[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            curveTo((float) vals[0], (float) vals[1], (float) vals[2], (float) vals[3], (float) vals[4],
                    (float) vals[5]);
            break;
        }
    }
}

From source file:com.jhlabs.awt.TextStroke.java

public float measurePathLength(Shape shape) {
    PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS);
    float points[] = new float[6];
    float moveX = 0, moveY = 0;
    float lastX = 0, lastY = 0;
    float thisX = 0, thisY = 0;
    int type = 0;
    float total = 0;

    while (!it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            moveX = lastX = points[0];//from w w w  .j  ava 2  s  . c  o  m
            moveY = lastY = points[1];
            break;

        case PathIterator.SEG_CLOSE:
            points[0] = moveX;
            points[1] = moveY;
            // Fall into....

        case PathIterator.SEG_LINETO:
            thisX = points[0];
            thisY = points[1];
            float dx = thisX - lastX;
            float dy = thisY - lastY;
            total += (float) Math.sqrt(dx * dx + dy * dy);
            lastX = thisX;
            lastY = thisY;
            break;
        }
        it.next();
    }

    return total;
}

From source file:com.t_oster.visicut.misc.Helper.java

public static Rectangle2D smallestBoundingBox(Shape s, AffineTransform t) {
    double minX = 0;
    double maxX = 0;
    double minY = 0;
    double maxY = 0;
    PathIterator pi = s.getPathIterator(t, 1);
    double[] last = null;
    boolean first = true;
    while (!pi.isDone()) {
        double[] d = new double[8];
        switch (pi.currentSegment(d)) {
        case PathIterator.SEG_LINETO: {
            if (last != null) {
                if (first) {
                    minX = last[0];/*  w  w w .  j a  v  a 2s.c  om*/
                    maxX = last[0];
                    minY = last[1];
                    maxY = last[1];
                    first = false;
                } else {
                    if (last[0] < minX) {
                        minX = last[0];
                    }
                    if (last[0] > maxX) {
                        maxX = last[0];
                    }
                    if (last[1] < minY) {
                        minY = last[1];
                    }
                    if (last[1] > maxY) {
                        maxY = last[1];
                    }
                }
            }
            if (first) {
                minX = d[0];
                maxX = d[0];
                minY = d[1];
                maxY = d[1];
                first = false;
            } else {
                if (d[0] < minX) {
                    minX = d[0];
                }
                if (d[0] > maxX) {
                    maxX = d[0];
                }
                if (d[1] < minY) {
                    minY = d[1];
                }
                if (d[1] > maxY) {
                    maxY = d[1];
                }
            }
            break;
        }
        case PathIterator.SEG_MOVETO: {
            last = d;
            break;
        }
        }
        pi.next();
    }
    return new Rectangle.Double(minX, minY, maxX - minX, maxY - minY);
}

From source file:PathLength.java

/**
 * Flattens the path and determines the path length.
 */// www. ja  va2s .c  om
protected void initialise() {
    pathLength = 0f;

    PathIterator pi = path.getPathIterator(new AffineTransform());
    SingleSegmentPathIterator sspi = new SingleSegmentPathIterator();
    segments = new ArrayList(20);
    List indexes = new ArrayList(20);
    int index = 0;
    int origIndex = -1;
    float lastMoveX = 0f;
    float lastMoveY = 0f;
    float currentX = 0f;
    float currentY = 0f;
    float[] seg = new float[6];
    int segType;

    segments.add(new PathSegment(PathIterator.SEG_MOVETO, 0f, 0f, 0f, origIndex));

    while (!pi.isDone()) {
        origIndex++;
        indexes.add(new Integer(index));
        segType = pi.currentSegment(seg);
        switch (segType) {
        case PathIterator.SEG_MOVETO:
            segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
            currentX = seg[0];
            currentY = seg[1];
            lastMoveX = currentX;
            lastMoveY = currentY;
            index++;
            pi.next();
            break;
        case PathIterator.SEG_LINETO:
            pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
            segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
            currentX = seg[0];
            currentY = seg[1];
            index++;
            pi.next();
            break;
        case PathIterator.SEG_CLOSE:
            pathLength += Point2D.distance(currentX, currentY, lastMoveX, lastMoveY);
            segments.add(new PathSegment(PathIterator.SEG_LINETO, lastMoveX, lastMoveY, pathLength, origIndex));
            currentX = lastMoveX;
            currentY = lastMoveY;
            index++;
            pi.next();
            break;
        default:
            sspi.setPathIterator(pi, currentX, currentY);
            FlatteningPathIterator fpi = new FlatteningPathIterator(sspi, 0.01f);
            while (!fpi.isDone()) {
                segType = fpi.currentSegment(seg);
                if (segType == PathIterator.SEG_LINETO) {
                    pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
                    segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
                    currentX = seg[0];
                    currentY = seg[1];
                    index++;
                }
                fpi.next();
            }
        }
    }
    segmentIndexes = new int[indexes.size()];
    for (int i = 0; i < segmentIndexes.length; i++) {
        segmentIndexes[i] = ((Integer) indexes.get(i)).intValue();
    }
    initialised = true;
}

From source file:SWTGraphics2D.java

/**
 * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>.
 *
 * @param shape  the shape (<code>null</code> not permitted).
 *
 * @return The path./*from   w ww.  j a  v  a 2s  . co  m*/
 */
private Path toSwtPath(Shape shape) {
    int type;
    float[] coords = new float[6];
    Path path = new Path(this.gc.getDevice());
    PathIterator pit = shape.getPathIterator(null);
    while (!pit.isDone()) {
        type = pit.currentSegment(coords);
        switch (type) {
        case (PathIterator.SEG_MOVETO):
            path.moveTo(coords[0], coords[1]);
            break;
        case (PathIterator.SEG_LINETO):
            path.lineTo(coords[0], coords[1]);
            break;
        case (PathIterator.SEG_QUADTO):
            path.quadTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        case (PathIterator.SEG_CUBICTO):
            path.cubicTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;
        case (PathIterator.SEG_CLOSE):
            path.close();
            break;
        default:
            break;
        }
        pit.next();
    }
    return path;
}

From source file:com.jhlabs.awt.TextStroke.java

public Shape createStrokedShape(Shape shape) {
    FontRenderContext frc = new FontRenderContext(null, true, true);
    GlyphVector glyphVector = font.createGlyphVector(frc, text);

    GeneralPath result = new GeneralPath();
    PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS);
    float points[] = new float[6];
    float moveX = 0, moveY = 0;
    float lastX = 0, lastY = 0;
    float thisX = 0, thisY = 0;
    int type = 0;
    float next = 0;
    int currentChar = 0;
    int length = glyphVector.getNumGlyphs();

    if (length == 0)
        return result;

    float factor = stretchToFit ? measurePathLength(shape) / (float) glyphVector.getLogicalBounds().getWidth()
            : 1.0f;//from   w w  w. j a  v  a2  s . co  m
    float height = (float) glyphVector.getLogicalBounds().getHeight();
    float nextAdvance = 0;

    while (currentChar < length && !it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            moveX = lastX = points[0];
            moveY = lastY = points[1];
            result.moveTo(moveX, moveY);
            nextAdvance = glyphVector.getGlyphMetrics(currentChar).getAdvance() * 0.5f;
            next = nextAdvance;
            break;

        case PathIterator.SEG_CLOSE:
            points[0] = moveX;
            points[1] = moveY;
            // Fall into....

        case PathIterator.SEG_LINETO:
            thisX = points[0];
            thisY = points[1];
            float dx = thisX - lastX;
            float dy = thisY - lastY;
            float distance = (float) FastMath.sqrt(dx * dx + dy * dy);
            if (distance >= next) {
                float r = 1.0f / distance;
                float angle = (float) FastMath.atan2(dy, dx);
                while (currentChar < length && distance >= next) {
                    Shape glyph = glyphVector.getGlyphOutline(currentChar);
                    Point2D p = glyphVector.getGlyphPosition(currentChar);
                    float px = (float) p.getX();
                    float py = (float) p.getY();
                    float x = lastX + next * dx * r;
                    float y = lastY + next * dy * r;
                    float advance = nextAdvance;
                    nextAdvance = currentChar < length - 1
                            ? glyphVector.getGlyphMetrics(currentChar + 1).getAdvance() * 0.5f
                            : 0;
                    t.setToTranslation(x, y);
                    t.rotate(angle);
                    t.translate(-px - advance, -py + height * factor / 2.0f);
                    result.append(t.createTransformedShape(glyph), false);
                    next += (advance + nextAdvance) * factor;
                    currentChar++;
                    if (repeat)
                        currentChar %= length;
                }
            }
            next -= distance;
            lastX = thisX;
            lastY = thisY;
            break;
        }
        it.next();
    }

    return result;
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Handle the Batik drawing event/*from   w  w w  .j  a va 2  s  .c o m*/
 *
 * @param shape
 *            the shape to draw
 * @param fill
 *            true if the shape is to be drawn filled
 */
private void doDrawing(Shape shape, boolean fill) {
    if (!fill) {
        graphicsObj.newSegment();
    }

    graphicsObj.setColor(gc.getColor());

    applyPaint(gc.getPaint(), fill);

    if (fill) {
        graphicsObj.beginArea();
    } else {
        applyStroke(gc.getStroke());
    }

    AffineTransform trans = gc.getTransform();
    PathIterator iter = shape.getPathIterator(trans);
    if (shape instanceof Line2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.currentSegment(dstPts);
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        graphicsObj.addLine(coords);
    } else if (shape instanceof Rectangle2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.next();
        iter.currentSegment(dstPts);
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        graphicsObj.addBox(coords);
    } else if (shape instanceof Ellipse2D) {
        double[] dstPts = new double[6];
        Ellipse2D elip = (Ellipse2D) shape;
        double scale = trans.getScaleX();
        double radiusWidth = elip.getWidth() / 2;
        double radiusHeight = elip.getHeight() / 2;
        graphicsObj.setArcParams((int) Math.round(radiusWidth * scale), (int) Math.round(radiusHeight * scale),
                0, 0);
        double[] srcPts = new double[] { elip.getCenterX(), elip.getCenterY() };
        trans.transform(srcPts, 0, dstPts, 0, 1);
        final int mh = 1;
        final int mhr = 0;
        graphicsObj.addFullArc((int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]), mh, mhr);
    } else {
        processPathIterator(iter);
    }

    if (fill) {
        graphicsObj.endArea();
    }
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Processes a path iterator generating the necessary painting operations.
 *
 * @param iter PathIterator to process/*from   w w w  . ja  v a2 s . co m*/
 */
private void processPathIterator(PathIterator iter) {
    double[] dstPts = new double[6];
    double[] currentPosition = new double[2];
    for (int[] openingCoords = new int[2]; !iter.isDone(); iter.next()) {
        switch (iter.currentSegment(dstPts)) {
        case PathIterator.SEG_LINETO:
            graphicsObj.addLine(new int[] { (int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]) }, true);
            currentPosition = new double[] { dstPts[X], dstPts[Y] };
            break;
        case PathIterator.SEG_QUADTO:
            graphicsObj.addFillet(new int[] { (int) Math.round(dstPts[X1]), (int) Math.round(dstPts[Y1]),
                    (int) Math.round(dstPts[X2]), (int) Math.round(dstPts[Y2]) }, true);
            currentPosition = new double[] { dstPts[X2], dstPts[Y2] };
            break;
        case PathIterator.SEG_CUBICTO:
            double[] cubicCoords = new double[] { currentPosition[0], currentPosition[1], dstPts[X1],
                    dstPts[Y1], dstPts[X2], dstPts[Y2], dstPts[X3], dstPts[Y3] };
            double[][] quadParts = CubicBezierApproximator.fixedMidPointApproximation(cubicCoords);
            if (quadParts.length >= 4) {
                for (int segIndex = 0; segIndex < quadParts.length; segIndex++) {
                    double[] quadPts = quadParts[segIndex];
                    if (quadPts != null && quadPts.length == 4) {
                        graphicsObj.addFillet(
                                new int[] { (int) Math.round(quadPts[X1]), (int) Math.round(quadPts[Y1]),
                                        (int) Math.round(quadPts[X2]), (int) Math.round(quadPts[Y2]) },
                                true);
                        currentPosition = new double[] { quadPts[X2], quadPts[Y2] };
                    }
                }
            }
            break;
        case PathIterator.SEG_MOVETO:
            openingCoords = new int[] { (int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]) };
            currentPosition = new double[] { dstPts[X], dstPts[Y] };
            graphicsObj.setCurrentPosition(openingCoords);
            break;
        case PathIterator.SEG_CLOSE:
            graphicsObj.addLine(openingCoords, true);
            currentPosition = new double[] { openingCoords[0], openingCoords[1] };
            break;
        default:
            LOG.debug("Unrecognised path iterator type");
            break;
        }
    }
}

From source file:org.apache.pdfbox.pdfviewer.font.CFFGlyph2D.java

private GeneralPath transformGlyph(GeneralPath glyph) {
    // we have to invert all y-coordinates due to the moved 0,0-reference
    PathIterator iter = glyph.getPathIterator(null);
    float[] currentSegment = new float[6];
    Path2D.Float path = new Path2D.Float(iter.getWindingRule());
    boolean glyphTransformed = false;
    while (!iter.isDone()) {
        glyphTransformed = true;/*from   w  w  w .j a v  a 2 s . c  o  m*/
        int type = iter.currentSegment(currentSegment);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            path.moveTo(currentSegment[0], -currentSegment[1]);
            break;
        case PathIterator.SEG_LINETO:
            path.lineTo(currentSegment[0], -currentSegment[1]);
            break;
        case PathIterator.SEG_QUADTO:
            path.quadTo(currentSegment[0], -currentSegment[1], currentSegment[2], -currentSegment[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            path.curveTo(currentSegment[0], -currentSegment[1], currentSegment[2], -currentSegment[3],
                    currentSegment[4], -currentSegment[5]);
            break;
        case PathIterator.SEG_CLOSE:
            path.closePath();
            break;
        }
        iter.next();
    }
    if (glyphTransformed) {
        return new GeneralPath(path);
    } else {
        return glyph;
    }
}