我有一個多邊形列表和一個函數,檢查兩個多邊形是否相同。我的問題是我想添加在這個列表中找到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