simplevectors  0.3.9
Simple vector implementations in C++
Public Member Functions | List of all members
svector::Vector2D Class Reference

A simple 2D vector representation. More...

#include <vector2d.hpp>

Inheritance diagram for svector::Vector2D:
Inheritance graph
[legend]
Collaboration diagram for svector::Vector2D:
Collaboration graph
[legend]

Public Member Functions

 Vector2D (const double x, const double y)
 Initializes a vector given xy components. More...
 
 Vector2D (const Vec2_ &other)
 Copy constructor for base class.
 
double x () const
 Gets x-component. More...
 
void x (const double &newX)
 Sets x-component. More...
 
double y () const
 Gets y-component. More...
 
void y (const double &newY)
 Sets y-component. More...
 
double angle () const
 Angle of vector. More...
 
Vector2D rotate (const double ang) const
 Rotates vector by a certain angle. More...
 
template<typename T >
componentsAs () const
 Converts vector to another object. More...
 
 Vector ()
 No-argument constructor. More...
 
 Vector (const std::initializer_list< T > args)
 Initializes a vector given initializer list. More...
 
 Vector (const Vector< D, T > &other)
 Copy constructor. More...
 
 Vector (Vector< D, T > &&) noexcept=default
 Move constructor. More...
 
- Public Member Functions inherited from svector::Vector< D, T >
 Vector ()
 No-argument constructor. More...
 
 Vector (const std::initializer_list< T > args)
 Initializes a vector given initializer list. More...
 
 Vector (const Vector< D, T > &other)
 Copy constructor. More...
 
 Vector (Vector< D, T > &&) noexcept=default
 Move constructor. More...
 
Vector< D, T > & operator= (const Vector< D, T > &other)
 Assignment operator. More...
 
Vector< D, T > & operator= (Vector< D, T > &&) noexcept=default
 Move assignment operator. More...
 
virtual ~Vector ()=default
 Destructor. More...
 
virtual std::string toString () const
 Returns string form of vector. More...
 
Vector< D, T > operator- () const
 Negative of a vector. More...
 
Vector< D, T > operator+ () const
 Positive of a vector. More...
 
Vector< D, T > & operator+= (const Vector< D, T > &other)
 In-place addition. More...
 
Vector< D, T > & operator-= (const Vector< D, T > &other)
 In-place subtraction. More...
 
Vector< D, T > & operator*= (const T other)
 In-place scalar multiplication. More...
 
Vector< D, T > & operator/= (const T other)
 In-place scalar division. More...
 
dot (const Vector< D, T > &other) const
 Dot product. More...
 
magn () const
 Magnitude. More...
 
Vector< D, T > normalize () const
 Normalizes a vector. More...
 
constexpr std::size_t numDimensions () const
 Gets the number of dimensions. More...
 
bool isZero () const
 Determines whether the current vector is a zero vector. More...
 
const T & operator[] (const std::size_t index) const
 Value of a certain component of a vector. More...
 
T & operator[] (const std::size_t index)
 Sets value of a certain component. More...
 
const T & at (const std::size_t index) const
 Value of a certain component of a vector. More...
 
T & at (const std::size_t index)
 Sets value of a certain component. More...
 
iterator begin () noexcept
 Iterator of first element. More...
 
const_iterator begin () const noexcept
 Const interator of first element. More...
 
iterator end () noexcept
 Interator of last element + 1. More...
 
const_iterator end () const noexcept
 Const interator of last element + 1. More...
 
reverse_iterator rbegin () noexcept
 Reverse iterator to last element. More...
 
const_reverse_iterator rbegin () const noexcept
 Const reverse iterator to last element. More...
 
reverse_iterator rend () noexcept
 Reverse iterator to first element - 1. More...
 
const_reverse_iterator rend () const noexcept
 Const reverse iterator to first element - 1. More...
 

Additional Inherited Members

- Public Types inherited from svector::Vector< D, T >
typedef std::array< T, D >::iterator iterator
 An std::array::iterator.
 
typedef std::array< T, D >::const_iterator const_iterator
 An std::array::const_iterator.
 
typedef std::array< T, D >::reverse_iterator reverse_iterator
 An std::array::reverse_iterator.
 
typedef std::array< T, D >::const_reverse_iterator const_reverse_iterator
 An std::array::const_reverse_iterator.
 
- Protected Attributes inherited from svector::Vector< D, T >
std::array< T, D > m_components
 An array of components for the vector.
 

Detailed Description

A simple 2D vector representation.

Constructor & Destructor Documentation

◆ Vector2D()

svector::Vector2D::Vector2D ( const double  x,
const double  y 
)
inline

Initializes a vector given xy components.

Parameters
xThe x-component.
yThe y-component.

Member Function Documentation

◆ angle()

double svector::Vector2D::angle ( ) const
inline

Angle of vector.

Gets the angle of the vector in radians.

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

Returns
The angle of the vector.

◆ componentsAs()

template<typename T >
T svector::Vector2D::componentsAs ( ) const
inline

Converts vector to another object.

Converts the components of the vector to an object with a constructor that has two parameters.

For example, this method can be used to convert the components of a 2D vector into a pair<double, double>, or a struct with two variables and a constructor for those two variables.

Returns
The converted value.

◆ rotate()

Vector2D svector::Vector2D::rotate ( const double  ang) const
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
angthe angle to rotate the vector, in radians.
Returns
A new, rotated vector.

◆ Vector() [1/4]

svector::Vector< D, T >::Vector
inline

No-argument constructor.

Initializes a zero vector (all components are 0).

◆ Vector() [2/4]

svector::Vector< D, T >::Vector
inline

Initializes a vector given initializer list.

The initializer list should represent the components of the vector in each dimension. If the size of the initializer list is greater than the number of dimensions given, then the vector only initializes with the first D elements in the list, where D is the number of dimensions. If the size of the initializer list is less than the number of dimensions given, then the vector fills the rest of the dimensions with the value 0.

Parameters
argsthe initializer list.

◆ Vector() [3/4]

svector::Vector< D, T >::Vector
inline

Copy constructor.

Copies from another vector to an uninitialized vector.

◆ Vector() [4/4]

svector::Vector< D, T >::Vector
defaultnoexcept

Move constructor.

Uses C++ default move constructor.

◆ x() [1/2]

double svector::Vector2D::x ( ) const
inline

Gets x-component.

Gets the x-component of the vector.

Returns
x-component of vector.

◆ x() [2/2]

void svector::Vector2D::x ( const double &  newX)
inline

Sets x-component.

Sets the x-component of the vector.

Parameters
newXx-value to set

◆ y() [1/2]

double svector::Vector2D::y ( ) const
inline

Gets y-component.

Gets the y-component of the vector.

Returns
y-component of vector.

◆ y() [2/2]

void svector::Vector2D::y ( const double &  newY)
inline

Sets y-component.

Sets the y-component of the vector.

Parameters
newYy-value to set

The documentation for this class was generated from the following file: