Search in sources :

Example 1 with FlatteningPathIterator

use of java.awt.geom.FlatteningPathIterator in project android by JetBrains.

the class ConstraintHandle method lengthOfPath.

/**
     * Return the length of the given path
     *
     * @param path
     * @return the length of the path
     */
private static int lengthOfPath(Path2D.Float path) {
    FlatteningPathIterator f = new FlatteningPathIterator(path.getPathIterator(null), 1);
    double sum = 0;
    float x1, x2, y1, y2;
    float[] coords = new float[6];
    f.currentSegment(coords);
    x1 = coords[0];
    y1 = coords[1];
    f.next();
    do {
        f.currentSegment(coords);
        f.next();
        x2 = coords[0];
        y2 = coords[1];
        sum += Math.hypot(x2 - x1, y2 - y1);
        x1 = x2;
        y1 = y2;
    } while (!f.isDone());
    return (int) sum;
}
Also used : FlatteningPathIterator(java.awt.geom.FlatteningPathIterator)

Example 2 with FlatteningPathIterator

use of java.awt.geom.FlatteningPathIterator in project antlrworks by antlr.

the class SLinkArc method contains.

public boolean contains(PathIterator iterator, double x, double y) {
    double[] coord = new double[6];
    double oldx = -1, oldy = -1;
    final double flateness = 0.8;
    final double inset = 4;
    FlatteningPathIterator i = new FlatteningPathIterator(iterator, flateness);
    while (!i.isDone()) {
        switch(i.currentSegment(coord)) {
            case FlatteningPathIterator.SEG_MOVETO:
                oldx = coord[0];
                oldy = coord[1];
                break;
            case FlatteningPathIterator.SEG_LINETO:
                double nx = coord[0];
                double ny = coord[1];
                double rx1 = Math.min(oldx, nx);
                double ry1 = Math.min(oldy, ny);
                double rx2 = Math.max(oldx, nx);
                double ry2 = Math.max(oldy, ny);
                if (Math.abs(rx1 - rx2) < inset || Math.abs(ry1 - ry2) < inset) {
                    rx1 -= inset;
                    ry1 -= inset;
                    rx2 += inset;
                    ry2 += inset;
                }
                if (x >= rx1 && x <= rx2 && y >= ry1 && y <= ry2)
                    return true;
                oldx = nx;
                oldy = ny;
                break;
        }
        i.next();
    }
    return false;
}
Also used : FlatteningPathIterator(java.awt.geom.FlatteningPathIterator)

Example 3 with FlatteningPathIterator

use of java.awt.geom.FlatteningPathIterator in project knime-core by knime.

the class CurvedPolylineConnection method calcCurve.

private void calcCurve() {
    // redraw the path and re-determine the curve approximation
    if (m_approxCurve != null) {
        m_approxCurve.removeAllPoints();
    } else {
        m_approxCurve = new PointList();
    }
    PointList points = getPoints();
    m_path = new Path(Display.getDefault());
    m_path.moveTo(points.getFirstPoint().x, points.getFirstPoint().y);
    m_approxCurve.addPoint(points.getFirstPoint().x, points.getFirstPoint().y);
    Point lastPoint = points.getFirstPoint();
    for (int i = 1; i < points.size(); i++) {
        int x = points.getPoint(i).x;
        int y = points.getPoint(i).y;
        double dist = Math.sqrt((x - lastPoint.x) * (x - lastPoint.x) + (y - lastPoint.y) * (y - lastPoint.y));
        // control pts
        int cp1x = lastPoint.x + (int) (RELATIVE_CONTROL_POINT_PLACEMENT * dist);
        int cp1y = lastPoint.y;
        int cp2x = x - (int) (RELATIVE_CONTROL_POINT_PLACEMENT * dist);
        int cp2y = y;
        m_path.cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
        CubicCurve2D cc = new CubicCurve2D.Float(lastPoint.x, lastPoint.y, cp1x, cp1y, cp2x, cp2y, x, y);
        FlatteningPathIterator fpi = new FlatteningPathIterator(cc.getPathIterator(null), 3, 5);
        float[] coords = new float[6];
        while (!fpi.isDone()) {
            int type = fpi.currentSegment(coords);
            switch(type) {
                case PathIterator.SEG_LINETO:
                    m_approxCurve.addPoint((int) coords[0], (int) coords[1]);
                    break;
            }
            fpi.next();
        }
        lastPoint = new Point(x, y);
    }
}
Also used : Path(org.eclipse.swt.graphics.Path) PointList(org.eclipse.draw2d.geometry.PointList) FlatteningPathIterator(java.awt.geom.FlatteningPathIterator) CubicCurve2D(java.awt.geom.CubicCurve2D) Point(org.eclipse.draw2d.geometry.Point) Point(org.eclipse.draw2d.geometry.Point)

Aggregations

FlatteningPathIterator (java.awt.geom.FlatteningPathIterator)3 CubicCurve2D (java.awt.geom.CubicCurve2D)1 Point (org.eclipse.draw2d.geometry.Point)1 PointList (org.eclipse.draw2d.geometry.PointList)1 Path (org.eclipse.swt.graphics.Path)1