simplevectors  0.3.9
Simple vector implementations in C++
Classes | Functions
embed.h File Reference

A minimized version of vectors for embedded devices without access to the STL (such as on an Arduino, hence the ".h" instead of ".hpp"). More...

#include <math.h>
Include dependency graph for embed.h:

Go to the source code of this file.

Classes

struct  svector::EmbVec2D
 A minimal 2D vector representation. More...
 
struct  svector::EmbVec3D
 A minimal 3D vector representation. More...
 

Functions

float svector::x (const EmbVec2D &v)
 Gets the x-component of a 2D vector. More...
 
void svector::x (EmbVec2D &v, const float xValue)
 Sets the x-component of a 2D vector. More...
 
float svector::x (const EmbVec3D &v)
 Gets the x-component of a 3D vector. More...
 
void svector::x (EmbVec3D &v, const float xValue)
 Sets the x-component of a 3D vector. More...
 
float svector::y (const EmbVec2D &v)
 Gets the y-component of a 2D vector. More...
 
void svector::y (EmbVec2D &v, const float yValue)
 Sets the y-component of a 2D vector. More...
 
float svector::y (const EmbVec3D &v)
 Gets the y-component of a 3D vector. More...
 
void svector::y (EmbVec3D &v, const float yValue)
 Sets the y-component of a 3D vector. More...
 
float svector::z (const EmbVec3D &v)
 Gets the z-component of a 3D vector. More...
 
void svector::z (EmbVec3D &v, const float zValue)
 Sets the z-component of a 3D vector. More...
 
EmbVec2D svector::operator+ (const EmbVec2D &lhs, const EmbVec2D &rhs)
 Vector addition. More...
 
EmbVec2D svector::operator- (const EmbVec2D &lhs, const EmbVec2D &rhs)
 Vector subtraction. More...
 
EmbVec2D svector::operator- (const EmbVec2D &vec)
 Negative of a vector. More...
 
EmbVec2D svector::operator+ (const EmbVec2D &vec)
 Positive of a vector. More...
 
EmbVec2D svector::operator* (const EmbVec2D &lhs, const float rhs)
 Scalar multiplication. More...
 
EmbVec2D svector::operator/ (const EmbVec2D &lhs, const float rhs)
 Scalar division. More...
 
bool svector::operator== (const EmbVec2D &lhs, const EmbVec2D &rhs)
 Compares equality of two vectors. More...
 
bool svector::operator!= (const EmbVec2D &lhs, const EmbVec2D &rhs)
 Compares inequality of two vectors. More...
 
float svector::dot (const EmbVec2D &lhs, const EmbVec2D &rhs)
 Calculates the dot product of two vectors. More...
 
float svector::magn (const EmbVec2D &vec)
 Gets the magnitude of the vector. More...
 
float svector::angle (const EmbVec2D &vec)
 Gets the angle of a 2D vector in radians. More...
 
EmbVec2D svector::normalize (const EmbVec2D &vec)
 Normalizes a vector. More...
 
bool svector::isZero (const EmbVec2D &vec)
 Determines whether a vector is a zero vector. More...
 
EmbVec2D svector::rotate (const EmbVec2D &vec, const float ang)
 Rotates vector by a certain angle. More...
 
EmbVec3D svector::operator+ (const EmbVec3D &lhs, const EmbVec3D &rhs)
 Vector addition. More...
 
EmbVec3D svector::operator- (const EmbVec3D &lhs, const EmbVec3D &rhs)
 Vector subtraction. More...
 
EmbVec3D svector::operator- (const EmbVec3D &vec)
 Negative of a vector. More...
 
EmbVec3D svector::operator+ (const EmbVec3D &vec)
 Positive of a vector. More...
 
EmbVec3D svector::operator* (const EmbVec3D &lhs, const float rhs)
 Scalar multiplication. More...
 
EmbVec3D svector::operator/ (const EmbVec3D &lhs, const float rhs)
 Scalar division. More...
 
bool svector::operator== (const EmbVec3D &lhs, const EmbVec3D &rhs)
 Compares equality of two vectors. More...
 
