按两个值排序我的类

本文关键字:排序 我的 两个 | 更新日期: 2023-09-27 18:07:24

我有一个类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.EndPoint不匹配,则MyLines应位于新列表的第一个

第三操作:

然后,我想对剩余的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

按两个值排序我的类

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