2010-05-11 83 views
5

我知道有很多算法來驗證兩條線段是否相交。兩條平行線段相交

我在說的線段是由2個端點構成的長度線。

但是一旦遇到並行條件,他們只會告訴用戶一個很大的「否」,並假裝沒有重疊,共享端點或端點合謀。

我知道我可以計算兩條線段之間的距離。
如果距離爲0,請檢查位於其他線段中的端點。
這意味着我必須使用很多if else和& & ||條件。

這並不困難,但我的問題是

「有沒有竅門(或數學)方法來計算這個特殊的平行呢?」

我希望這幅畫澄清我的問題
http://judark.myweb.hinet.net/parallel.JPG

+0

你究竟在尋找什麼?根據定義,平行線不會相交。 – Tyler 2010-05-11 02:53:41

+0

平行線從不相交,除非距離爲0. 但由於它們的距離爲0,所以它們重疊。 但是,我的問題是關於線段。 拉伸線重疊,但線段保持未知。 所以我試着問一個解決方案來揭示未知。 – Judarkness 2010-05-11 03:08:04

+0

Judarkness您可能需要稍微改進您的問題。如果我們正在檢查並行性,它們是否是線段又有什麼關係? – WhirlWind 2010-05-11 03:23:05

回答

4

是,給出的公式爲兩行的,測試是否它們的斜率是相等的。如果是這樣,這些線是平行的並且不會相交。

如果您在每條線上都有積分,則可以使用slope formula

如果二者都垂直於x軸,它們都將具有無限的斜率,但它們將是平行的。每條線上的所有點都具有相同的x座標。

要處理線段,請計算交點,然後確定是否存在兩個線段的交點。

+0

哦,我的。我想我最好填寫角色要求。我誤解了。 = P – 2010-05-11 02:53:39

+0

哈哈...... *洗牌腳* =) – 2010-05-11 02:56:08

+0

@Chris啊,好吧,我也是這樣做的) – WhirlWind 2010-05-11 02:56:33

0

我假設你感興趣的情況是兩條線段平行的位置(通過檢查斜率來確定,就像Whirlwind所說的那樣),並且你試圖確定兩條線段是否重疊。

而不是擔心的線之間的距離,我認爲這樣做會以最簡單的方式,如果一個節段的兩個端點位於其他內:

if (segment_contains_point(segment_A, segment_B.start) || 
    segment_contains_point(segment_A, segment_B.end)) { 
     // lines overlap 
} 
+1

這就是我想要的,我有我自己的segment_contains_points()(用其他名字) 但是它太低效了。 而且我必須稱它三次,因爲B段可能更大並且包含整個段A. 而且我想知道是否有更好的解決方案? – Judarkness 2010-05-11 03:21:06

0

讓我們假設你有兩條線由公式a.x + b.y + c = 0d.x + e.y + f = 0描述。當a = 0 and d = 0b/a = e/d時,這兩條線是平行的。也許代替做這個部門只是確保b.d = a.e

0

,我發現這個(修改一點點由我來適應) 它會返回intercetion X,Y否則,如果沒有發現intercetion它將返回-1,-1

Public Function intercetion(ByVal ax As Integer, ByVal ay As Integer, ByVal bx As Integer, ByVal by As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal dx As Integer, ByVal dy As Integer) As Point 
    '// Determines the intersection point of the line segment defined by points A and B 
    '// with the line segment defined by points C and D. 
    '// 
    '// Returns YES if the intersection point was found, and stores that point in X,Y. 
    '// Returns NO if there is no determinable intersection point, in which case X,Y will 
    '// be unmodified. 

    Dim distAB, theCos, theSin, newX, ABpos As Double 

    '// Fail if either line segment is zero-length. 
    If ax = bx And ay = by Or cx = dx And cy = dy Then Return New Point(-1, -1) 

    '// Fail if the segments share an end-point. 
    If ax = cx And ay = cy Or bx = cx And by = cy Or ax = dx And ay = dy Or bx = dx And by = dy Then Return New Point(-1, -1) 

    '// (1) Translate the system so that point A is on the origin. 
    bx -= ax 
    by -= ay 
    cx -= ax 
    cy -= ay 
    dx -= ax 
    dy -= ay 

    '// Discover the length of segment A-B. 
    distAB = Math.Sqrt(bx * bx + by * by) 

    '// (2) Rotate the system so that point B is on the positive X axis. 
    theCos = bx/distAB 
    theSin = by/distAB 
    newX = cx * theCos + cy * theSin 
    cy = cy * theCos - cx * theSin 
    cx = newX 
    newX = dx * theCos + dy * theSin 
    dy = dy * theCos - dx * theSin 
    dx = newX 

    '// Fail if segment C-D doesn't cross line A-B. 
    If cy < 0 And dy < 0 Or cy >= 0 And dy >= 0 Then Return New Point(-1, -1) 

    '// (3) Discover the position of the intersection point along line A-B. 
    ABpos = dx + (cx - dx) * dy/(dy - cy) 

    '// Fail if segment C-D crosses line A-B outside of segment A-B. 
    If ABpos < 0 Or ABpos > distAB Then Return New Point(-1, -1) 

    '// (4) Apply the discovered position to line A-B in the original coordinate system. 
    '*X=Ax+ABpos*theCos 
    '*Y=Ay+ABpos*theSin 

    '// Success. 
    Return New Point(ax + ABpos * theCos, ay + ABpos * theSin) 
End Function 

Origin

+0

這並不能解決確定共線線段是否重疊的問題 - 事實上,在這段代碼的註釋中,它特別指出共線(和平行)線會失敗://如果段CD不交叉則失敗AB – 2013-08-03 10:06:08

0

我剛剛遇到同樣的問題:我想出了最簡單的方法來檢查線條是否重疊: 假設線段是共線的(平行並與x軸具有相同的交點)。 從較長的段(A,B)開始取一個A點作爲起點。現在找到距離A點最小距離的其他三個點(平方距離更好,甚至曼哈頓長度也可以),測量B方向的距離。如果與A的最近點是B,則線不相交。如果它屬於他們所做的其他部分。 也許你必須檢查零長度線或相同線條等特殊情況,但這應該很容易。