如何获取GeneralPath对象的顶点?看起来这应该是可能的,因为路径是由点(lineTo,curveTo等)构成的.
我正在尝试创建一个double [] []点数据(x/y坐标数组).
你可以从中获得积分PathIterator.
我不确定你的约束是什么,但是如果你的形状总是只有一个封闭的子路径而且只有直边(没有曲线),那么下面的工作方法是:
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");
}
Run Code Online (Sandbox Code Playgroud)
如果您的路径可能包含曲线,则可以使用展平版本getPathIterator().