【问题标题】:Java PathIterator clockwise or counterclockwise?Java PathIterator 顺时针还是逆时针?
【发布时间】:2012-07-06 16:19:26
【问题描述】:

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

【问题讨论】:

  • 根据PathIterator API,Moves the iterator to the next segment of the path forwards along the primary direction of traversal as long as there are more points in that direction.

标签: java path-iterator


【解决方案1】:

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

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

    }
}

【讨论】:

  • 为了记录,我不知道为什么那些0.0 0.0 部分会出现。如果有人知道并且可以编辑并发表评论,我将不胜感激......这只是一个简单的例子,所以我没有投入太多,但是......
  • 嗯,你知道什么......一年半后,有人过来“等等,这不对......”并修复它!太棒了。
  • 只是一个额外的说明。我测试过,如果您从路径创建一个区域并从中获取 PathIterator,那么它将始终是 CCW。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多