Java路径迭代器是顺时针还是逆时针?
Java PathIterator是否按顺时针或逆时针顺序为您提供多边形的点?它按多边形中定义的点的顺序“行走”。请考虑下面的代码示例:Java路径迭代器是顺时针还是逆时针?,java,path-iterator,Java,Path Iterator,Java PathIterator是否按顺时针或逆时针顺序为您提供多边形的点?它按多边形中定义的点的顺序“行走”。请考虑下面的代码示例: c:\files\j>java ShapeTest Walking clockwise Currently at: 2.0 5.0 Currently at: 4.0 4.0 Currently at: 4.0 1.0 Currently at: 0.0 1.0 Currently at: 0.0 3.0 Walking counter-clockwis
c:\files\j>java ShapeTest
Walking clockwise
Currently at: 2.0 5.0
Currently at: 4.0 4.0
Currently at: 4.0 1.0
Currently at: 0.0 1.0
Currently at: 0.0 3.0
Walking counter-clockwise
Currently at: 0.0 3.0
Currently at: 0.0 1.0
Currently at: 4.0 1.0
Currently at: 4.0 4.0
Currently at: 2.0 5.0
是由
import java.awt.Polygon;
import java.awt.geom.PathIterator;
class ShapeTest {
/**
* Use points to make a pentagon
. 2,5
0,3 . . 4,3
0,1 . . 4,1
*/
public static void main(String...args) {
// from the top clockwise
int[] xClock = new int[] { 2,4,4,0,0 };
int[] yClock = new int[] { 5,4,1,1,3 };
// the reverse order from the clockwise way
int[] xCounter = new int[] { 0,0,4,4,2 };
int[] yCounter = new int[] { 3,1,1,4,5 };
Polygon clock = new Polygon(xClock, yClock, 5);
Polygon counter = new Polygon(xCounter, yCounter, 5);
int index = 0;
System.out.println("Walking clockwise");
PathIterator clockIter = clock.getPathIterator(null);
while(!clockIter.isDone() && index < clock.npoints) {
float[] coords = new float[6];
clockIter.currentSegment(coords);
System.out.println("Currently at: " + coords[0] + " " + coords[1]);
clockIter.next();
index++;
}
index = 0;
System.out.println("Walking counter-clockwise");
PathIterator counterIter = counter.getPathIterator(null);
while(!counterIter.isDone() && index < counter.npoints) {
float[] coords = new float[6];
counterIter.currentSegment(coords);
System.out.println("Currently at: " + coords[0] + " " + coords[1]);
counterIter.next();
index++;
}
}
}
导入java.awt.Polygon;
导入java.awt.geom.PathIterator;
类形状测试{
/**
*使用点创建五角大楼
. 2,5
0,3 . . 4,3
0,1 . . 4,1
*/
公共静态void main(字符串…参数){
//从顶部顺时针
int[]xClock=newint[]{2,4,4,0,0};
int[]yClock=newint[]{5,4,1,1,3};
//与顺时针方向相反的顺序
int[]xCounter=新的int[]{0,0,4,4,2};
int[]yCounter=新的int[]{3,1,1,4,5};
多边形时钟=新多边形(xClock,yClock,5);
多边形计数器=新多边形(xCounter,yCounter,5);
int指数=0;
System.out.println(“顺时针行走”);
PathIterator clockIter=clock.getPathIterator(null);
而(!clockIter.isDone()&&index
根据PathIterator
API,将迭代器沿主遍历方向向前移动到路径的下一段,只要该方向上有更多点。
作为记录,我不知道为什么会出现这些0.0.0
部分。如果有人知道并且可以编辑并留下评论,我将不胜感激。。。这只是一个简单的例子,所以我没有投入太多,但是的…好吧,你知道什么。。。一年半后,有人来了,说“等等,那不对……”然后把它修好了!太棒了,多留一张便条。我测试过,如果你从一个路径创建一个区域并从中获取PathIterator,那么它将始终是CCW。