bool svector::operator!= (const EmbVec3D &lhs, const EmbVec3D &rhs)
 Compares inequality of two vectors. More...
 
float svector::dot (const EmbVec3D &lhs, const EmbVec3D &rhs)
 Calculates the dot product of two vectors. More...
 
EmbVec3D svector::cross (const EmbVec3D &lhs, const EmbVec3D &rhs)
 Cross product of two vectors. More...
 
float svector::magn (const EmbVec3D &vec)
 Gets the magnitude of the vector. More...
 
EmbVec3D svector::normalize (const EmbVec3D &vec)
 Normalizes a vector. More...
 
bool svector::isZero (const EmbVec3D &vec)
 Determines whether a vector is a zero vector. More...
 
float svector::alpha (const EmbVec3D &vec)
 Gets α angle. More...
 
float svector::beta (const EmbVec3D &vec)
 Gets β angle. More...
 
float svector::gamma (const EmbVec3D &vec)
 Gets γ angle. More...
 
EmbVec3D svector::rotateAlpha (const EmbVec3D &vec, const float ang)
 Rotates around x-axis. More...
 
EmbVec3D svector::rotateBeta (const EmbVec3D &vec, const float ang)
 Rotates around y-axis. More...
 
EmbVec3D svector::rotateGamma (const EmbVec3D &vec, const float ang)
 Rotates around z-axis. More...
 

Detailed Description

A minimized version of vectors for embedded devices without access to the STL (such as on an Arduino, hence the ".h" instead of ".hpp").

This file is meant to be a standalone file, so it is not included in vectors.hpp.

Function Documentation

◆ alpha()

float svector::alpha ( const EmbVec3D vec)
inline

Gets α angle.

α is the angle between the vector and the x-axis.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Parameters
vecA 3D Vector.
Returns
α.

◆ angle()

float svector::angle ( const EmbVec2D vec)
inline

Gets the angle of a 2D vector in radians.

The angle will be in the range (-π, π].

Parameters
vecA 2D vector.
Returns
angle of the vector.

◆ beta()

float svector::beta ( const EmbVec3D vec)
inline

Gets β angle.

β is the angle between the vector and the y-axis.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Parameters
vecA 3D Vector.
Returns
β.

◆ cross()

EmbVec3D svector::cross ( const EmbVec3D lhs,
const EmbVec3D rhs 
)
inline

Cross product of two vectors.

Parameters
lhsA 3D vector.
rhsA 3D vector.
Returns
The crossed 3D vector.

◆ dot() [1/2]

float svector::dot ( const EmbVec2D lhs,
const EmbVec2D rhs 
)
inline

Calculates the dot product of two vectors.

Parameters
lhsFirst vector.
rhsSecond vector.
Returns
The dot product of lhs and rhs.

◆ dot() [2/2]

float svector::dot ( const EmbVec3D lhs,
const EmbVec3D rhs 
)
inline

Calculates the dot product of two vectors.

Parameters
lhsFirst vector.
rhsSecond vector.
Returns
The dot product of lhs and rhs.

◆ gamma()

float svector::gamma ( const EmbVec3D vec)
inline

Gets γ angle.

γ is the angle between the vector and the z-axis.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Parameters
vecA 3D Vector.
Returns
γ.

◆ isZero() [1/2]

bool svector::isZero ( const EmbVec2D vec)
inline

Determines whether a vector is a zero vector.

Returns
Whether the given vector is a zero vector.

◆ isZero() [2/2]

bool svector::isZero ( const EmbVec3D vec)
inline

Determines whether a vector is a zero vector.

Returns
Whether the given vector is a zero vector.

◆ magn() [1/2]

float svector::magn ( const EmbVec2D vec)
inline

Gets the magnitude of the vector.

Parameters
vecA 2D vector.
Returns
magnitude of vector.

◆ magn() [2/2]

float svector::magn ( const EmbVec3D vec)
inline

Gets the magnitude of the vector.

Parameters
vecA 3D vector.
Returns
magnitude of vector

