2
자바에서는 PathIterator
을 사용하여 Area
을 제한하는 선분을 반복하는 방법은 무엇입니까? Area
은 선으로 만 묶이지 만 (곡선 지원은 아프지 않습니다).PathIterator를 사용하여 Area를 제한하는 모든 선분을 돌려 주시겠습니까?
메서드는 모든 선분의 모음을 반환해야합니다.
자바에서는 PathIterator
을 사용하여 Area
을 제한하는 선분을 반복하는 방법은 무엇입니까? Area
은 선으로 만 묶이지 만 (곡선 지원은 아프지 않습니다).PathIterator를 사용하여 Area를 제한하는 모든 선분을 돌려 주시겠습니까?
메서드는 모든 선분의 모음을 반환해야합니다.
이 (모든 상황에서, 내가 믿는) 작동하지만,보다 철저한 테스트가 필요할 수 있습니다 : 코드에 주석을 포함하는 SO의 소수 민족 중 하나 인에 대한
Area area; // The value is set elsewhere in the code
ArrayList<double[]> areaPoints = new ArrayList<double[]>();
ArrayList<Line2D.Double> areaSegments = new ArrayList<Line2D.Double>();
double[] coords = new double[6];
for (PathIterator pi = area.getPathIterator(null); !pi.isDone(); pi.next()) {
// The type will be SEG_LINETO, SEG_MOVETO, or SEG_CLOSE
// Because the Area is composed of straight lines
int type = pi.currentSegment(coords);
// We record a double array of {segment type, x coord, y coord}
double[] pathIteratorCoords = {type, coords[0], coords[1]};
areaPoints.add(pathIteratorCoords);
}
double[] start = new double[3]; // To record where each polygon starts
for (int i = 0; i < areaPoints.size(); i++) {
// If we're not on the last point, return a line from this point to the next
double[] currentElement = areaPoints.get(i);
// We need a default value in case we've reached the end of the ArrayList
double[] nextElement = {-1, -1, -1};
if (i < areaPoints.size() - 1) {
nextElement = areaPoints.get(i + 1);
}
// Make the lines
if (currentElement[0] == PathIterator.SEG_MOVETO) {
start = currentElement; // Record where the polygon started to close it later
}
if (nextElement[0] == PathIterator.SEG_LINETO) {
areaSegments.add(
new Line2D.Double(
currentElement[1], currentElement[2],
nextElement[1], nextElement[2]
)
);
} else if (nextElement[0] == PathIterator.SEG_CLOSE) {
areaSegments.add(
new Line2D.Double(
currentElement[1], currentElement[2],
start[1], start[2]
)
);
}
}
// areaSegments now contains all the line segments
감사합니다! – cb4
이것은 또한 경로의 개별 점을 찾는 데 (세그먼트를 순환하고 첫 번째 점을 잡거나 세그먼트 생성을 건너 뜀으로써 알고리즘을 단순화함으로써) 매우 유용합니다. –
http://docs.oracle.com/javase/7/docs/api/java/awt/geom/FlatteningPathIterator.html에서 경로 반복자를 래핑하여 커브를 선으로 변환 한 다음 위의 솔루션을 사용할 수 있습니다 – tofarr