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

A minimized version of vectors for embedded devices with access to the STL. More...

#include <cmath>
#include <string>
Include dependency graph for embed.hpp:

Go to the source code of this file.

Classes

struct  svector::Vec2D
 A minimal 2D vector representation. More...
 
struct  svector::Vec3D
 A minimal 3D vector representation. More...
 

Functions

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

Detailed Description

A minimized version of vectors for embedded devices with access to the STL.

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

Function Documentation

◆ alpha()

double svector::alpha ( const Vec3D 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()

double svector::angle ( const Vec2D 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()

double svector::beta ( const Vec3D 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()

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

Cross product of two vectors.

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

◆ dot() [1/2]

double svector::dot ( const Vec2D lhs,
const Vec2D 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]

double svector::dot ( const Vec3D lhs,
const Vec3D rhs 
)
inline

Calculates the dot product of two vectors.

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

◆ gamma()

double svector::gamma ( const Vec3D 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 Vec2D 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 Vec3D vec)
inline

Determines whether a vector is a zero vector.

Returns
Whether the given vector is a zero vector.

◆ magn() [1/2]

double svector::magn ( const Vec2D vec)
inline

Gets the magnitude of the vector.

Parameters
vecA 2D vector.
Returns
magnitude of vector.

◆ magn() [2/2]

double svector::magn ( const Vec3D vec)
inline

Gets the magnitude of the vector.

Parameters
vecA 3D vector.
Returns
magnitude of vector.

◆ normalize() [1/2]

Vec2D svector::normalize ( const Vec2D 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]

Vec3D svector::normalize ( const Vec3D 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 Vec2D lhs,
const Vec2D 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 Vec3D lhs,
const Vec3D 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]

Vec2D svector::operator* ( const Vec2D lhs,
const double  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]

Vec3D svector::operator* ( const Vec3D lhs,
const double  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]

Vec2D svector::operator+ ( const Vec2D lhs,
const Vec2D 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]

Vec2D svector::operator+ ( const Vec2D 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]

Vec3D svector::operator+ ( const Vec3D lhs,
const Vec3D 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]

Vec3D svector::operator+ ( const Vec3D 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]

Vec2D svector::operator- ( const Vec2D lhs,
const Vec2D 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]

Vec2D svector::operator- ( const Vec2D 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 2D vector.
Returns
A new vector representing the negative of the given vector.

◆ operator-() [3/4]

Vec3D svector::operator- ( const Vec3D lhs,
const Vec3D 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]

Vec3D svector::operator- ( const Vec3D 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]

Vec2D svector::operator/ ( const Vec2D lhs,
const double  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]

Vec3D svector::operator/ ( const Vec3D lhs,
const double  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 Vec2D lhs,
const Vec2D 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 Vec3D lhs,
const Vec3D 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()

Vec2D svector::rotate ( const Vec2D vec,
const double  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()

Vec3D svector::rotateAlpha ( const Vec3D vec,
const double  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()

Vec3D svector::rotateBeta ( const Vec3D vec,
const double  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()

Vec3D svector::rotateGamma ( const Vec3D vec,
const double  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.

◆ toString() [1/2]

std::string svector::toString ( const Vec2D vec)
inline

String form.

This can be used for printing.

Parameters
vecA 2D Vector.
Returns
string form of vector.

◆ toString() [2/2]

std::string svector::toString ( const Vec3D vec)
inline

String form.

This can be used for printing.

Parameters
vecA 3D Vector.
Returns
string form of vector.

◆ x() [1/4]

double svector::x ( const Vec2D v)
inline

Gets the x-component of a 2D vector.

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

◆ x() [2/4]

double svector::x ( const Vec3D 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 ( Vec2D v,
const double  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 ( Vec3D v,
const double  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]

double svector::y ( const Vec2D v)
inline

Gets the y-component of a 2D vector.

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

◆ y() [2/4]

double svector::y ( const Vec3D 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 ( Vec2D v,
const double  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 ( Vec3D v,
const double  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]

double svector::z ( const Vec3D 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 ( Vec3D v,
const double  zValue 
)
inline

Sets the z-component of a 3D vector.

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