Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: Stonemonkey on March 11, 2007
-
Just as the topic says, and I came up with this little bit of code but it could be taken much further.
type vector2d
x as single
y as single
end type
type vector3d
x as single
y as single
z as single
end type
function length overload(byref vector as vector2d)as single
function=sqr(vector.x^2+vector.y^2)
end function
function length(byref vector as vector3d)as single
function=sqr(vector.x^2+vector.y^2+vector.z^2)
end function
sub normalise overload(byref vector as vector2d)
dim lr as single=1.0/length(vector)
vector.x*=lr
vector.y*=lr
end sub
sub normalise(byref vector as vector3d)
dim lr as single=1.0/length(vector)
vector.x*=lr
vector.y*=lr
vector.z*=lr
end sub
function set_vector overload(byval x as single,byval y as single)as vector2d
dim vector as vector2d
vector.x=x
vector.y=y
function=vector
end function
function set_vector(byval x as single,byval y as single,byval z as single)as vector3d
dim vector as vector3d
vector.x=x
vector.y=y
vector.z=z
function=vector
end function
sub print_vector overload(byref vector as vector2d)
print vector.x;",";vector.y
end sub
sub print_vector(byref vector as vector3d)
print vector.x;",";vector.y;",";vector.z
end sub
sub main
dim v2d as vector2d=set_vector(-3.0,7.0)
print_vector v2d
normalise v2d
print_vector v2d
print
dim v3d as vector3d=set_vector(-5.0,6.0,2.0)
print_vector v3d
normalise v3d
print_vector v3d
end sub
main
sleep
end
-
Nice. You could extend this further by using the extended types in the CVS version.
-
Cool rdc, I've not gone into types in a lot of depth myself so far but for what I have used them has been useful. Even knowing a bit about them I found the exampes with FB not particularly easy to follow and thought it might be helpful to have a topic with bits n pieces from anyone showing how they can be used.
EDIT:
can FB do inline functions?
-
Inline function aren't supported, at least yet. I am not sure if that is in the works or not. It would be quite handy though.
-
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
-
Ah, nice one rdc. I'll have a look through this.
-
I just converted your code over; there are probably other things you might need here such as member properties for the individual vector elements. The external consts aren't really needed. You could use an internal identifier such as 2 for 2D and 3 for 3D. I added the consts to illustrate Properties and so that you could determine what type of vector this is (such as passing it to sub of function).
-
operator overloading and typing tricks is getting quite C++y there!
Jim
-
rdc-"I just converted your code over"
yep, it's good to see it done like that though, I know what's going on so thanks.
Jim-"operator overloading and typing tricks is getting quite C++y there!"
Even just the basics have helped me so far to getting into c++, I'm slowly getting there.
-
The extended types have their uses, but it will get much more interesting when we have true classes in FB. I am not sure when that will happen, but it can't be too far off.