2017-06-21 68 views
2

我找到了通過determinants的線陣列的交點。但是,要查找所有交叉點,我正在檢查每一行,並創建O(n^2)檢查。高效地找到線陣列的交點

是否有更有效的方法來檢查所有的交叉點?當我試圖理清數百或數千行之間的交叉點時,我害怕運行時間。

+0

請看[用C#實現Hoey Shamos算法](https://stackoverflow.com/q/18512815/2521214) – Spektre

回答

3

請指定 - 你的意思是無限的行嗎?

對於線段,有高效的Bentley-Ottmann算法。

對於N無限線大約有N^2個交叉點(如果他們大多是不平行的),所以你的做法是在複雜感(也許,微優化是可能的)最佳

編輯:明確的任務描述Bentley-Ottmann看起來像開銷一樣。

Find intersections of lines with clipping window 
(for example, using Liang-Barsky algorithm) 
Consider only lines that intersect window 

Scan top window border from left corner to the right 
Insert every line end into the binary search tree 
(and add link to this tree node from the paired end to the map) 


Scan right window border from top corner to the bottom right one 
Check whether current line already has another end in the tree/map 
If yes 
    all lines with ends between positions of the first and 
    the second ends do intersect with it 
    Calculate intersections and remove both line ends from tree and map 
else 
    Insert line end into the list 

Continue for bottom and left edge. 

複雜O(N)進行初步治療和O(K + MlogM)對於M線交叉窗口矩形和K交叉點(式中,k可以是大約N^2) enter image description here

示例:樹狀態圍繞周邊

行走
E    //and map F to E node 
EG    //and map H to G 
EGI  
EGIK 
EGIK + H 
     H has pair point G, so GH intersect IJ and KL 
     remove G 
EIK 
EIK + F 
     F has pair point E, so EH intersect IJ and KL 
     remove E 
IK 
IK + J 
     J has pair point I, so IJ intersect KL 
     remove I 
K 
K+L 
    remove K 
end (5 intersections detected) 
+0

無限長爲此目的這個問題實際上受限於它所處的圖像的大小。我可能會將所有的線條延伸至比圖像更大的位置,並使用Bently-Ottmann,我會怎麼做,我不確定。 –

+0

您可以使用任何線條裁剪算法 - 例如,Liang-Barsky one,然後使用窗口邊界上的點作爲線段結束。 – MBo

+0

添加了建議的數據結構 – MBo