Template class for working with 2D vectors.
Besides operators defined in the template and standalone ones described below the template description there are several additional useful operators for working with vectors and matrices togather. You can find them at TMat2 matrix template description page.
You can find more information in comments below.
template<class TYPE> union TVec2
{
struct
{
TYPE X;
TYPE Y;
};
TYPE Data[2];
TVec2() = default;
TVec2(const TVec2 &other) = default;
TVec2(TVec2 &&other) = default;
TVec2(const TYPE x, const TYPE y); // Create with specified values
TVec2(const TVec3<TYPE> &v); // Create from 3D vector
TVec2(const TVec4<TYPE> &v); // Create from 4D vector
const TYPE &operator[](const size_t index) const; // Return component value constant reference by index
TYPE &operator[](const size_t index); // Return component value reference by index
bool operator==(const TVec2 &other) const; // Strict comparison with other vector for equality
bool operator!=(const TVec2 &other) const; // Strict comparison with other vector for inequality
TVec2 &operator=(const TVec2 &other) = default;
TVec2 &operator=(TVec2 &&other) = default;
operator TVec3<TYPE>() const; // Convert to 3D vector
operator TVec4<TYPE>() const; // Convert to 4D vector(quaternion)
TVec2 operator+(const TYPE value) const; // Add scalar value to each component and return new vector
TVec2 operator-(const TYPE value) const; // Subtract scalar value from each component and return new vector
TVec2 operator*(const TYPE value) const; // Multiply each component by scalar value and return new vector
TVec2 operator/(const TYPE value) const; // Divide each component by scalar value and return new vector
TVec2 &operator+=(const TYPE value); // Add scalar value to each component of this vector
TVec2 &operator-=(const TYPE value); // Subtract scalar value from each component of this vector
TVec2 &operator*=(const TYPE value); // Multiply each component of this vector by scalar value
TVec2 &operator/=(const TYPE value); // Divide each component of this vector by scalar value
TVec2 operator+(const TVec2 &other) const; // Add each components of this and another vectors and return new vector
TVec2 operator-(const TVec2 &other) const; // Subtract each components of this and another vectors and return new vector
TVec2 operator*(const TVec2 &other) const; // Multiply each components of this and another vectors and return new vector
TVec2 operator/(const TVec2 &other) const; // Divide each components of this and another vectors and return new vector
TVec2 &operator+=(const TVec2 &other); // Add each components of another vector to corresponding component of this one
TVec2 &operator-=(const TVec2 &other); // Subtract each components of another vector from corresponding component of this one
TVec2 &operator*=(const TVec2 &other); // Multiply each components of another vector to corresponding component of this one
TVec2 &operator/=(const TVec2 &other); // Divide each components of this vector by corresponding component of another one
const TYPE *data() const; // Return pointer to components array
bool is_equal(const TVec2 &other, const TYPE tolerance) const; // Soft comparison for equality with another vector with specified tolerance
bool is_not_equal(const TVec2 &other, const TYPE tolerance) const; // Soft comparison for inequality with another vector with specified tolerance
TYPE norm() const; // Return norm/length
TYPE norm2() const; // Return squared norm/length
TYPE distance(const TVec2 &other) cons;t // Return distance to point
TYPE distance2(const TVec2 &other) const; // Return squared distance to point
TYPE dot(const TVec2 &other) const; // Return dot product
void normalize(); // Normalize(make nor/length equal to 1) this vector
TVec2 normalized() const; // Return normalized equivalent of this vector
};
Additionally following operators exists and can be used for arithmetic operations with scalar value and 2D vectors. So as a final result you may use the 2D vector template with scalar values in both directions(scalar * vector
and vector * scalar
). Pay attention on the - and / operators. The result of operation like scalar - vector
will be different from the result of operation like vector - scalar
. For example, 5 - { 1, 2 }
will give you vector { 4, 3 }(which is { 5 - 1, 5 - 2 }) and { 1, 2 } - 5
will give you vector { -4, -3 }(which is { 1 - 5, 2 - 5 }).
template<class TYPE> TVec2<TYPE> operator+(const TYPE value, const TVec2<TYPE> &v);
template<class TYPE> TVec2<TYPE> operator-(const TYPE value, const TVec2<TYPE> &v);
template<class TYPE> TVec2<TYPE> operator*(const TYPE value, const TVec2<TYPE> &v);
template<class TYPE> TVec2<TYPE> operator/(const TYPE value, const TVec2<TYPE> &v);
Namespace: | nitisa::math |
Include: | Nitisa/Modules/Math/Vector.h |