Java PathIterator是顺时针还是逆时针?

时间:2012-07-06 16:19:26

标签: java path-iterator

Java PathIterator是以顺时针还是逆时针顺序为您提供多边形的点?

1 个答案:

答案 0 :(得分:5)

它按照多边形中定义点的顺序“行走”。考虑一下我刚刚掀起的以下代码示例:

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++;
        }

    }
}