排序和交换的结构数组

时间:2017-04-12 15:04:05

标签: arrays algorithm sorting search data-structures

我有一个结构化数据数组,其中包含行坐标(P1和P2),它们是包含X和Y As Double的Vector2d(OpenTK)类型

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索引中的点将在P1P2之间进行交换,以修复已转换的细分。这个算法有效,但经过几次迭代后,即使我手动浏览剩下的段,搜索算法也找不到下一个段,我能够找到下一个段。这是输出:

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数组的一个坏方法?

1 个答案:

答案 0 :(得分:0)

这个问题已经解决了。我只是将Vector2d格式更改为Vector2,使用Single - 类型而不是Double,并且算法有效。显然VB可能无法处理太多可能由于内存问题导致64位精度的Double。也许有人可以更好地解释这一点。