# Small 3D Wireframe Cube Example

Creates a 3D cube on the form which can be manipulated using the left, right, up and down arrow keys.

Original Author: Matthew Pearce

### Assumptions

This code uses Sine and Cosine to spin the cube around.

### Code

`' Name: 3D Cube Program' Author: Matthew Pearce' Date: Tuesday, 20th January 2004' Purpose: To display an interactive 3D cube on the screen using Sin and CosDim i1 As Integer, i2 As Integer, i3 As Integer, i4 As Integer, pitch As DoublePrivate Sub Form_Activate()  i1 = degtorad(0)  i2 = degtorad(90)  i3 = degtorad(180)  i4 = degtorad(270)  pitch = 0.5  UpdateCubeEnd SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)  Select Case KeyCode  Case vbKeyLeft:    i1 = i1 - 10    i2 = i2 - 10    i3 = i3 - 10    i4 = i4 - 10  Case vbKeyRight:    i1 = i1 + 10    i2 = i2 + 10    i3 = i3 + 10    i4 = i4 + 10  Case vbKeyUp:    If pitch > -1 Then      pitch = pitch - 0.1    End If  Case vbKeyDown:    If pitch < 1 Then      pitch = pitch + 0.1    End If  End Select  UpdateCubeEnd SubFunction degtorad(deg As Integer)  deg = deg / 360 * 3.14 * 200  degtorad = degEnd FunctionFunction LineCalc(inum1, inum2, inum3, inum4, add1, add2)  ' Calculate verteces  LX1 = LineCalcX(inum1)  LX2 = LineCalcX(inum2)  LY1 = LineCalcY(inum3, add1)  LY2 = LineCalcY(inum4, add2)  ' Draw lines between verteces  Line (LX1, LY1)-(LX2, LY2)End FunctionFunction LineCalcX(inum)  LineCalcX = Sin(inum / 100) * 1000 + 2000End FunctionFunction LineCalcY(inum, a1)  LineCalcY = Cos(inum / 100) * 400 * pitch + a1End FunctionFunction UpdateCube()  Refresh  ' Draw Top Face  LineCalc i1, i2, i1, i2, 1000, 1000  LineCalc i2, i3, i2, i3, 1000, 1000  LineCalc i3, i4, i3, i4, 1000, 1000  LineCalc i4, i1, i4, i1, 1000, 1000  ' Draw Sides  LineCalc i1, i2, i1, i2, 2000, 2000  LineCalc i2, i3, i2, i3, 2000, 2000  LineCalc i3, i4, i3, i4, 2000, 2000  LineCalc i4, i1, i4, i1, 2000, 2000  ' Draw Bottom Face  LineCalc i1, i1, i1, i1, 1000, 2000  LineCalc i2, i2, i2, i2, 1000, 2000  LineCalc i3, i3, i3, i3, 1000, 2000  LineCalc i4, i4, i4, i4, 1000, 2000End Function`

Posted: 2003-06-01
By: ArchiveBot
Viewed: 109 times

Categories

Visual Basic 6

Attachments

No attachments for this post