HighMap library (C++)
Loading...
Searching...
No Matches
hmap::Vec2< T > Struct Template Reference

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.
 
Vec2operator/= (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).
 
magnitude () const
 Calculate the magnitude (length) of the vector.
 
void normalize ()
 Normalize the vector to have a magnitude of 1.
 

Public Attributes

x
 
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.
 

Detailed Description

template<typename T>
struct hmap::Vec2< T >

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.

Template Parameters
TData type for the vector components (e.g., int, float, double).

Constructor & Destructor Documentation

◆ Vec2() [1/3]

template<typename T >
hmap::Vec2< T >::Vec2 ( )
inline

Default constructor initializing the vector to (0, 0).

Initializes both x and y components to zero.

◆ Vec2() [2/3]

template<typename T >
hmap::Vec2< T >::Vec2 ( x,
y 
)
inline

Parameterized constructor initializing the vector to given values.

Parameters
xThe x component of the vector.
yThe y component of the vector.

◆ Vec2() [3/3]

template<typename T >
hmap::Vec2< T >::Vec2 ( const std::vector< T > &  vec)
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.

Template Parameters
TThe type of elements in the vector (e.g., float, int, double).
Parameters
vecA const reference to a vector of size 2, where the first element corresponds to x and the second to y.
Exceptions
std::invalid_argumentIf the vector does not contain exactly two elements.

Member Function Documentation

◆ operator==()

template<typename T >
bool hmap::Vec2< T >::operator== ( const Vec2< T > &  other_vec) const
inline

Equality operator.

Compares two vectors for equality.

Parameters
other_vecThe vector to compare with.
Returns
true if the vectors are equal, false otherwise.

◆ operator!=()

template<typename T >
bool hmap::Vec2< T >::operator!= ( const Vec2< T > &  other_vec) const
inline

Inequality operator.

Compares two vectors for inequality.

Parameters
other_vecThe vector to compare with.
Returns
true if the vectors are not equal, false otherwise.

◆ operator/=()

template<typename T >
Vec2 & hmap::Vec2< T >::operator/= ( const T  value)
inline

Division-assignment operator.

Divides both components of the vector by a scalar value and assigns the result.

Parameters
valueThe scalar value to divide by.
Returns
A reference to the current vector after division.

◆ operator/()

template<typename T >
Vec2 hmap::Vec2< T >::operator/ ( const Vec2< T > &  other_vec) const
inline

Division operator.

Divides each component of the vector by the corresponding component of another vector.

Parameters
other_vecThe vector to divide by.
Returns
A new vector that is the result of the division.

◆ operator*() [1/2]

template<typename T >
Vec2 hmap::Vec2< T >::operator* ( const Vec2< T > &  other_vec) const
inline

Multiplication operator.

Multiplies each component of the vector by the corresponding component of another vector.

Parameters
other_vecThe vector to multiply by.
Returns
A new vector that is the result of the multiplication.

◆ operator+()

template<typename T >
Vec2 hmap::Vec2< T >::operator+ ( const Vec2< T > &  other_vec) const
inline

Addition operator.

Adds each component of the vector to the corresponding component of another vector.

Parameters
other_vecThe vector to add.
Returns
A new vector that is the result of the addition.

◆ operator-()

template<typename T >
Vec2 hmap::Vec2< T >::operator- ( const Vec2< T > &  other_vec) const
inline

Subtraction operator.

Subtracts each component of another vector from the corresponding component of this vector.

Parameters
other_vecThe vector to subtract.
Returns
A new vector that is the result of the subtraction.

◆ operator*() [2/2]

template<typename T >
Vec2 hmap::Vec2< T >::operator* ( scalar) const
inline

Scalar multiplication (Vec2 * scalar).

Multiplies each component of the vector by a scalar value.

Parameters
scalarThe scalar value to multiply with.
Returns
Vec2 A new vector with each component multiplied by the scalar.

◆ magnitude()

template<typename T >
T hmap::Vec2< T >::magnitude ( ) const
inline

Calculate the magnitude (length) of the vector.

Returns
The magnitude of the vector.

◆ normalize()

template<typename T >
void hmap::Vec2< T >::normalize ( )
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.

Friends And Related Symbol Documentation

◆ operator*

template<typename T >
Vec2 operator* ( scalar,
const Vec2< T > &  vec 
)
friend

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.

Parameters
scalarThe scalar value to multiply with.
vecThe vector to multiply.
Returns
Vec2 A new vector with each component multiplied by the scalar.

◆ dot

template<typename T >
float dot ( const Vec2< T >  v1,
const Vec2< T >  v2 
)
friend

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.

Parameters
v1The first vector.
v2The second vector.
Returns
The dot product of the two vectors.

Member Data Documentation

◆ x

template<typename T >
T hmap::Vec2< T >::x

◆ y

template<typename T >
T hmap::Vec2< T >::y

The x and y components of the vector.


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