2
Javaでは、PathIterator
を使用して、Area
を制約する線分をどのように反復するのでしょうか? Area
は線のみで結ばれています(しかし、曲線のサポートは傷つきません)。PathIteratorを使用して領域を拘束するすべての線分を返しますか?
このメソッドは、すべての線分のコレクションを返す必要があります。
Javaでは、PathIterator
を使用して、Area
を制約する線分をどのように反復するのでしょうか? Area
は線のみで結ばれています(しかし、曲線のサポートは傷つきません)。PathIteratorを使用して領域を拘束するすべての線分を返しますか?
このメソッドは、すべての線分のコレクションを返す必要があります。
これは、(すべての状況で、私は信じて)動作しますが、それはより徹底的なテストが必要な場合があります:あなたのコード内のコメントを含めるように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