【问题标题】:Array of structures sorting and swapping结构数组排序和交换
【发布时间】:2017-04-12 15:04:04
【问题描述】:

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

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 时的语句 这相当令人困惑。我的编码有错误吗?或者这只是处理结构数组的一种不好的方法?

【问题讨论】:

    标签: arrays algorithm sorting search data-structures


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 2016-02-15
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2020-03-30
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多