我有结构化数据的阵列保持的线坐标(P1和P2),它们的Vector2D(OpenTK)类型包含X和Y为双阵列结构排序和交换
Public Structure sliced
Public P1, P2 As Vector2d
Public ID As Integer
End Structure
Dim slicedPoint(30000) As sliced
阵列将持有多个随机排列的线段。为了将每条线连接在一起,我使用一个简单的算法来找到每对线段以形成一个封闭轮廓。
For i = 0 To SPcount - 2
slicedPoint(i).ID = groupID
'Assign starting Point of the loop
If initialFlag = False Then 'This is done once
Pstart = slicedPoint(i).P1
initialFlag = True
End If
If slicedPoint(i).P2 <> Pstart Then
For k = i + 1 To SPcount - 1
If slicedPoint(i).P2 = slicedPoint(k).P1 Then
'Normal order points
bufferPoint = slicedPoint(i + 1)
slicedPoint(i + 1) = slicedPoint(k)
slicedPoint(k) = bufferPoint
Exit For
End If
If slicedPoint(i).P2 = slicedPoint(k).P2 Then
'Inverted points
bufferPoint.P1 = slicedPoint(k).P2
bufferPoint.P2 = slicedPoint(k).P1
slicedPoint(k) = bufferPoint
bufferPoint = slicedPoint(i + 1)
slicedPoint(i + 1) = slicedPoint(k)
slicedPoint(k) = bufferPoint
Exit For
End If
If k = SPcount - 1 Then
My.Application.Log.WriteEntry("Not Found")
End If
Next
Else
'Closed Contour found, Increment group count
groupID += 1 'next group starting point refers here
Pstart = slicedPoint(i + 1).P1
End If
Next
在上面的代码,i
指数是基准,并且k
是搜索索引。 SPCount
是可用分段的最大数量。该代码通过参考slicedPoint(i).P2
搜索下一对线段。如果slicedPoint(i).P2
等于slicedPoint(k).P1
,那么这是下一个分段。有时,下一个分段存储为反转分段,与slicedPoint(i).P2 = slicedPoint(k).P2
一样。点k
索引将在P1
到P2
之间交换以修复反转段。这个算法可以工作,但是经过几次迭代后,即使手动检查剩余的段,搜索算法也找不到下一个段,我可以找到下一个段。下面是输出:
DefaultSource Information: 0 : Intersecting Facet: 104
DefaultSource Information: 0 : Slicing Point: 104
DefaultSource Information: 0 : SliceZ: 1
DefaultSource Information: 0 : 0 0 (-57.1428571428571, -10) (-50, -10)
DefaultSource Information: 0 : 1 0 (-50, -10) (-49.7306428571429, -9.49302657142857)
DefaultSource Information: 0 : 2 0 (-49.7306428571429, -9.49302657142857) (-49.68575, -9.408531)
DefaultSource Information: 0 : 3 0 (-49.68575, -9.408531) (-49.3656414285714, -8.93196814285714)
DefaultSource Information: 0 : 4 0 (-49.3656414285714, -8.93196814285714) (-49.31229, -8.852541)
DefaultSource Information: 0 : 5 0 (-49.31229, -8.852541) (-48.94485, -8.41144757142857)
DefaultSource Information: 0 : 6 0 (-48.94485, -8.41144757142857) (-48.88361, -8.337932)
DefaultSource Information: 0 : 7 0 (-48.88361, -8.337932) (-48.47273, -7.93699142857143)
DefaultSource Information: 0 : 8 0 (-48.47273, -7.93699142857143) (-48.40425, -7.870168)
DefaultSource Information: 0 : 9 0 (-48.40425, -7.870168) (-47.9542928571429, -7.51363771428571)
DefaultSource Information: 0 : 10 0 (-47.9542928571429, -7.51363771428571) (-47.8793, -7.454216)
它工作得很好,直到:
DefaultSource Information: 0 : 22 0 (-44.2059442857143, -6.25492957142857) (-44.11039, -6.25)
DefaultSource Information: 0 : 23 0 (-44.11039, -6.25) (31.5074214285714, -6.25)
DefaultSource Information: 0 : Not Found
DefaultSource Information: 0 : 24 0 (-49.6323985714286, 9.32910385714286) (-49.31229, 8.852541)
,但24日实际对P2 (31.5074214285714, -6.25)
可以位于67
DefaultSource Information: 0 : 67 0 (44.11039, -6.25) (31.5074214285714, -6.25)
其满足报表时slicedPoint(i).P2 = slicedPoint(k).P2
这很混乱。我的编码有错误吗?或者这只是一个不好的方法来处理struct数组?