使用点3D不执行的vb.net中的线框立方体

时间:2016-07-06 12:20:35

标签: vb.net

Imports System.Drawing.Graphics
Imports System.Drawing.Pen
Imports System.Drawing.Color
Imports System.Drawing.Brush
Imports System.Drawing.Point

Public Class Main
    Protected m_pen As Pen
    Protected m_timer As Timer
    Protected m_vertices(10) As Point3D
    Protected m_faces(10, 4) As Integer
    Protected m_angle As Integer

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create a GDI+ Pen. This will be used to draw lines.
        m_pen = New Pen(Color.Red)

        InitCube()

        ' Create the timer.
        m_timer = New Timer()

        ' Set the timer interval to 33 milliseconds. This will give us 1000/34 ~ 30 frames per second.
        m_timer.Interval = 33

        ' Set the callback for the timer.
        AddHandler m_timer.Tick, AddressOf AnimationLoop

        ' Start the timer.
        m_timer.Start()
    End Sub

    Private Sub InitCube()
        ' Create an array with 12 points.
        m_vertices = New Point3D() {
                     New Point3D(0, 0, 1),
                     New Point3D(1, 0, -1),
                     New Point3D(1, 0, 1),
                     New Point3D(0, 0, 1),
                     New Point3D(0, 1, 1),
                     New Point3D(1, 1, 1),
                     New Point3D(0, 1, -1),
                     New Point3D(1, 1, -1),
                     New Point3D(-1, 0, -1),
                     New Point3D(-1, 0, 1),
                     New Point3D(-1, 1, 1),
                     New Point3D(-1, 1, -1)}

        ' Create an array representing the 6 faces of a cube. Each face is composed by indices to the vertex array
        ' above.
        m_faces = New Integer(,) {{0, 1, 2, 3}, {0, 8, 9, 3}, {0, 1, 7, 6}, {0, 8, 6, 10}, {10, 11, 4, 6}, {4, 5, 6, 7}, {2, 3, 4, 5}, {3, 4, 11, 9}, {8, 9, 10, 11}, {1, 2, 5, 7}}
    End Sub

    Private Sub AnimationLoop()
        ' Forces the Paint event to be called.
        Me.Invalidate()

        ' Update the variable after each frame.
        m_angle += 1
    End Sub

    Private Sub Main_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim t(8) As Point3D
        Dim f(4) As Integer
        Dim v As Point3D

        ' Clear the window
        e.Graphics.Clear(Color.LightBlue)

        ' Transform all the points and store them on the "t"- array.
        For i = 0 To 19
            v = m_vertices(i)
            t(i) = v.RotateX(m_angle).RotateY(m_angle).RotateZ(m_angle)
            t(i) = t(i).Project(Me.ClientSize.Width, Me.ClientSize.Height, 256, 4)
        Next

        ' Draw the wireframe cube. Uses the "m_faces" array to find the vertices that compose each face.
        For i = 0 To 17
            e.Graphics.DrawLine(m_pen, CInt(t(m_faces(i, 0)).X), CInt(t(m_faces(i, 0)).Y), CInt(t(m_faces(i, 1)).X), CInt(t(m_faces(i, 1)).Y))
            e.Graphics.DrawLine(m_pen, CInt(t(m_faces(i, 1)).X), CInt(t(m_faces(i, 1)).Y), CInt(t(m_faces(i, 2)).X), CInt(t(m_faces(i, 2)).Y))
            e.Graphics.DrawLine(m_pen, CInt(t(m_faces(i, 2)).X), CInt(t(m_faces(i, 2)).Y), CInt(t(m_faces(i, 3)).X), CInt(t(m_faces(i, 3)).Y))
            e.Graphics.DrawLine(m_pen, CInt(t(m_faces(i, 3)).X), CInt(t(m_faces(i, 3)).Y), CInt(t(m_faces(i, 0)).X), CInt(t(m_faces(i, 0)).Y))
        Next
    End Sub
End Class

此代码用于显示围绕所有3轴旋转连接在一起的两个立方体的动画。请注意,这是仅显示单个多维数据集的原始程序的修改代码。

该程序在遇到此循环时抛出错误,说“indexOutOfRangeExeption Occured”

For i = 0 To 19
        v = m_vertices(i)
        t(i) = v.RotateX(m_angle).RotateY(m_angle).RotateZ(m_angle)
        t(i) = t(i).Project(Me.ClientSize.Width, Me.ClientSize.Height, 256, 4)
    Next

导致错误的实际行(根据视觉工作室)是:

    t(i) = t(i).Project(Me.ClientSize.Width, Me.ClientSize.Height, 256, 4)

对此有何解决方法以及为什么要修复它,因为我不明白为什么这是一个问题。

1 个答案:

答案 0 :(得分:0)

m_vertices只有11个元素的大小,而您正在尝试阅读20个元素。 t的大小为9,您正在尝试阅读20。 您需要更大的数组,或者您需要更改代码执行的方式。 尝试增加Protected m_vertices(10) As Point3DDim t(8) As Point3D的数组大小,但您可能还有其他问题。