2016-09-17 63 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