B4A Class [Class] Vector

wonder

Expert
Licensed User
Here's a simple, yet useful, Vector class which stores a 2D vector's magnitude, angle and x,y components.

What are vectors?

Initialization:
Not needed.

Functions:
B4X:
'Sets this vector's description and units
vecDescription(Vector_Description As String, Vector_Units As String)

'Sets this vector's directional components, magnitude and angle from an imaginary line between two points in space, given their X, Y coordinates.
vecLine(aX As Double, aY As Double, bX As Double, bY As Double)

'Sets this vector's directional components based on the given size and angle in degrees.
vecAngleMagD(Magnitude As Double, Degrees As Double)

'Sets this vector's directional components based on the given size and angle in degrees.
vecAngleMagR(Magnitude As Double, Radians As Double)

'Sets this Vector's directional components, magnitude and angle in radians based on the given angle in degrees.
vecAngleD(Degrees As Double)

'Sets this vector's directional components, magnitude and angle in degrees based on the given angle in radians.
vecAngleR(Radians As Double)
Usage:
You may set your vector components manually or by using its internal functions.
For example:
B4X:
Dim myVector as Vector
myVector.vecLine(0, 3, -1, 39)
Code:
B4X:
'Class module
Sub Class_Globals
    Dim Description As String
    Dim Units       As String
    Dim Size        As Double
    Dim AngleD      As Double
    Dim AngleR      As Double
    Dim dirX        As Double
    Dim dirY        As Double
End Sub

'Sets this vector's description and units
Public Sub vecDescription(Vector_Description As String, Vector_Units As String)
    Description = Vector_Description
    Units       = Vector_Units
End Sub

'Sets this vector's directional components, magnitude and angle from an imaginary line between two points in space, given their X, Y coordinates.
Public Sub vecLine(aX As Double, aY As Double, bX As Double, bY As Double)
    dirX      = bX - aX
    dirY      = bY - aY
    AngleD    = ATan2D(dirY, dirX)
    AngleR    = ATan2 (dirY, dirX)
    Size      = Sqrt(Power(dirX, 2) + Power(dirY, 2))
End Sub

'Sets this vector's directional components based on the given size and angle in degrees.
Public Sub vecAngleMagD(Magnitude As Double, Degrees As Double)
    AngleD    = Degrees
    AngleR    = Degrees * cPI / 180
    Size      = Magnitude
    dirX      = CosD(Degrees) * Magnitude
    dirY      = SinD(Degrees) * Magnitude
End Sub

'Sets this vector's directional components based on the given size and angle in degrees.
Public Sub vecAngleMagR(Magnitude As Double, Radians As Double)
    AngleD    = Radians * 180 / cPI
    AngleR    = Radians
    Size      = Magnitude
    dirX      = Cos(Radians) * Magnitude
    dirY      = Sin(Radians) * Magnitude
End Sub

'Sets this Vector's directional components, magnitude and angle in radians based on the given angle in degrees.
Public Sub vecAngleD(Degrees As Double)
    AngleD    = Degrees
    AngleR    = Degrees * cPI / 180
    dirX      = CosD(Degrees) * Size
    dirY      = SinD(Degrees) * Size
End Sub

'Sets this vector's directional components, magnitude and angle in degrees based on the given angle in radians.
Public Sub vecAngleR(Radians As Double)
    AngleD    = Radians * 180 / cPI
    AngleR    = Radians
    dirX      = Cos(Radians) * Size
    dirY      = Sin(Radians) * Size
End Sub
Example:
 

Attachments

Last edited:

Troberg

Well-Known Member
Licensed User
Nice! Not something I have use for at the moment, but still nice.

One small suggestion, though. Add this:

B4X:
Public Sub setAngleD(Value as Double)
  AngleD = Value
  AngleR = Value * 3.14159265359 / 180
End Sub

Public Sub setAngleR(Value as Double)
  AngleR = Value
  AngleD = Value * 180 / 3.14159265359
End Sub
That way, setting one of the angles will automatically update the other, reducing the risk of inconsistencies.

Another way of doing it is to keep a single internal storage for angle, then having getters that convert as needed. What's more efficient depends on if you intend to update more often than you read it, or the other way around.
 

klaus

Expert
Licensed User
Instead of using 3.14159265359 I suggest you to use cPI.
Better accuracy as you are working with Doubles.
And why not add a conversion constant ConvertD_R = cPI / 180. You do the division only once.
 

wonder

