gpt4 book ai didi

java - 在Java中获取Path2D对象的坐标对?

转载 作者:行者123 更新时间:2023-12-02 02:26:19 35 4
gpt4 key购买 nike

我必须获取 Path2D 对象中每组坐标的坐标,但我不知道如何获取。之前我们使用的是多边形,所以我能够初始化两个长度为 Polygon.npoints 的数组。然后将它们设置为 Polygon.xpointsPolygon.ypoints数组。现在我们正在使用 Path2D 对象,我不知道该怎么做,因为我似乎所能做的就是初始化一个 PathIterator,它接受一个数组作为输入并返回段?有人可以解释如何获取 Path2D 对象的所有坐标对吗?

最佳答案

下面是一个如何获取 a 的所有线段和坐标对的示例 PathIterator :

您调用 PathIteratorcurrentSegment方法反复进行。每次调用时,您都会获得一段的坐标。特别注意,坐标的数量取决于线段类型(从 currentSegment 方法获得的返回值)。

public static void dump(Shape shape) {
float[] coords = new float[6];
PathIterator pathIterator = shape.getPathIterator(new AffineTransform());
while (!pathIterator.isDone()) {
switch (pathIterator.currentSegment(coords)) {
case PathIterator.SEG_MOVETO:
System.out.printf("move to x1=%f, y1=%f\n",
coords[0], coords[1]);
break;
case PathIterator.SEG_LINETO:
System.out.printf("line to x1=%f, y1=%f\n",
coords[0], coords[1]);
break;
case PathIterator.SEG_QUADTO:
System.out.printf("quad to x1=%f, y1=%f, x2=%f, y2=%f\n",
coords[0], coords[1], coords[2], coords[3]);
break;
case PathIterator.SEG_CUBICTO:
System.out.printf("cubic to x1=%f, y1=%f, x2=%f, y2=%f, x3=%f, y3=%f\n",
coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
break;
case PathIterator.SEG_CLOSE:
System.out.printf("close\n");
break;
}
pathIterator.next();
}
}

您可以使用此方法转储任何 Shape(因此也适用于它的实现,如矩形多边形Ellipse2DPath2D,...)

Shape shape = ...;
dump(shape);

关于java - 在Java中获取Path2D对象的坐标对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47728519/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com