获取的GeneralPath有序顶点 [英] Obtain ordered vertices of GeneralPath

查看:186
本文介绍了获取的GeneralPath有序顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得一个GeneralPath对象的顶点?看起来这应该是可能的,因为路径是从点(了lineTo,curveTo等)。构建

How can I obtain the vertices of a GeneralPath object? It seems like this should be possible, since the path is constructed from points (lineTo, curveTo, etc).

我想创建一个双[] []点数据(一个x / y坐标数组)。

I'm trying to create a double[][] of point data (an array of x/y coordinates).

推荐答案

您可以得到点从的PathIterator

You can get the points back from the PathIterator.

我不知道你的约束,但如果你的外形始终只有一个封闭的子路径,并具有唯一的直边(没有曲线),那么下面的工作:

I'm not sure what your constraints are, but if your shape always has just one closed subpath and has only straight edges (no curves) then the following will work:

static double[][] getPoints(Path2D path) {
    List<double[]> pointList = new ArrayList<double[]>();
    double[] coords = new double[6];
    int numSubPaths = 0;
    for (PathIterator pi = path.getPathIterator(null);
         ! pi.isDone();
         pi.next()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            pointList.add(Arrays.copyOf(coords, 2));
            ++ numSubPaths;
            break;
        case PathIterator.SEG_LINETO:
            pointList.add(Arrays.copyOf(coords, 2));
            break;
        case PathIterator.SEG_CLOSE:
            if (numSubPaths > 1) {
                throw new IllegalArgumentException("Path contains multiple subpaths");
            }
            return pointList.toArray(new double[pointList.size()][]);
        default:
            throw new IllegalArgumentException("Path contains curves");
        }
    }
    throw new IllegalArgumentException("Unclosed path");
}

如果您的路径可能包含的曲线,你可以使用<一个href="http://download.oracle.com/javase/6/docs/api/java/awt/geom/Path2D.html#getPathIterator%28java.awt.geom.AffineTransform,%20double%29"相对=nofollow> 的getPathIterator()的扁平化版本

If your path may contain curves, you can use the flattening version of getPathIterator().

这篇关于获取的GeneralPath有序顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