ART v1.0-alpha
A Robot Template that raises the floor for VRC teams
Loading...
Searching...
No Matches
art::Vec2 Struct Reference

A Utility 2D Vector class. More...

#include <Vec2.h>

Public Member Functions

 Vec2 ()
 Construct a new Vec 2 object.
 
double magnitude () const
 Gets the magnitude of the Vector.
 
double direction () const
 Gets the direction of the Vector.
 
Vec2 normalize () const
 Returns a unit Vector with the same direction.
 
double distTo (Vec2 target) const
 Gets the distance from this vector to another.
 
double angleTo (Vec2 target) const
 Gets the angle from this vector to another.
 
Vec2 operator+ (Vec2 const &obj) const
 Returns the sum of 2 vectors.
 
Vec2 operator- (Vec2 const &obj) const
 Returns the difference of 2 vectors.
 
Vec2 operator* (double const &scale) const
 Returns a vector after scaling it.
 
double operator* (Vec2 const &other) const
 Returns the Dot Product of 2 vectors.
 
Vec2operator+= (Vec2 const &obj)
 Adds another vector to this one.
 
Vec2operator-= (Vec2 const &obj)
 Substracts another vector from this one.
 
Vec2operator*= (double const &scale)
 Changes the vector by a scale.
 

Static Public Member Functions

static Vec2 XandY (double x, double y)
 Constructs a vector with the specified x and y components.
 
static Vec2 dirAndMag (double dir, double mag)
 Constructs a vector with the specified direction and magnitude.
 

Public Attributes

double x
 Stores the X component of the Vector.
 
double y
 Stores the Y component of the Vector.
 

Detailed Description

A Utility 2D Vector class.

The Vec2 class aims to provide a better way to store coordinates, travel and displacement in a 2d space. Vec2 objects store thier x and y components, but has methods to access thier direction and magnitude as well. Vectors can be added together, scaled or multiplied by each other to find the dot product.

The components' units can be whatever you want, but the direction will always be in radians, as the math works best that way. The Angle Unit Utility Class defaults to Radians as a result, but as of right now, using the Angle class is optional.

Todo
integrate Unit Classes to Vec2

Definition at line 38 of file Vec2.h.

Constructor & Destructor Documentation

◆ Vec2()

art::Vec2::Vec2 ( )

Construct a new Vec 2 object.

The default constructor to be used when no constructing function is called. Initializes the X and Y coordinates to 0, resulting in the direction and magnitude also being 0 as well.

Definition at line 19 of file Vec2.cpp.

Member Function Documentation

◆ angleTo()

double art::Vec2::angleTo ( Vec2 target) const

Gets the angle from this vector to another.

Parameters
targetthe vector to find the angle too
Returns
double - the angle to the other vector

Finds the angle(in radians) of the difference of 2 vectors. In practice, this is most often used to find the angle connecting 2 points. This method simply returns the angle after finding the difference between the two vectors, so if you want both the direction and magnitude, you can simply store the difference between the vectors:

art::Vec2 difference = ending - starting;
A Utility 2D Vector class.
Definition Vec2.h:39

Both ending and starting are Vec2s as well.

Definition at line 46 of file Vec2.cpp.

◆ dirAndMag()

Vec2 art::Vec2::dirAndMag ( double dir,
double mag )
static

Constructs a vector with the specified direction and magnitude.

Parameters
dirthe direction of the vector(in radians)
magthe magnitude of the vector
Returns
Vec2 - the constructed vector

One of 2 construction functions within the Vec2 class. If you want to create a new vector with a specified direction(in radians) and magnitude, you will need to call this by using the accessor operator(::) and store the returned value.

static Vec2 dirAndMag(double dir, double mag)
Constructs a vector with the specified direction and magnitude.
Definition Vec2.cpp:104

Understanding directions using radians can be somewhat strange if you're not already familiar. The Angle Unit class can help by converting degrees or revolutions to radians.

Definition at line 104 of file Vec2.cpp.

◆ direction()

double art::Vec2::direction ( ) const

Gets the direction of the Vector.

Returns
double - The Direction of the Vector

The value returned is in radians, but is calculated based on the units used when the vector was constructed. If different units were mixed, this value is probably useless.

Definition at line 26 of file Vec2.cpp.

◆ distTo()

double art::Vec2::distTo ( Vec2 target) const

Gets the distance from this vector to another.

Parameters
targetthe vector to find the distance too
Returns
double - the distance to the other vector

Finds the magnitude of the difference of 2 vectors. In practice, this is most often used to find the distance between 2 points. This method simply returns the magnitude after finding the difference between the two vectors, so if you want both the direction and magnitude, you can simply store the difference between the vectors:

