2017-04-12 94 views
0

我有結構化數據的陣列保持的線座標(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索引將在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數組?

回答

0

此問題已解決。我只是將Vector2d格式更改爲Vector2,它使用Single類型而不是Double,並且該算法起作用。顯然VB無法處理太多的Double,這可能是由於內存問題導致的64位精度。也許有人能更好地解釋這一點。