如何通过函数对多维数组进行排序

时间:2017-06-16 23:47:09

标签: vb.net

我在vb.net 2012中遇到了一些排序多维数组的麻烦,我试图制作一个自定义排序算法来通过z轴对坐标进行排序,这样就可以绘制出一个四边形多边形。这样可以像css中的z索引那样按顺序绘制形状

(还有如何向数组添加一个东西,以便您可以定义多边形颜色)

'there's nothing here in the x and y sections right now
Dim G_ObjList(,,) As Double = {
    {{0, 0, 1}, {0, 0, 0.75}, {0, 0, 0.75}, {0, 0, 0.95}},
    {{0, 0, 1}, {0, 0, 0.75}, {0, 0, 0.75}, {0, 0, 0.95}},
    {{0, 0, 1}, {0, 0, 0.75}, {0, 0, 0.75}, {0, 0, 0.95}},
    {{0, 0, 1}, {0, 0, 0.75}, {0, 0, 0.75}, {0, 0, 0.95}},
    {{0, 0, 0.75}, {0, 0, 0.75}, {0, 0, 0.75}, {0, 0, 0.75}}
}
Function p3DOrder(ByVal a(,,) As Double, ByVal p As Point)
    '0 is point
    '1 is depth
    '2 is distance to p after being translated to p3d (probably only to be used here, but just in case for anyother use)
    'a(depth/point, infopoint
    'create new jagged array (not jagged array anymore)
    Dim avDP(2, a.GetLength(0) - 1)
    'sorts data into new jagged array
    For i = 0 To (a.GetLength(0) - 1)
        'calculate averages and set array
        avDP(2, i) = (Math.Sqrt((a(i, 0, 0) - p.X) ^ 2 + (a(i, 0, 1) - p.Y) ^ 2) + Math.Sqrt((a(i, 1, 0) - p.X) ^ 2 + (a(i, 1, 1) - p.Y) ^ 2) + Math.Sqrt((a(i, 2, 0) - p.X) ^ 2 + (a(i, 2, 1) - p.Y) ^ 2) + Math.Sqrt((a(i, 3, 0) - p.X) ^ 2 + (a(i, 3, 1) - p.Y) ^ 2)) / 4 'calculate distance to main perspective point
        avDP(1, i) = (a(i, 0, 2) + a(i, 1, 2) + a(i, 2, 2) + a(i, 3, 2)) / 4 'set depth side by side with distance calculated
        avDP(0, i) = {New Point(a(i, 0, 0), a(i, 0, 1)), New Point(a(i, 1, 0), a(i, 1, 1)), New Point(a(i, 2, 0), a(i, 2, 1)), New Point(a(i, 3, 0), a(i, 3, 1))}
        'have to keep the varibles in this way to prevent separation between data pairs
        'sort jagged array
        If i <= a.GetLength(0) - 2 Then
            If (avDP(1, i) > avDP(1, i + 1)) Then
                'test switch program
                Dim _tm0() = {avDP(0, i), avDP(1, i), avDP(2, i)} 'temporary stores data to switch
                Dim _tm1() = {avDP(0, i + 1), avDP(1, i + 1), avDP(2, i + 1)}
                avDP(0, i) = _tm1(0) 'switch around array data
                avDP(1, i) = _tm1(1)
                avDP(2, i) = _tm1(2)
                avDP(0, i + 1) = _tm0(0)
                avDP(1, i + 1) = _tm0(1)
                avDP(2, i + 1) = _tm0(2)
                Dim _tmbStep As Integer = 0
                Do While (avDP(1, i - _tmbStep) > avDP(1, i + 1 - _tmbStep)) 'step back if true
                    Dim _tm2() = {avDP(0, i - _tmbStep), avDP(1, i - _tmbStep), avDP(2, i - _tmbStep)} 'temporary stores data to switch
                    Dim _tm3() = {avDP(0, i + 1 - _tmbStep), avDP(1, i + 1 - _tmbStep), avDP(2, i + 1 - _tmbStep)}
                    avDP(0, i - _tmbStep) = _tm3(0) 'switch around array data
                    avDP(1, i - _tmbStep) = _tm3(1)
                    avDP(2, i - _tmbStep) = _tm3(2)
                    avDP(0, i + 1 - _tmbStep) = _tm2(0)
                    avDP(1, i + 1 - _tmbStep) = _tm2(1)
                    avDP(2, i + 1 - _tmbStep) = _tm2(2)
                    _tmbStep += 1 ' continue step back
                    If (i - _tmbStep < 0) Then 'stops error
                        Exit Do
                    End If
                Loop
            End If
        End If
    Next
    'return resorted array
    Return avDP
End Function

Me.Canvas.Image = New Bitmap(Me.Canvas.Width, Me.Canvas.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)

    Using g As Graphics = Graphics.FromImage(Me.Canvas.Image)
        Dim mainPP = New Point(Me.Canvas.ClientRectangle.Width / 2, Me.Canvas.ClientRectangle.Height / 2)
        For i = 0 To (G_ObjList.GetLength(0) - 1)
            Dim _tmpy() As Point = {
                p3d(G_ObjList(i, 0, 0), G_ObjList(i, 0, 1), G_ObjList(i, 0, 2), mainPP),
                p3d(G_ObjList(i, 1, 0), G_ObjList(i, 1, 1), G_ObjList(i, 1, 2), mainPP),
                p3d(G_ObjList(i, 2, 0), G_ObjList(i, 2, 1), G_ObjList(i, 2, 2), mainPP),
                p3d(G_ObjList(i, 3, 0), G_ObjList(i, 3, 1), G_ObjList(i, 3, 2), mainPP)
            }
            Dim br As New SolidBrush(Color.FromArgb(255, G_ObjList(i, 0, 0) * 0.3, G_ObjList(i, 0, 0) * 0.3, G_ObjList(i, 0, 0) * 0.3))
            g.FillPolygon(br, _tmpy)
        Next
end using
'be aware that this isn't all the code

0 个答案:

没有答案