我有一个多边形列表和一个函数,检查两个多边形是否相同。我的问题是我想添加在这个列表中找到n个相同的多边形中只有一个。如果多边形是唯一的,那么它会被添加到唯一列表中。我怎样才能调整我的下面的代码来做到这一点:嵌套for循环:从多边形列表中删除相同的多边形
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)通过这种方式投指数超出范围的异常,因为列表成员的数量有所减少。
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)此抛出名单已经改变了异常:
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