gpt4 book ai didi

Java PathIterator顺时针还是逆时针?

转载 作者:行者123 更新时间:2023-11-30 06:29:35 24 4
gpt4 key购买 nike

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-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 PathIterator顺时针还是逆时针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11366040/

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