'This is your point
private Type tPoint3D
x as single
y as single
z as single
end Type
'This line type connects 2 points
private Type tLine
p1 as Long
p2 as Long
end Type
'This will become a cube
dim p(7) as tPoint3D
dim l(11) as tLine
Private Sub Form_Load()
'Define the edge points
'Top rect
with p(0): .x = -1: .y = -1: .z = 1: end with
with p(1): .x = 1: .y = -1: .z = 1: end with
with p(2): .x = 1: .y = 1: .z = 1: end with
with p(3): .x = -1: .y = 1: .z = 1: end with
'Bottom rect
with p(4): .x = -1: .y = -1: .z = -1: end with
with p(5): .x = 1: .y = -1: .z = -1: end with
with p(6): .x = 1: .y = 1: .z = -1: end with
with p(7): .x = -1: .y = 1: .z = -1: end with
'Define the lines
'Top rect
with l(0): .p1 = 0: .p2 = 1: end With
with l(1): .p1 = 1: .p2 = 2: end With
with l(2): .p1 = 2: .p2 = 3: end With
with l(3): .p1 = 3: .p2 = 0: end With
'Bottom rect
with l(4): .p1 = 4: .p2 = 5: end With
with l(5): .p1 = 5: .p2 = 6: end With
with l(6): .p1 = 6: .p2 = 7: end With
with l(7): .p1 = 7: .p2 = 4: end With
'Connectors
with l(8): .p1 = 0: .p2 = 4: end With
with l(9): .p1 = 1: .p2 = 5: end With
with l(10): .p1 = 2: .p2 = 6: end With
with l(11): .p1 = 3: .p2 = 7: end With
'Setup the form for drawing
scaleMode = vbPixels
End Sub
Public Sub Form_MouseDown(b As integer, s As integer, x As single, y As single)
'Draw the lines
dim A as Long
'This will be screen coordinates
dim x1 as long
dim x2 as long
dim y1 as long
dim y2 as long
'This is the screen middle
dim tX as long
dim tY as long
'Get screen middle
tX = (scaleWidth / 2)
tX = (scaleHeight / 2)
'Since 3d positions are relative we need a size
Dim size As Single
size = 100
'The camera says where to draw the cube
dim cam As tPoint3D
'Move camera with mouse
cam.x = (x - tX) / size
cam.y = (y - tY) / size
cam.z = 5
'FOV (field-of-view) corrects the stretching
dim fov as single
fov = 3
me.Cls
for a = 0 to uBound(l)
'Get screen coordinates of both points
with p(l(a).p1)
x1 = (.x + cam.x) / (.z + cam.z) * size * fov
y1 = (.y + cam.y) / (.z + cam.z) * size * fov
end with
with p(l(a).p2)
x2 = (.x + cam.x) / (.z + cam.z) * size * fov
y2 = (.y + cam.y) / (.z + cam.z) * size * fov
end with
'Draw line (origin at screen center)
Line (x1+tX, y1+tY)-(x2+tX, y2+tY)
next
End Sub