【问题标题】:Nested For loops: Remove identical polygons from list of polygons嵌套 For 循环:从多边形列表中删除相同的多边形
【发布时间】:2014-09-25 20:18:35
【问题描述】:

我有一个多边形列表和一个检查两个多边形是否相同的函数。我的问题是我只想添加此列表中找到的 n 个相同多边形中的一个。如果多边形是唯一的,那么它将被添加到唯一列表中。我怎样才能调整我的以下代码来做到这一点:

    Dim bIdentical As Boolean = False
    Dim bTwinAdded As Boolean = False
    For Each outerEle As clsElement In liAllPolygons
        'bTwinAdded = False
        bIdentical = False
        For Each innerEle As clsElement In liAllPolygons
            If outerEle.Equals(innerEle) Then Continue For
            If ClsMath.AreTwoPolygonsIdentical2(outerEle.Nodes, innerEle.Nodes) Then
                bIdentical = True
                Exit For
            End If
        Next

        If Not bIdentical Then liUniquePolygons.Add(outerEle)
        If bIdentical AndAlso Not bTwinAdded Then
            liUniquePolygons.Add(outerEle)
            bTwinAdded = True
        End If

我简直想不出我能做什么。在 10 个多边形的列表中,数字 3 ,4 相同,数字 9, 10 相同,那么列表 liUniquePolygons 应该只得到数字 3 和数字 9 以及列表中的其余部分。使用上面的代码,对除数字 4、9 和 10 之外的所有多边形进行编号。

编辑:

1) 这种方式会抛出 Index Out Of Range 异常,因为列表成员的数量减少了。

For outerCount As Integer = 0 To liAllPolygons.Count - 1
        'bTwinAdded = False
        bIdentical = False
        For innerCount As Integer = 0 To liAllPolygons.Count - 1
            If liAllPolygons(outerCount).Equals(liAllPolygons(innerCount)) Then Continue For
            If ClsMath.AreTwoPolygonsIdentical2(liAllPolygons(outerCount).Nodes, liAllPolygons(innerCount).Nodes) Then
                liAllPolygons.RemoveAt(innerCount)
            End If
        Next
    Next

2) 这会抛出 List Has Changed 的​​异常:

 For Each outerEle as clsElement in liAllPolygons
        'bTwinAdded = False
        bIdentical = False
        For Each innerEle as clsElement in liAllPolygons
            If outerEle .Equals(innerEle ) Then Continue For
            If ClsMath.AreTwoPolygonsIdentical2(outerEle .Nodes, innerEle.Nodes) Then
                liAllPolygons.Remove(innerEle)
            End If
        Next
    Next

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    下面将根据您的函数 AreTwoPolygonsIdentical2 进行的比较生成唯一对象列表。

    这似乎有点浪费,但它应该可以工作......

    For Each outerEle In liAllPolygons
    
            'Compare outerEle  to all other items and see if there are identical matches
            Dim isIdentical As Boolean = False 'set a flag to indicate a match was found
    
            For Each innerEle In liAllPolygons
    
                If outerEle.Equals(innerEle) Then Continue For 'ignore the innerEle that is literaly the same as the outerEle
    
                'if the innerEle and outerEle are the same
                If ClsMath.AreTwoPolygonsIdentical2(innerEle.Nodes, outerEle.Nodes) Then
                    'this item is not a unique item
                    isIdentical = True
                    Exit For
                End If
    
            Next
    
            If Not isIdentical Then
                'if the item is unique, add it to the unique items list
                liUniquePolygons.Add(outerEle)
            Else
                'the item has a twin, we need to look at our list of unique items and see if we've already added the match
                Dim isMatchAdded As Boolean = False
                For Each uniqueEle In liUniquePolygons
                    If ClsMath.AreTwoPolygonsIdentical2(uniqueEle.Nodes, outerEle.Nodes) Then
                        isMatchAdded = True
                        Exit For
                    End If
                Next
    
                'we have nomatching items in the list, add it in
                If Not isMatchAdded Then
                    liUniquePolygons.Add(outerEle)
                End If
    
            End If
    
        Next
    

    【讨论】:

      【解决方案2】:

      我明白了:

      For outerCount As Integer = 0 To liAllPolygons.Count - 1
      
              If outerCount > liAllPolygons.Count - 1 Then Exit For
      
              For innerCount As Integer = 0 To liAllPolygons.Count - 1
      
                  If innerCount > liAllPolygons.Count - 1 Then Exit For
      
                  If liAllPolygons(outerCount).Equals(liAllPolygons(innerCount)) Then Continue For
                  If ClsMath.AreTwoPolygonsIdentical2(liAllPolygons(outerCount).Nodes, liAllPolygons(innerCount).Nodes) Then
                      liAllPolygons.RemoveAt(innerCount)
                      'GoTo Reset
                  End If
              Next
          Next
      

      【讨论】:

        猜你喜欢
        • 2016-05-03
        • 2012-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-07
        • 1970-01-01
        • 2020-03-25
        • 1970-01-01
        相关资源
        最近更新 更多