2016-09-17 51 views
2

我有一個班級MyLines有2個屬性,StartPointEndPoint我的班級由兩個值排序

我也有一個List(Of MyLines)

Dim ListOfLines As New List(Of MyLines) 

理論上所有MyLines將在一端匹配的「行系列」(如果是有道理的) 我想做這個名單上3個操作。

首先操作: 如果任何MyLines.EndPoint等於任何其他MyLines.Endpoint應該執行SwapEnds,以確保所有的數據都井然有序。由於數據應該是SP,EP,SP,EP,SP,EP ......

二操作: 而且,任何MyLines.Startpoint沒有匹配任何其他MyLines.EndPointMyLines應該是第一的新名單

第三操作: 後來我想剩下的MyLines排序,以便每個MyLinesMyLines.EndPoint匹配的下一個MyLinesMyLines.StartPoint

由於數據可以在我的不正確的順序輸入(已經創建了一個SwapEnd方法,但我不知道如何檢查這一點)

尋找思路。生病在VB.net或C# 在此先感謝。 :)

Public Class MyLines 
Implements IComparable(Of MyLines) 

Private m_StartPoint As Point3d 
Private m_EndPoint As Point3d 

Public Sub New(ByVal StartPoint As Point3d, ByVal EndPoint As Point3d) 
    m_StartPoint = StartPoint 
    m_EndPoint = EndPoint 
End Sub 

Public ReadOnly Property StartPoint() As Point3d 
    Get 
     Return m_StartPoint 
    End Get 
End Property 

Public ReadOnly Property EndPoint() As Point3d 
    Get 
     Return m_EndPoint 
    End Get 
End Property 

Public Sub SwapEnd() 

    Dim OldValue As Point3d = New Point3d(m_StartPoint) 
    m_StartPoint = New Point3d(m_EndPoint) 
    m_EndPoint = New Point3d(OldValue) 
    Debug.Print("Swapped") 
End Sub 

Public Function CompareTo(other As MyLines) As Integer Implements IComparable(Of MyLines).CompareTo 
    Return EndPoint.IsEqualTo(other.StartPoint, New Tol(0.0001, 0.0001)) 
End Function 
+0

當三條線在相同點相交時會發生什麼?通常情況下,這是通過具有連接點的線路的鄰居點列表來完成的。因爲一條直線是對稱的,所以交換並沒有什麼意義,並且您將獲得與您開始時完全相同的結果。 – jdweng

+0

線條只會在兩端以不同的線條交叉。把它看成是一個帶頂點的長線。 – user6628265

+0

路徑可以循環回自己還是總是向一個方向遞進? –

回答

0

請線段仍然有列表open需訂購併責令段的列表ordered。前者將是線段的初始列表,後者將開始爲空。然後從任何線段開始,並嘗試在兩個方向上找到其延續:

//Pseudo code 
Move first entry from open to ordered 
Dim foundNext As Boolean 
Do 'Forward direction 
    foundNext = False 
    For Each segment in open 
     If open.Last().EndPoint = segment.StartPoint 
      move segment from open to end of ordered 
      FoundNext = True 
      Continue While 
     Else If open.Last().EndPoint = segment.EndPoint 
      segment.Swap() 
      move segment from open to end of ordered 
      FoundNext = True 
      Continue While 
     End If 
    Next 
While foundNext 
Do 'Backward direction 
    foundNext = False 
    For Each segment in open 
     If open.First().StartPoint = segment.EndPoint 
      move segment from open to beginning of ordered 
      FoundNext = True 
      Continue While 
     Else If open.Last().StartPoint = segment.StartPoint 
      segment.Swap() 
      move segment from open to beginning of ordered 
      FoundNext = True 
      Continue While 
     End If 
    Next 
While foundNext