◆ normalize() [1/2]

EmbVec2D svector::normalize ( const EmbVec2D vec)
inline

Normalizes a vector.

Finds the unit vector with the same direction angle as the current vector.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Parameters
vecA 2D vector.
Returns
Normalized vector.

◆ normalize() [2/2]

EmbVec3D svector::normalize ( const EmbVec3D vec)
inline

Normalizes a vector.

Finds the unit vector with the same direction angle as the current vector.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Parameters
vecA 3D vector.
Returns
Normalized vector.

◆ operator!=() [1/2]

bool svector::operator!= ( const EmbVec2D lhs,
const EmbVec2D rhs 
)
inline

Compares inequality of two vectors.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A boolean representing whether the two vectors do not compare equal.

◆ operator!=() [2/2]

bool svector::operator!= ( const EmbVec3D lhs,
const EmbVec3D rhs 
)
inline

Compares inequality of two vectors.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A boolean representing whether the two vectors do not compare equal.

◆ operator*() [1/2]

EmbVec2D svector::operator* ( const EmbVec2D lhs,
const float  rhs 
)
inline

Scalar multiplication.

Performs scalar multiplication and returns a new vector representing the product.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the scalar product.

◆ operator*() [2/2]

EmbVec3D svector::operator* ( const EmbVec3D lhs,
const float  rhs 
)
inline

Scalar multiplication.

Performs scalar multiplication and returns a new vector representing the product.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the scalar product.

◆ operator+() [1/4]

EmbVec2D svector::operator+ ( const EmbVec2D lhs,
const EmbVec2D rhs 
)
inline

Vector addition.

Performs vector addition and returns a new vector representing the sum of the two vectors.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the vector sum.

◆ operator+() [2/4]

EmbVec2D svector::operator+ ( const EmbVec2D vec)
inline

Positive of a vector.

Represents value of vector, with the unary plus operator applied to each component. In almost all cases, this should result in the original vector.

Parameters
vecA 2D vector.
Returns
The same 2D vector.

◆ operator+() [3/4]

EmbVec3D svector::operator+ ( const EmbVec3D lhs,
const EmbVec3D rhs 
)
inline

Vector addition.

Performs vector addition and returns a new vector representing the sum of the two vectors.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the vector sum.

◆ operator+() [4/4]

EmbVec3D svector::operator+ ( const EmbVec3D vec)
inline

Positive of a vector.

Represents value of vector, with the unary plus operator applied to each component. In almost all cases, this should result in the original vector.

Parameters
vecA 3D vector.
Returns
The same 3D vector.

◆ operator-() [1/4]

EmbVec2D svector::operator- ( const EmbVec2D lhs,
const EmbVec2D rhs 
)
inline

Vector subtraction.

Performs vector subtraction and returns a new vector representing the difference of the two vectors.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the vector sum.

◆ operator-() [2/4]

EmbVec2D svector::operator- ( const EmbVec2D vec)
inline

Negative of a vector.

Makes all components of a vector negative.

This can also be thought of flipping the direction of the vector.

Parameters
vecA 2D vector.
Returns
A new vector representing the negative of the given vector.

◆ operator-() [3/4]

EmbVec3D svector::operator- ( const EmbVec3D lhs,
const EmbVec3D rhs 
)
inline

Vector subtraction.

Performs vector subtraction and returns a new vector representing the difference of the two vectors.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the vector sum.

◆ operator-() [4/4]

EmbVec3D svector::operator- ( const EmbVec3D vec)
inline

Negative of a vector.

Makes all components of a vector negative of what they currently are.

This can also be thought of flipping the direction of the vector.

Parameters
vecA 3D vector.
Returns
A new vector representing the negative of the given vector.

◆ operator/() [1/2]

EmbVec2D svector::operator/ ( const EmbVec2D lhs,
const float  rhs 
)
inline

Scalar division.

Performs scalar division and returns a new vector representing the quotient.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the scalar product.

◆ operator/() [2/2]

EmbVec3D svector::operator/ ( const EmbVec3D lhs,
const float  rhs 
)
inline

