2016-07-06 37 views
2
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轴旋转的两个立方体的动画。请注意,这是仅显示单个多维数据集的原始程序的修改代码。vb.net线框立方体使用点3D未执行

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

该程序时,如果击中该循环说“indexOutOfRangeExeption出现”

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 

实际线(根据视觉工作室),其导致该错误是这样的引发错误这是什么修复以及为什么它修复它,因为我不明白为什么这是一个问题。

回答

0

m_vertices只有尺寸为11元,而你试图读取20 t的大小9,和你想读20 您需要更大的阵列,或者您需要更改代码执行的方式。 尝试增加数组大小Protected m_vertices(10) As Point3DDim t(8) As Point3D,尽管您可能还有其他问题。