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

查看:29
本文介绍了获取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).

我正在尝试创建点数据的 double[][](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");
}

如果您的路径可能包含曲线,您可以使用 getPathIterator() 的扁平化版本.

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

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

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