Template Class vector2

Nested Relationships

Nested Types

Inheritance Relationships

Base Type

Class Documentation

template<typename T>
class vector2 : public rcppsw::er::stringizable

Base template class encapsulating mathematical actions on a pair of numbers. Is specialized by vector2i, vector2d.

All operations are performed in whatever the template parameter is, so take care if you are trying to do scaling, trigonometric things with integers…

Public Types

using value_type = T

Public Functions

template<typename U = T, typename std::enable_if<std::is_floating_point<U>::value, int>::type = 0>
inline vector2(const T &length, const radians &angle) noexcept

Initializes the vector coordinates from polar coordinates.

Parameters:
  • length – The vector length.

  • angle – The vector angle.

inline constexpr vector2(const T &x, const T &y) noexcept

Initializes the vector from Cartesian coordinates.

Parameters:
  • x – The X coordinate.

  • y – The Y coordinate.

vector2(void) noexcept = default

Initializes vector to (0,0)

inline radians angle(void) const

Return the angle of this vector.

inline bool is_pd(void) const

Is the vector is positive definite?

inline bool is_psd(void) const

Is the vector is positive semi-definite?

inline double length(void) const

Returns the length of this vector.

template<typename U = T, typename std::enable_if<std::is_floating_point<U>::value, int>::type = 0>
inline vector2 &normalize(void)

Normalizes this vector.

After this method is called, the vector has length 1. If the vector is (0,0), this call results in a division by zero error, and your program will probably crash.

Returns:

A reference to the normalized vector.

inline bool operator!=(const vector2 &other) const

Returns if this vector and the passed one are not equal by checking coordinates for equality.

Should generally not be called if the template parameter type is not an integer, as floating point comparisons in general are unsafe.

inline vector2 operator*(T val) const
inline vector2 &operator*=(T val)
inline vector2 operator+(const vector2 &other) const
inline vector2 &operator+=(const vector2 &other)
inline vector2<T> operator-(const vector2 &other) const
inline vector2 operator-(void) const
inline vector2 &operator-=(const vector2 &other)
inline vector2 operator/(T val) const
inline vector2 &operator/=(T val)
template<typename U = T, typename std::enable_if<!std::is_floating_point<U>::value, int>::type = 0>
inline bool operator<(const vector2 &other) const

Needed for using vectors as keys in a map.

inline bool operator<=(const vector2 &other) const
template<typename U = T, typename std::enable_if<!std::is_floating_point<U>::value, int>::type = 0>
inline bool operator==(const vector2 &other) const

Returns if this vector and the argument are considered equal, determined by coordinate comparison.

Only available if the template argument is not floating point.

template<typename U = T, typename std::enable_if<std::is_floating_point<U>::value, int>::type = 0>
inline bool operator==(const vector2 &other) const

Returns if this vector and the argument are considered equal, determined by coordinate comparison.

Only available if the template argument is floating point.

template<typename U = T, typename std::enable_if<!std::is_floating_point<U>::value, int>::type = 0>
inline bool operator>(const vector2 &other) const
inline bool operator>=(const vector2 &other) const
template<typename U = T, typename std::enable_if<std::is_floating_point<U>::value, int>::type = 0>
inline vector2 &rotate(const radians &angle)

Rotate this vector by the specified angle.

This is only available if the template parameter is not an integer.

Parameters:

angle – The rotation angle.

Returns:

A reference to the rotated vector.

inline vector2 &scale(const T &factor)

Scales the vector by the specified values.

Parameters:

factor – The scaling factor applied to both X and Y.

Returns:

A reference to the scaled vector.

inline vector2 &scale(const T &scale_x, const T &scale_y)

Scales the vector by the specified values.

Parameters:
  • scale_x – the scale factor for the X coordinate.

  • scale_y – the scale factor for the Y coordinate.

Returns:

A reference to the scaled vector.

inline void set(const T &x, const T &y)

Sets the vector contents from Cartesian coordinates.

Parameters:
  • x – The new X coordinate.

  • y – The new Y coordinate.

template<typename U = T, typename std::enable_if<std::is_floating_point<U>::value, int>::type = 0>
inline void set_from_polar(const T &length, const radians &angle)

Sets the vector contents from polar coordinates.

Parameters:
  • length – The length of the vector.

  • angle – The angle of the vector (range [0,2pi)

inline T square_length(void) const

Returns the square length of this vector.

inline virtual std::string to_str(void) const override

Return a string representation of a class.

inline void x(const T &x)
inline T x(void)
inline const T &x(void) const
inline void y(const T &y)
inline T y(void)
inline const T &y(void) const

Public Static Functions

template<typename U, typename V, typename std::enable_if<!std::is_floating_point<U>::value && !std::is_floating_point<V>::value, int>::type = 0>
static inline size_t l1norm(const vector2<U> &v1, const vector2<V> &v2)

Computes the manhattan distance between the passed vectors.

Public Static Attributes

static constexpr size_t kDIMENSIONALITY = 2
static const vector2<T> X

The positive X axis.

static const vector2<T> Y

The positive Y axis.

Friends

inline friend std::ostream &operator<<(std::ostream &stream, const vector2 &v)
inline friend std::istream &operator>>(std::istream &is, vector2<T> &v)

For parsing a vector from a string in the form of "X,Y".

struct componentwise_compare

Needed to compare in mathematical contexts.

Public Functions

inline bool operator()(const vector2<T> &lhs, const vector2<T> &rhs) const
struct key_compare

Needed for using vectors as keys in a map.

Public Functions

template<typename U = T, typename std::enable_if<!std::is_floating_point<U>::value, int>::type = 0>
inline bool operator()(const vector2<U> &lhs, const vector2<U> &rhs) const
template<typename U = T, typename std::enable_if<std::is_floating_point<U>::value, int>::type = 0>
inline bool operator()(const vector2<U> &lhs, const vector2<U> &rhs) const