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

Vb.net 如何通过函数对多维数组进行排序,vb.net,Vb.net,我在vb.net 2012中对多维数组进行排序时遇到了一些问题,我尝试使用自定义排序算法通过z轴对坐标进行排序,以便绘制四边形。这使得形状可以像css中的z索引一样按顺序绘制 (另外,如何向阵列中添加对象,以便定义多边形颜色) “现在x和y部分中没有任何内容 尺寸G_对象列表(,)为双精度={ {{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,

我在vb.net 2012中对多维数组进行排序时遇到了一些问题,我尝试使用自定义排序算法通过z轴对坐标进行排序,以便绘制四边形。这使得形状可以像css中的z索引一样按顺序绘制

(另外,如何向阵列中添加对象,以便定义多边形颜色)

“现在x和y部分中没有任何内容
尺寸G_对象列表(,)为双精度={
{{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}}
}
函数p3DOrder(ByVal a(,)为双精度,ByVal p为点)
“0是点
“1是深度
'2是转换为p3d后到p的距离(可能仅用于此处,但仅用于其他用途)
'a(深度/点,infopoint
'创建新锯齿阵列(不再是锯齿阵列)
尺寸avDP(2,a.GetLength(0)-1)
'将数据排序到新的交错数组中
对于i=0到(a.GetLength(0)-1)
'计算平均值并设置数组
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+(a(i,2,0)-p.X)^2+(a(i,2,1)-p.Y)^2+(a(i,3,0)-p.X)^2+(a(i,3,1)-p.Y)计算主透视点的距离
avDP(1,i)=(a(i,0,2)+a(i,1,2)+a(i,2,2)+a(i,3,2))/4'并排设置深度并计算距离
avDP(0,i)={新点(a(i,0,0),a(i,0,1)),新点(a(i,1,0),a(i,1,1)),新点(a(i,2,0),a(i,2,1)),新点(a(i,3,0),a(i,3,1))}
'必须以这种方式保持变量,以防止数据对之间的分离
'排序锯齿数组
如果i avDP(1,i+1))那么
'测试开关程序
Dim_tm0()={avDP(0,i),avDP(1,i),avDP(2,i)}'临时存储要切换的数据
Dim_tm1()={avDP(0,i+1),avDP(1,i+1),avDP(2,i+1)}
avDP(0,i)=_tm1(0)'切换阵列数据
avDP(1,i)=_tm1(1)
avDP(2,i)=_tm1(2)
avDP(0,i+1)=\u tm0(0)
avDP(1,i+1)=\u tm0(1)
avDP(2,i+1)=\u tm0(2)
Dim_tmbStep作为整数=0
执行While(avDP(1,i-_-tmbStep)>avDP(1,i+1-_-tmbStep))'如果为真,请后退
Dim_tm2()={avDP(0,i-_tmbStep),avDP(1,i-_tmbStep),avDP(2,i-_tmbStep)}'临时存储要切换的数据
Dim_tm3()={avDP(0,i+1-_tmbStep),avDP(1,i+1-_tmbStep),avDP(2,i+1-_tmbStep)}
avDP(0,i-_tmbStep)=_tm3(0)'切换阵列数据
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'继续后退
如果(i-_tmbStep<0),则“停止错误”
退出Do
如果结束
环
如果结束
如果结束
下一个
'返回-返回阵列
返回avDP
端函数
Me.Canvas.Image=新位图(Me.Canvas.Width、Me.Canvas.Height、System.Drawing.Imaging.PixelFormat.Format24bppRgb)
使用g作为Graphics=Graphics.FromImage(Me.Canvas.Image)
Dim mainPP=新点(Me.Canvas.ClientRectangle.Width/2,Me.Canvas.ClientRectangle.Height/2)
对于i=0到(G_ObjList.GetLength(0)-1)
Dim _tmpy()作为点={
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作为新的SolidBrush(颜色:从argb(255,G_对象列表(i,0,0)*0.3,G_对象列表(i,0,0)*0.3,G_对象列表(i,0,0)*0.3))
g、 填充多边形(br,_tmpy)
下一个
终端使用
“请注意,这并不是全部代码

创建一个X-Y-Z类要容易得多,把它们放在一个列表中,并在一行中排序:
sorted=myPoint.OrderBy(Function(j)j.Z).ToArray()
请阅读并接受。如果您认为需要对2D数组进行排序,那么您几乎肯定不应该首先使用2D数组。如果数组中的每个“行”表示单个实体,则应使用现有类型或定义自己的类型来表示该实体,然后创建该类型的1D数组。也就是说,即使使用2D数组,也不需要自定义排序算法。您将使用与排序1D数组时完全相同的算法。任何这样的算法都将包括反复比较两个元素,然后在适当的情况下重新排列它们。你在这里也会做同样的事情。唯一的区别是,对于每个“行”,比较一个“列”中的值,然后重新排列所有“列”中的值。如果必须使用二维数组,请尝试为一维数组实现类似快速排序的功能,然后进行简单的调整。
'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