2012-07-06 103 views
2

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

+0

产生按照'PathIterator' API, '迭代器移动到沿着路径转发的下一段只要在那个方向上有更多的点,主要的遍历方向。' – 2012-07-06 16:37:17

回答

4

它按点在多边形中定义的顺序“走”。考虑下面的代码示例我刚刮起:

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.0'部分出现。如果有人知道并且可以编辑并发表评论,我会很感激...这仅仅是一个简单的例子,所以我没有太多投入,但是是的... – corsiKa 2012-07-06 16:59:04

+0

那么,你知道什么?一年半后,有人过来,“等等,这是不正确的......”并修复它!真棒。 – corsiKa 2014-01-31 03:44:34

+0

只是一个额外的说明。我测试过,看起来如果你从一个路径创建一个Area并从中获得PathIterator,那么它总是CCW。 – clankill3r 2015-02-12 10:07:37

相关问题