# B4A Class[Class] Vector

#### wonder

##### Expert
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.
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
Size      = 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.
AngleD    = Radians * 180 / cPI
End Sub``````
Example: #### Attachments

• 2.2 KB Views: 113
Last edited:

#### Troberg

##### Well-Known Member
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.

• thedesolatesoul and wonder

#### klaus

##### Expert
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
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! • RandomCoder

#### wonder

##### Expert
Rework complete. Check the first post.

• Cableguy

#### Cableguy

##### Expert
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

#### thedesolatesoul

##### Expert
It would be used to simulate physical phenomena like force, acceleration, velocity, flux. It can be used to solve different problems like distance calculation, games and other stuff I can't think of right now!

• wonder

#### Cableguy

##### Expert
I see, so nothing directly related to graphics but to "Vectorial Forces"

• wonder

#### thedesolatesoul

##### Expert
Any line between two points can be represented by a vector or a resolution of multiple vectors with respect to an origin.
This is probably not directly related to vector graphics as in scalable/svgs etc but I'm sure they use it in vector graphics to represent lines.

• wonder

#### wonder

##### Expert
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

#### thedesolatesoul

##### Expert
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.

• wonder

#### klaus

##### Expert
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
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.

• nemiroG1 and wonder

#### Cableguy

##### Expert
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

#### thedesolatesoul

##### Expert
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)
Without an approximation a curve can be represented by a function and the limits of the parameters.

• wonder

#### Informatix

##### Expert
I'd like to mention that you have an extended class for vectors in SteeringBehaviors (SB_Vector2D) and a very complete class in libGDX (lgMathVector2).

• walterf25 and wonder

#### wonder

##### Expert
I've added a video to the first post, Paulo (@Cableguy) you should check it out! 