Example usage for java.awt.geom PathIterator currentSegment

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

Introduction

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

Prototype

public int currentSegment(double[] coords);

Source Link

Document

Returns the coordinates and type of the current path segment in the iteration.

Usage

From source file:org.geotools.gce.imagemosaic.Utils.java

/**
 * Checks if the Shape equates to a Rectangle, if it does it performs a conversion, otherwise
 * returns null//from   w  ww  . jav  a  2s  . co  m
 * @param shape
 * @return
 */
static Rectangle toRectangle(Shape shape) {
    if (shape instanceof Rectangle) {
        return (Rectangle) shape;
    }

    if (shape == null) {
        return null;
    }

    // check if it's equivalent to a rectangle
    PathIterator iter = shape.getPathIterator(new AffineTransform());
    double[] coords = new double[2];

    // not enough points?
    if (iter.isDone()) {
        return null;
    }

    // get the first and init the data structures
    iter.next();
    int action = iter.currentSegment(coords);
    if (action != PathIterator.SEG_MOVETO && action != PathIterator.SEG_LINETO) {
        return null;
    }
    double minx = coords[0];
    double miny = coords[1];
    double maxx = minx;
    double maxy = miny;
    double prevx = minx;
    double prevy = miny;
    int i = 0;

    // at most 4 steps, if more it's not a strict rectangle
    for (; i < 4 && !iter.isDone(); i++) {
        iter.next();
        action = iter.currentSegment(coords);

        if (action == PathIterator.SEG_CLOSE) {
            break;
        }
        if (action != PathIterator.SEG_LINETO) {
            return null;
        }

        // check orthogonal step (x does not change and y does, or vice versa)
        double x = coords[0];
        double y = coords[1];
        if (!(prevx == x && prevy != y) && !(prevx != x && prevy == y)) {
            return null;
        }

        // update mins and maxes
        if (x < minx) {
            minx = x;
        } else if (x > maxx) {
            maxx = x;
        }
        if (y < miny) {
            miny = y;
        } else if (y > maxy) {
            maxy = y;
        }

        // keep track of prev step
        prevx = x;
        prevy = y;
    }

    // if more than 4 other points it's not a standard rectangle
    iter.next();
    if (!iter.isDone() || i != 3) {
        return null;
    }

    // turn it into a rectangle
    return new Rectangle2D.Double(minx, miny, maxx - minx, maxy - miny).getBounds();
}

From source file:org.opensha.commons.geo.RegionTest.java

private static void readArea(Area area) {
    PathIterator pi = area.getPathIterator(null);
    double[] vertex = new double[6];
    while (!pi.isDone()) {
        pi.currentSegment(vertex);
        System.out.println("AreaCoord: " + vertex[1] + " " + vertex[0]);
        pi.next();/*from   w  w w.  jav  a2s  .  com*/
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

private void followPath(Shape s, final int drawType) {
    if (s == null) {
        return;/*from   www.jav a2 s.  c om*/
    }
    if (drawType == PdfGraphics2D.STROKE) {
        if (!(stroke instanceof BasicStroke)) {
            s = stroke.createStrokedShape(s);
            followPath(s, PdfGraphics2D.FILL);
            return;
        }
    }
    if (drawType == PdfGraphics2D.STROKE) {
        setStrokeDiff(stroke, oldStroke);
        oldStroke = stroke;
        setStrokePaint();
    } else if (drawType == PdfGraphics2D.FILL) {
        setFillPaint();
    }
    final PathIterator points;
    if (drawType == PdfGraphics2D.CLIP) {
        points = s.getPathIterator(PdfGraphics2D.IDENTITY);
    } else {
        points = s.getPathIterator(transform);
    }
    final float[] coords = new float[6];
    int traces = 0;
    while (!points.isDone()) {
        ++traces;
        final int segtype = points.currentSegment(coords);
        normalizeY(coords);
        switch (segtype) {
        case PathIterator.SEG_CLOSE:
            cb.closePath();
            break;

        case PathIterator.SEG_CUBICTO:
            cb.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;

        case PathIterator.SEG_LINETO:
            cb.lineTo(coords[0], coords[1]);
            break;

        case PathIterator.SEG_MOVETO:
            cb.moveTo(coords[0], coords[1]);
            break;

        case PathIterator.SEG_QUADTO:
            cb.curveTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        default:
            throw new IllegalStateException("Invalid segment type in path");
        }
        points.next();
    }
    switch (drawType) {
    case PdfGraphics2D.FILL:
        if (traces > 0) {
            if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
                cb.eoFill();
            } else {
                cb.fill();
            }
        }
        break;
    case PdfGraphics2D.STROKE:
        if (traces > 0) {
            cb.stroke();
        }
        break;
    default: // drawType==CLIP
        if (traces == 0) {
            cb.rectangle(0, 0, 0, 0);
        }
        if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
            cb.eoClip();
        } else {
            cb.clip();
        }
        cb.newPath();
    }
}