Scalar division.

Performs scalar division and returns a new vector representing the quotient.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the scalar product.

◆ operator==() [1/2]

bool svector::operator== ( const EmbVec2D lhs,
const EmbVec2D rhs 
)
inline

Compares equality of two vectors.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A boolean representing whether the two vectors compare equal.

◆ operator==() [2/2]

bool svector::operator== ( const EmbVec3D lhs,
const EmbVec3D rhs 
)
inline

Compares equality of two vectors.

Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A boolean representing whether the two vectors compare equal.

◆ rotate()

EmbVec2D svector::rotate ( const EmbVec2D vec,
const float  ang 
)
inline

Rotates vector by a certain angle.

The angle should be given in radians. The vector rotates counterclockwise when the angle is positive and clockwise when the angle is negative.

Parameters
vecA 2D vector.
angthe angle to rotate the vector, in radians.
Returns
a new, rotated vector.

◆ rotateAlpha()

EmbVec3D svector::rotateAlpha ( const EmbVec3D vec,
const float  ang 
)
inline

Rotates around x-axis.

Uses the basic gimbal-like 3D rotation matrices for rotation.

Parameters
vecA 3D vector.
angThe angle to rotate the vector, in radians.
Returns
A new, rotated vector.

◆ rotateBeta()

EmbVec3D svector::rotateBeta ( const EmbVec3D vec,
const float  ang 
)
inline

Rotates around y-axis.

Uses the basic gimbal-like 3D rotation matrices for rotation.

Parameters
vecA 3D vector.
angThe angle to rotate the vector, in radians.
Returns
A new, rotated vector.

◆ rotateGamma()

EmbVec3D svector::rotateGamma ( const EmbVec3D vec,
const float  ang 
)
inline

Rotates around z-axis.

Uses the basic gimbal-like 3D rotation matrices for rotation.

Parameters
vecA 3D vector.
angThe angle to rotate the vector, in radians.
Returns
A new, rotated vector.

◆ x() [1/4]

float svector::x ( const EmbVec2D v)
inline

Gets the x-component of a 2D vector.

Parameters
vA 2D Vector.
Returns
x-component of the vector.

◆ x() [2/4]

float svector::x ( const EmbVec3D v)
inline

Gets the x-component of a 3D vector.

Parameters
vA 3D Vector.
Returns
x-component of the vector.

◆ x() [3/4]

void svector::x ( EmbVec2D v,
const float  xValue 
)
inline

Sets the x-component of a 2D vector.

Parameters
vA 2D Vector.
xValueThe x-value to set to the vector.

◆ x() [4/4]

void svector::x ( EmbVec3D v,
const float  xValue 
)
inline

Sets the x-component of a 3D vector.

Parameters
vA 3D Vector.
xValueThe x-value to set to the vector.

◆ y() [1/4]

float svector::y ( const EmbVec2D v)
inline

Gets the y-component of a 2D vector.

Parameters
vA 2D Vector.
Returns
y-component of the vector.

◆ y() [2/4]

float svector::y ( const EmbVec3D v)
inline

Gets the y-component of a 3D vector.

Parameters
vA 3D Vector.
Returns
y-component of the vector.

◆ y() [3/4]

void svector::y ( EmbVec2D v,
const float  yValue 
)
inline

Sets the y-component of a 2D vector.

Parameters
vA 2D Vector.
yValueThe y-value to set to the vector.

◆ y() [4/4]

void svector::y ( EmbVec3D v,
const float  yValue 
)
inline

Sets the y-component of a 3D vector.

Parameters
vA 3D Vector.
yValueThe y value to set to the vector.

◆ z() [1/2]

float svector::z ( const EmbVec3D v)
inline

Gets the z-component of a 3D vector.

Parameters
vA 3D Vector.
Returns
z-component of the vector.

◆ z() [2/2]

void svector::z ( EmbVec3D v,
const float  zValue 
)
inline

Sets the z-component of a 3D vector.

Parameters
vA 3D Vector.
zValueThe z value to set to the vector.