  # B4A Class [Class] Vector

Discussion in 'Additional libraries, classes and official updates' started by wonder, May 17, 2015.

1. 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:
Code:
`'Sets this vector's description and unitsvecDescription(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:
Code:
`Dim myVector as VectormyVector.vecLine(0, 3, -1, 39)`
Code:
Code:
`'Class moduleSub 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 DoubleEnd Sub'Sets this vector's description and unitsPublic Sub vecDescription(Vector_Description As String, Vector_Units As String)    Description = Vector_Description    Units       = Vector_UnitsEnd 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) * MagnitudeEnd 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) * MagnitudeEnd 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) * SizeEnd 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) * SizeEnd Sub`
Example: #### Attached Files:

• ###### Vector.bas
File size:
2.2 KB
Views:
37
Last edited: May 19, 2015
nemiroG1, Troberg, inakigarm and 2 others like this.

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

One small suggestion, though. Add this:

Code:
`Public Sub setAngleD(Value as Double)  AngleD = Value  AngleR = Value * 3.14159265359 / 180End SubPublic Sub setAngleR(Value as Double)  AngleR = Value  AngleD = Value * 180 / 3.14159265359End 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 like this.
3. 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.

4. 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 likes this.
5. Rework complete. Check the first post.

Cableguy likes this.
6. 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 likes this.
7. 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 likes this.
8. I see, so nothing directly related to graphics but to "Vectorial Forces"

wonder likes this.
9. 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 likes this.
10. 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 likes this.
11. 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 likes this.
12. 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: May 19, 2015
nemiroG1, wonder and thedesolatesoul like this.

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 like this.
14. 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 likes this.
15. Without an approximation a curve can be represented by a function and the limits of the parameters.

wonder likes this.
16. 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 like this.
17. 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! 18. 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!