Loading...
Searching...
No Matches
yoda::Vec2< T > Class Template Reference

A 2D vector with common geometric operations. More...

#include <yoda.hpp>

Collaboration diagram for yoda::Vec2< T >:
[legend]

Public Member Functions

 Vec2 (T x=T{}, T y=T{})
 Construct a new Vec2.
 
dot (const Vec2 &other) const
 Compute the dot product of two vectors.
 
double magnitude () const
 Compute the Euclidean magnitude.
 
Vec2< double > normalized () const
 Return a normalized (unit-length) copy of this vector.
 
Vec2 operator* (T scalar) const
 Scalar multiplication.
 
Vec2 operator+ (const Vec2 &rhs) const
 Vector addition.
 

Public Attributes

x
 X component.
 
y
 Y component.
 

Detailed Description

template<typename T>
class yoda::Vec2< T >

A 2D vector with common geometric operations.

Demonstrates template classes, operator overloading, and mathematical documentation with MathJax.

The magnitude of a vector \((x, y)\) is computed as:

\[ \| \mathbf{v} \| = \sqrt{x^2 + y^2} \]

Template Parameters
TNumeric type (must support arithmetic operators).
Warning
Normalization of a zero-length vector is undefined behavior.

Definition at line 82 of file yoda.hpp.

Constructor & Destructor Documentation

◆ Vec2()

template<typename T>
yoda::Vec2< T >::Vec2 ( T x = T{},
T y = T{} )
inline

Construct a new Vec2.

Parameters
xThe x component (default: 0).
yThe y component (default: 0).

Definition at line 92 of file yoda.hpp.

92{}, T y = T{}) : x(x), y(y) {}
A 2D vector with common geometric operations.
Definition yoda.hpp:82
T y
Y component.
Definition yoda.hpp:85
T x
X component.
Definition yoda.hpp:84

Member Function Documentation

◆ dot()

template<typename T>
T yoda::Vec2< T >::dot ( const Vec2< T > & other) const
inlinenodiscard

Compute the dot product of two vectors.

The dot product is defined as:

\[ \mathbf{a} \cdot \mathbf{b} = a_x b_x + a_y b_y \]

Parameters
otherThe other vector.
Returns
The scalar dot product.

Definition at line 127 of file yoda.hpp.

127 {
128 return x * other.x + y * other.y;
129 }

◆ magnitude()

template<typename T>
double yoda::Vec2< T >::magnitude ( ) const
inlinenodiscard

Compute the Euclidean magnitude.

Returns
The length of the vector.
Precondition
The type T must be convertible to double.

Definition at line 100 of file yoda.hpp.

100 {
101 return std::sqrt(static_cast<double>(x * x + y * y));
102 }

Referenced by yoda::Vec2< double >::normalized().

◆ normalized()

template<typename T>
Vec2< double > yoda::Vec2< T >::normalized ( ) const
inlinenodiscard

Return a normalized (unit-length) copy of this vector.

Attention
The result is undefined if magnitude() == 0.
Returns
A new Vec2 with unit length.

Definition at line 111 of file yoda.hpp.

111 {
112 double m = magnitude();
113 return {static_cast<double>(x) / m, static_cast<double>(y) / m};
114 }
double magnitude() const
Compute the Euclidean magnitude.
Definition yoda.hpp:100

◆ operator*()

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

Scalar multiplication.

Parameters
scalarThe scalar multiplier.
Returns
The scaled vector.

Definition at line 145 of file yoda.hpp.

145 {
146 return {x * scalar, y * scalar};
147 }

◆ operator+()

template<typename T>
Vec2 yoda::Vec2< T >::operator+ ( const Vec2< T > & rhs) const
inline

Vector addition.

Parameters
rhsRight-hand side operand.
Returns
The component-wise sum.

Definition at line 136 of file yoda.hpp.

136 {
137 return {x + rhs.x, y + rhs.y};
138 }

Member Data Documentation

◆ x

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

X component.

Definition at line 84 of file yoda.hpp.

Referenced by yoda::Vec2< double >::dot(), and yoda::Vec2< double >::operator+().

◆ y

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

Y component.

Definition at line 85 of file yoda.hpp.

Referenced by yoda::Vec2< double >::dot(), and yoda::Vec2< double >::operator+().


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