我有一个具有2个属性MyLinesStartPoint的类EndPoint

我也有一个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.EndPoint不匹配的MyLines应该是新列表的第一个

第三操作:
然后,我想对其余的MyLines进行排序,以便每个MyLines.EndPointMyLines与下一个MyLines.StartPointMyLines匹配。

由于数据输入顺序不正确(我创建了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

最佳答案

保留仍要排序的线段的列表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

关于c# - 按两个值对我的类(class)排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39546622/

10-11 07:14