获取的GeneralPath有序顶点顶点、GeneralPath

由网友(嘻哈男孩)分享简介:我怎样才能获得一个GeneralPath对象的顶点?看起来这应该是可能的,因为路径是从点(了lineTo,curveTo等)。构建How can I obtain the vertices of a GeneralPath object? It seems like this should be possible,...

我怎样才能获得一个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");
}

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

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

阅读全文

相关推荐

最新文章