Path2Dが交差するかどうかを調べる必要があります。今のところ、私は単純にパスから行の配列を抽出し、これらのいずれかが交差するかどうかを調べることによって行います。しかし、それはO(n^2)の複雑さを持ち、非常に遅いです。それを行うより速い方法がありますか?Path2Dが自己交差するかどうかを調べる
10
A
答えて
3
あなたはスイープラインのアルゴリズムを使用してより速く、これを行うことができます:http://en.wikipedia.org/wiki/Sweep_line_algorithm
擬似コード:
Each line has a start point and an end point. Say that `start_x` <= `end_x` for all the lines.
Create an empty bucket of lines.
Sort all the points by their x coordinates, and then iterate through the sorted list.
If the current point is a start point, test its line against all the lines in the bucket, and then add its line to the
bucket.
if the current point is an end point, remove its line from the bucket.
最悪の場合は、まだO(N^2)
ですが、平均的なケースは、ここO(NlogN)
+0
ありがとう!しかし、あなたのメソッドの改良点 - バケット内の "above-below"の順序(行の最初の点のy座標でソート)を維持するならば、新しい行はその行の上と下の行に対してのみテストすることができ、 log n)O(n)の代わりに時間の複雑さ。それを見つけた:
3
では私ですこのアルゴリズムのJava実装:
import java.awt.Point;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.util.*;
/**
* Path2D helper functions.
* <p/>
* @author Gili Tzabari
*/
public class Path2Ds
{
/**
* Indicates if a Path2D intersects itself.
* <p/>
* @return true if a Path2D intersects itself
*/
public static boolean isSelfIntersecting(PathIterator path)
{
SortedSet<Line2D> lines = getLines(path);
if (lines.size() <= 1)
return false;
Set<Line2D> candidates = new HashSet<Line2D>();
for (Line2D line: lines)
{
if (Double.compare(line.getP1().distance(line.getP2()), 0) <= 0)
{
// Lines of length 0 do not cause self-intersection
continue;
}
for (Iterator<Line2D> i = candidates.iterator(); i.hasNext();)
{
Line2D candidate = i.next();
// Logic borrowed from Line2D.intersectsLine()
int lineRelativeToCandidate1 = Line2D.relativeCCW(line.getX1(), line.getY1(), line.
getX2(),
line.getY2(), candidate.getX1(), candidate.getY1());
int lineRelativeToCandidate2 = Line2D.relativeCCW(line.getX1(), line.getY1(), line.
getX2(),
line.getY2(), candidate.getX2(), candidate.getY2());
int candidateRelativeToLine1 = Line2D.relativeCCW(candidate.getX1(),
candidate.getY1(),
candidate.getX2(), candidate.getY2(), line.getX1(), line.getY1());
int candidateRelativeToLine2 = Line2D.relativeCCW(candidate.getX1(),
candidate.getY1(),
candidate.getX2(), candidate.getY2(), line.getX2(), line.getY2());
boolean intersection = (lineRelativeToCandidate1 * lineRelativeToCandidate2 <= 0)
&& (candidateRelativeToLine1 * candidateRelativeToLine2 <= 0);
if (intersection)
{
// Lines may share a point, so long as they extend in different directions
if (lineRelativeToCandidate1 == 0 && lineRelativeToCandidate2 != 0)
{
// candidate.P1 shares a point with line
if (candidateRelativeToLine1 == 0 && candidateRelativeToLine2 != 0)
{
// line.P1 == candidate.P1
continue;
}
if (candidateRelativeToLine1 != 0 && candidateRelativeToLine2 == 0)
{
// line.P2 == candidate.P1
continue;
}
// else candidate.P1 intersects line
}
else if (lineRelativeToCandidate1 != 0 && lineRelativeToCandidate2 == 0)
{
// candidate.P2 shares a point with line
if (candidateRelativeToLine1 == 0 && candidateRelativeToLine2 != 0)
{
// line.P1 == candidate.P2
continue;
}
if (candidateRelativeToLine1 != 0 && candidateRelativeToLine2 == 0)
{
// line.P2 == candidate.P2
continue;
}
// else candidate.P2 intersects line
}
else
{
// line and candidate overlap
}
return true;
}
if (candidate.getX2() < line.getX1())
i.remove();
}
candidates.add(line);
}
return false;
}
/**
* Returns all lines in a path. The lines are constructed such that the starting point is found
* on the left (or same x-coordinate) of the ending point.
* <p/>
* @param path the path
* @return the lines, sorted in ascending order of the x-coordinate of the starting point and
* ending point, respectively
*/
private static SortedSet<Line2D> getLines(PathIterator path)
{
double[] coords = new double[6];
SortedSet<Line2D> result = new TreeSet<Line2D>(new Comparator<Line2D>()
{
@Override
public int compare(Line2D o1, Line2D o2)
{
int result = Double.compare(o1.getX1(), o2.getX1());
if (result == 0)
{
// Ensure we are consistent with equals()
return Double.compare(o1.getX2(), o2.getX2());
}
return result;
}
});
if (path.isDone())
return result;
int type = path.currentSegment(coords);
assert (type == PathIterator.SEG_MOVETO): type;
Point.Double startPoint = new Point.Double(coords[0], coords[1]);
Point.Double openPoint = startPoint;
path.next();
while (!path.isDone())
{
type = path.currentSegment(coords);
assert (type != PathIterator.SEG_CUBICTO && type != PathIterator.SEG_QUADTO): type;
switch (type)
{
case PathIterator.SEG_MOVETO:
{
openPoint = startPoint;
break;
}
case PathIterator.SEG_CLOSE:
{
coords[0] = openPoint.x;
coords[1] = openPoint.y;
break;
}
}
Point.Double endPoint = new Point.Double(coords[0], coords[1]);
if (Double.compare(startPoint.getX(), endPoint.getX()) < 0)
result.add(new Line2D.Double(startPoint, endPoint));
else
result.add(new Line2D.Double(endPoint, startPoint));
path.next();
startPoint = endPoint;
}
return result;
}
}
関連する問題
- 1. Path2Dが他のPath2Dと交差するかどうかを調べる方法
- 2. 自己交差ポリゴンを非自己交差ポリゴンに分割する
- 3. 2つのPolyLinesが交差するかどうかを調べる方法
- 4. 変数が相互に交差するかどうかを調べる(MAクロスオーバー)
- 5. turf.js OpenLayers3からの自己交差ポリゴンの交差エラーDraw
- 6. ストリームの交差が空でないかどうかを調べる
- 7. C#ポリラインは自己交差です
- 8. ポリゴンに自己交差があるかどうかを検出する方法は?
- 9. MKPolylineが自己交差線を検出する目的C
- 10. 行が平行か、一致しているか、交差しているかを調べます。交差する場合、交差点を見つける
- 11. 点が回転した長方形と交差するかどうかを調べる?
- 12. JTSでラインが交差しているかどうかを調べる方法は?
- 13. IOSプロジェクトで2つのGoogleマップルートが交差しているかどうかを調べるには
- 14. 空間データベースで自己交差ポリゴンをクリーンアップするにはどうすればよいですか?
- 15. 自己交差ポリゴンを分割すると、多角形が返されます。
- 16. boost :: geometry :: union_は自己交差を生成します
- 17. スカラセット交差が空であるかどうかの確認
- 18. 自己交差を使ってポリゴンを修復する方法は?
- 19. ポリゴンの自己交差のテストの数値精度
- 20. Javaで自己交差するポリゴンの面積を求めたい
- 21. 線分が四角形を交差するかどうかを検出する
- 22. 2線分が交差するかどうかを確認する方法は?
- 23. 円が無限線と交差するかどうかを決定する
- 24. 行がC#で交差するかどうかを検出する方法は?
- 25. 点がポリゴンと交差するかどうかをチェックする方法
- 26. ジオハッシュが別のジオハッシュと交差するかどうかを確認する
- 27. JSTSライブラリを使用してリーフレットポリゴンで自己交差を見つける
- 28. 矩形が交差しているかどうかをチェックする方法は?
- 29. Pytablesが列が存在するかどうかを調べる
- 30. フォルダが存在するかどうかをPythonが調べる
PHPと同等の質問:http://stackoverflow.com/questions/2411636/is-there-an-easy-way-to-detect-line-segment-intersections – finnw