art::Vec2 difference = ending - starting;

Both ending and starting are Vec2s as well.

Definition at line 40 of file Vec2.cpp.

◆ magnitude()

double art::Vec2::magnitude ( ) const

Gets the magnitude of the Vector.

Returns
double - The Magnitude of the Vector

The unit returned is dependant on the units used when the vector was constructed. If different units were mixed, this value is probably useless.

Definition at line 21 of file Vec2.cpp.

◆ normalize()

Vec2 art::Vec2::normalize ( ) const

Returns a unit Vector with the same direction.

Returns
Vec2 - The normalized Vector

The process of normalization divides a vector by it's magnitude, resulting in a vector with a magnitude of 1 unit. This vector preserves the original direction, so it can be useful for other calculations or simply to change the scale of a vector.

Definition at line 33 of file Vec2.cpp.

◆ operator*() [1/2]

Vec2 art::Vec2::operator* ( double const & scale) const

Returns a vector after scaling it.

Parameters
scalethe number to scale by
Returns
Vec2 the resultant vector

Functionally, this just multiplies the components by the scale. The vector used in this operation ins't modified, so it retains its original values. The result must be stored or used immediately.

Definition at line 64 of file Vec2.cpp.

◆ operator*() [2/2]

double art::Vec2::operator* ( Vec2 const & other) const

Returns the Dot Product of 2 vectors.

Parameters
otherthe other vector to calculate using
Returns
double - the Dot Product r scalar product of the 2 vectors

The Dot Product doesn't have that many use-cases, but it has use in some more calculations for more complex things. For example in physics, it is used to calculate work by using the vectors of the force and distance travelled.

Neither of the vectors used in this operation are modified, they retain thier original values.

Definition at line 70 of file Vec2.cpp.

◆ operator*=()

Vec2 & art::Vec2::operator*= ( double const & scale)

Changes the vector by a scale.

Parameters
scalethe scale to multiply by
Returns
Vec2& - a refrence to this vector

This does modify the vector and can be used to scale a vector.

Definition at line 89 of file Vec2.cpp.

◆ operator+()

Vec2 art::Vec2::operator+ ( Vec2 const & obj) const

Returns the sum of 2 vectors.

Parameters
objthe other vector to add
Returns
Vec2 the resultant vector

Neither of the vectors used in this operation are modified, they retain thier original values. The result must be stored or used immediately.

Definition at line 52 of file Vec2.cpp.

◆ operator+=()

Vec2 & art::Vec2::operator+= ( Vec2 const & obj)

Adds another vector to this one.

Parameters
objthe vector to add
Returns
Vec2& - a refrence to this vector

This does modify the vector and can be used to increment one vector by another.

Definition at line 75 of file Vec2.cpp.

◆ operator-()

Vec2 art::Vec2::operator- ( Vec2 const & obj) const

Returns the difference of 2 vectors.

Parameters
objthe other vector to subtract
Returns
Vec2 the resultant vector

Functionally, this just flips the second vector and takes the sum. Neither of the vectors used in this operation are modified, they retain thier original values. The result must be stored or used immediately.

Definition at line 58 of file Vec2.cpp.

◆ operator-=()

Vec2 & art::Vec2::operator-= ( Vec2 const & obj)

Substracts another vector from this one.

Parameters
objthe vector to subtract
Returns
Vec2& - a refrence to this vector

This does modify the vector and can be used to decrement one vector by another.

Definition at line 82 of file Vec2.cpp.

◆ XandY()

Vec2 art::Vec2::XandY ( double x,
double y )
static

Constructs a vector with the specified x and y components.

Parameters
xthe x component of the vector
ythe y component of the vector
Returns
Vec2 - the constructed vector

One of 2 construction functions within the Vec2 class. If you want to create a new vector with a specified x and y, you will need to call this by using the accessor operator(::) and store the returned value.

static Vec2 XandY(double x, double y)
Constructs a vector with the specified x and y components.
Definition Vec2.cpp:96

Definition at line 96 of file Vec2.cpp.

Member Data Documentation

◆ x

double art::Vec2::x

Stores the X component of the Vector.

A Unitless value, so any unit can be used. However, its best to keep the components and magnitude all the same unit.

Definition at line 46 of file Vec2.h.

◆ y

double art::Vec2::y

Stores the Y component of the Vector.

A Unitless value, so any unit can be used. However, its best to keep the components and magnitude all the same unit.

Definition at line 54 of file Vec2.h.


The documentation for this struct was generated from the following files: