Here is your code using the new extended type syntax, which is available in the CVS version.
Type vector2d
x As Single
y As Single
End Type
Type vector3d
x As Single
y As Single
z As Single
End Type
'Used to identify which type of vector in union
Const is2d = 1
Const is3d = 2
Type Vector
Private:
vtype As Integer 'this identifies a 2D or 3D vector
Union 'we can use a union here to save some space
v2 As vector2d
v3 As vector3d
End Union 'Set up our overloaded constructors
Public:
Declare Constructor (Byval xx As Single,Byval yy As Single)
Declare Constructor (Byval xx As Single,Byval yy As Single, zz As Single)
Declare Property GetVectType() As Integer 'Read-only property
Declare Function Length() As Single
Declare Sub Normalise ()
Declare Operator Cast() As String
End Type
Constructor Vector (Byval xx As Single, Byval yy As Single)
this.vType = is2d
this.v2.x = xx
this.v2.y = yy
End Constructor
Constructor Vector (Byval xx As Single, Byval yy As Single, Byval zz As Single)
this.vType = is3d
this.v3.x = xx
this.v3.y = yy
this.v3.z = zz
End Constructor
Property Vector.GetVectType() As Integer
Return this.vType
End Property
Function Vector.Length () As Single
If this.vType = is2d Then
Return Sqr(this.v2.x^2 + this.v2.y^2)
Else
Return Sqr(this.v3.x^2 + this.v3.y^2 + this.v3.z^2)
End If
End Function
Sub Vector.Normalise()
Dim lr As Single
lr = 1.0 / this.Length
If this.vType = is2d Then
this.v2.x *= lr
this.v2.y *= lr
Else
this.v3.x *= lr
this.v3.y *= lr
this.v3.z *= lr
End If
End Sub
Operator Vector.Cast() As String
If this.vtype = is2d Then
Return this.v2.x & ", " & this.v2.y
Else
Return this.v3.x & ", " & this.v3.y & ", " & this.v3.z
End If
End Operator
Sub main
Dim vectType As Integer
Dim v2d As Vector = vector(-3.0,7.0)
vectType = v2d.GetVectType
If vectType = is2d Then
Print "Vector is 2D."
Else
Print "Vector is 3D."
End If
Print v2d
v2d.normalise
Print v2d
Print
Dim v3d As Vector = vector(-5.0,6.0,2.0)
vectType = v3d.GetVectType
If vectType = is2d Then
Print "Vector is 2D."
Else
Print "Vector is 3D."
End If
Print v3d
v3d.normalise
Print v3d
End Sub
main
Sleep
End