2012-07-06 2 views

답변

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 

가`PathIterator`의 API에 따르면

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

+0

글쎄요, 나중에 뭘 알고 .. 1 년 반 만에 누군가가 따라 와서 "기다림, 그건 옳지 않습니다 ..."하고 고쳐줍니다! 굉장해. – corsiKa

+0

그냥 여분의 메모. 테스트 한 결과 Path에서 Area를 생성하고 PathIterator를 얻으면 항상 CCW가됩니다. – clankill3r