Expert
Licensed User
Nice! Not something I have use for at the moment, but still nice.

One small suggestion, though. Add this:

B4X:
Public Sub setAngleD(Value as Double)
  AngleD = Value
  AngleR = Value * 3.14159265359 / 180
End Sub

Public Sub setAngleR(Value as Double)
  AngleR = Value
  AngleD = Value * 180 / 3.14159265359
End Sub
That way, setting one of the angles will automatically update the other, reducing the risk of inconsistencies.

Another way of doing it is to keep a single internal storage for angle, then having getters that convert as needed. What's more efficient depends on if you intend to update more often than you read it, or the other way around.
Instead of using 3.14159265359 I suggest you to use cPI.
Better accuracy as you are working with Doubles.
And why not add a conversion constant ConvertD_R = cPI / 180. You do the division only once.
Thank you so much for the suggestions, guys! :) I will rework this class and post the results later.
I didn't know about cPI! This is why I love B4A so much, everyday I'm learning something new! :)
 

Cableguy

Expert
Licensed User
Hi Bruno,

I know you're a PS expert, and for you a vector is just as a commom word for you as a smoov is to me, but for guys like me, a vector is a line representation, as in vector graphics... If I'm correct, could you elaborate on how to use this in a 'real' app for the common (cable)guy?
 

wonder

Expert
Licensed User
Indeed, just like @thedesolatesoul said, a vector is a very useful mathematical concept which can be used for many diferent purposes, from vector graphics to simulations. Think flight simulators, Google self-driving cars, scientific investigation and Angry Birds. It was this kind of stuff that put man on the Moon in 1969. :)
 

thedesolatesoul

Expert
Licensed User
Indeed, just like @thedesolatesoul said, a vector is a very useful mathematical concept which can be used for many diferent purposes, from vector graphics to simulations. Think flight simulators, Google self-driving cars, scientific investigation and Angry Birds. It was this kind of stuff that put man on the Moon in 1969. :)
And also what brought the Apollo 13 mission back after conserving their fuel by using their residual velocity and some more to slingshot around the moon using its gravity to come back to Earth. Was just thinking about the velocity vector which is orthogonal to the gravitational force.
 

klaus

Expert
Licensed User
From Wikipedia:
Vector: In physics and geometry, a Euclidean vector, used to represent physical quantities that have both magnitude and direction.
Like, as thedesolatesoul already mentioned, forces, speeds, accelerations etc in physics.
In geometry a line between two points is a vector, it has an origin, a magnitude and a direction.
In this case there can be two vectors:
Origin first point, direction towards the second point.
Origin second point, direction towards the first point.
These are two different vectors.
You can use vectors in geometry to check if a point is on the right or left side of the vector looking to the direction or if a point in insides a polygon or outsides etc.
 
Last edited:

Troberg

Well-Known Member
Licensed User
Klaus described it well. I'll try to put it in ordinary words.

A vector is a measure that has a starting point, a magnitude and a direction.

Examples:

* I'm driving along the road at 100 km/h. My velocity vector can then be described as a vector that starts in the center of mass for my vehicle, points in the forward direction and is 100 km/h in length.

* I'm lifting the front of my lawnmower, so I can kick some ramps under the front wheels so I can get under it to remove the wire that has tangled the knives (real example from this weekend). The lawnmower exerts a force which can be described as a vector originating from the point where I grip the mower, points downwards and has a length of approximately 1500 N (which, on Earth, is about 150 kg). I, in turn, had to produce an equally strong counterforce to lift it. That would be a vector of equal length and origin, but pointing in the exact opposite direction.

* I want to draw a clock hand on screen. It can be described as a vector which starts at the center of the clock, has a length equal to the radius of the clock face and a direction that varies depending on the time.

So, basically, think of it as a line that has one end nailed down, a length and a direction.
 

Cableguy

Expert
Licensed User
So if I understand correctly, a vector is always a straight line (?) How can a curve be represented by a (or series of) vector(s)
 

wonder

Expert
Licensed User
Wow, I'm really glad to see so many people interested in this topic! Thanks for your contributions, guys!
I've added a video to the first post, Paulo (@Cableguy) you should check it out! :)
 

nemiroG1

Member
Licensed User
Perhaps a question that has already been answered, but is there a version of this that will handle 3D vectors? Just looking to do some 'simple' math on a couple of sets of 3D coordinates. I have read up on javax.vecmath.vector3d, but was unsure if there was already a B4A solution. Thanks!
 
Top