HighMap library (C++)
|
Vec2 class for basic manipulation of 2D vectors. More...
#include <algebra.hpp>
Public Member Functions | |
Vec2 () | |
Default constructor initializing the vector to (0, 0). | |
Vec2 (T x, T y) | |
Parameterized constructor initializing the vector to given values. | |
Vec2 (const std::vector< T > &vec) | |
Constructs a Vec2 object from a std::vector. | |
bool | operator== (const Vec2 &other_vec) const |
Equality operator. | |
bool | operator!= (const Vec2 &other_vec) const |
Inequality operator. | |
Vec2 & | operator/= (const T value) |
Division-assignment operator. | |
Vec2 | operator/ (const Vec2 &other_vec) const |
Division operator. | |
Vec2 | operator* (const Vec2 &other_vec) const |
Multiplication operator. | |
Vec2 | operator+ (const Vec2 &other_vec) const |
Addition operator. | |
Vec2 | operator- (const Vec2 &other_vec) const |
Subtraction operator. | |
Vec2 | operator* (T scalar) const |
Scalar multiplication (Vec2 * scalar). | |
T | magnitude () const |
Calculate the magnitude (length) of the vector. | |
void | normalize () |
Normalize the vector to have a magnitude of 1. | |
Public Attributes | |
T | x |
T | y |
The x and y components of the vector. | |
Friends | |
Vec2 | operator* (T scalar, const Vec2 &vec) |
Scalar multiplication (scalar * Vec2). | |
float | dot (const Vec2 v1, const Vec2 v2) |
Friend function to calculate the dot product of two vectors. | |
Vec2 class for basic manipulation of 2D vectors.
This class provides basic operations for 2D vectors, such as addition, subtraction, multiplication, division, and dot product calculation. The class supports various data types through templating.
T | Data type for the vector components (e.g., int, float, double). |
|
inline |
Default constructor initializing the vector to (0, 0).
Initializes both x and y components to zero.
|
inline |
Parameterized constructor initializing the vector to given values.
x | The x component of the vector. |
y | The y component of the vector. |
|
inline |
Constructs a Vec2 object from a std::vector.
This constructor takes a vector containing exactly two elements and assigns the first element to x
and the second element to y
.
T | The type of elements in the vector (e.g., float, int, double). |
vec | A const reference to a vector of size 2, where the first element corresponds to x and the second to y . |
std::invalid_argument | If the vector does not contain exactly two elements. |
|
inline |
Equality operator.
Compares two vectors for equality.
other_vec | The vector to compare with. |
|
inline |
Inequality operator.
Compares two vectors for inequality.
other_vec | The vector to compare with. |
|
inline |
Division-assignment operator.
Divides both components of the vector by a scalar value and assigns the result.
value | The scalar value to divide by. |
|
inline |
Division operator.
Divides each component of the vector by the corresponding component of another vector.
other_vec | The vector to divide by. |
|
inline |
Multiplication operator.
Multiplies each component of the vector by the corresponding component of another vector.
other_vec | The vector to multiply by. |
|
inline |
Addition operator.
Adds each component of the vector to the corresponding component of another vector.
other_vec | The vector to add. |
|
inline |
Subtraction operator.
Subtracts each component of another vector from the corresponding component of this vector.
other_vec | The vector to subtract. |
|
inline |
|
inline |
Calculate the magnitude (length) of the vector.
|
inline |
Normalize the vector to have a magnitude of 1.
This method modifies the vector in place. If the vector has zero length, the components remain unchanged to avoid division by zero.
Scalar multiplication (scalar * Vec2).
Multiplies each component of the vector by a scalar value. This function allows expressions where the scalar is on the left side of the multiplication operator.
scalar | The scalar value to multiply with. |
vec | The vector to multiply. |
Friend function to calculate the dot product of two vectors.
The dot product is the sum of the products of the corresponding components of the vectors.
v1 | The first vector. |
v2 | The second vector. |
T hmap::Vec2< T >::x |
T hmap::Vec2< T >::y |
The x and y components of the vector.