获取GeneralPath的有序顶点

如何获取GeneralPath对象的顶点? 看起来这应该是可能的,因为路径是由点(lineTo,curveTo等)构成的。

我正在尝试创建一个double [] []点数据(x / y坐标数组)。

您可以从PathIterator获取积分。

我不确定你的约束是什么,但是如果你的形状总是只有一个封闭的子路径并且只有直边(没有曲线),那么下面的工作方法是:

 static double[][] getPoints(Path2D path) { List pointList = new ArrayList(); 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()的展平版本 。

我怀疑它是否总是可能的,如果有的话…… JavaDoc说:

“GeneralPath类表示由直线和二次和三次(Bézier)曲线构成的几何路径。”

因此,如果它是一条曲线,它所包含的点不一定是曲线的一部分。