ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
asw::Vec2< T > Class Template Reference

A 2D vector in space. More...

#include <geometry.h>

Public Types

using Real = std::conditional_t<std::is_floating_point_v<T>, T, float>
 Type used for lengths and angles. Integer vectors use float so results are not truncated.
 

Public Member Functions

 Vec2 ()=default
 Default constructor for the Vec2 class.
 
 Vec2 (T x, T y)
 Constructor for the Vec2 class.
 
Real angle (const Vec2 &other) const
 Get angle of two vectors.
 
Real angle () const
 Calculate the angle of the vector.
 
Real distance (const Vec2 &other) const
 Get distance between two vectors.
 
Real distance_to_segment (const Vec2 &start, const Vec2 &end) const
 Get the distance from this point to the nearest point on a line segment. Useful for beams, lasers and swept hit checks.
 
T dot (const Vec2 &other) const
 Calculate the dot product of two vectors.
 
T cross (const Vec2 &other) const
 Calculate the cross product of two vectors.
 
Real magnitude () const
 Calculate the magnitude of the vector.
 
Vec2 normalized () const
 Get a vector with the same direction and a length of one.
 
Vec2 operator+ (const Vec2 &other) const
 Addition operator for the Vec2 class.
 
Vec2 operator- (const Vec2 &other) const
 Subtraction operator for the Vec2 class.
 
Vec2 operator* (const T scalar) const
 Multiplication operator for the Vec2 class.
 
Vec2 operator/ (const T scalar) const
 Division operator for the Vec2 class.
 
Vec2 operator% (const T scalar) const
 Modulo the Vec2 object by a scalar.
 
Vec2 & operator+= (const Vec2 &other)
 Addition assignment operator for the Vec2 class.
 
Vec2 & operator-= (const Vec2 &other)
 Subtraction assignment operator for the Vec2 class.
 
Vec2 & operator*= (const T scalar)
 Multiplication assignment operator for the Vec2 class.
 
Vec2 & operator/= (const T scalar)
 Division assignment operator for the Vec2 class.
 
Vec2 & operator%= (const T scalar)
 Modulo assignment operator for the Vec2 class.
 
bool operator== (const Vec2 &other) const =default
 Equality operator for the Vec2 class.
 

Static Public Member Functions

static Vec2 from_angle (T angle, T length=T(1))
 Create a vector pointing at an angle.
 

Public Attributes

T x { 0 }
 The x component of the vector.
 
T y { 0 }
 The y component of the vector.
 

Detailed Description

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

A 2D vector in space.

This class represents a 2D vector in space. It is used to represent points, directions, and sizes.

Definition at line 27 of file geometry.h.

Member Typedef Documentation

◆ Real

template<typename T >
using asw::Vec2< T >::Real = std::conditional_t<std::is_floating_point_v<T>, T, float>

Type used for lengths and angles. Integer vectors use float so results are not truncated.

Definition at line 31 of file geometry.h.

Constructor & Destructor Documentation

◆ Vec2() [1/2]

template<typename T >
asw::Vec2< T >::Vec2 ( )
default

Default constructor for the Vec2 class.

◆ Vec2() [2/2]

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

Constructor for the Vec2 class.

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

Definition at line 42 of file geometry.h.

Member Function Documentation

◆ angle() [1/2]

template<typename T >
Real asw::Vec2< T >::angle ( ) const
inline

Calculate the angle of the vector.

Returns
T The angle of the vector in radians.

Definition at line 61 of file geometry.h.

◆ angle() [2/2]

template<typename T >
Real asw::Vec2< T >::angle ( const Vec2< T > & other) const
inline

Get angle of two vectors.

Returns
The angle of the vector in radians.

Definition at line 52 of file geometry.h.

◆ cross()

template<typename T >
T asw::Vec2< T >::cross ( const Vec2< T > & other) const
inline

Calculate the cross product of two vectors.

Parameters
otherThe vector to cross with.
Returns
T The cross product of the vectors.

Definition at line 129 of file geometry.h.

◆ distance()

template<typename T >
Real asw::Vec2< T >::distance ( const Vec2< T > & other) const
inline

Get distance between two vectors.

Returns
The distance between the vectors.

Definition at line 86 of file geometry.h.

◆ distance_to_segment()

template<typename T >
Real asw::Vec2< T >::distance_to_segment ( const Vec2< T > & start,
const Vec2< T > & end ) const
inline

Get the distance from this point to the nearest point on a line segment. Useful for beams, lasers and swept hit checks.

Parameters
startOne end of the segment.
endThe other end of the segment.
Returns
The distance, or the distance to start if both ends are the same point.

Definition at line 99 of file geometry.h.

◆ dot()

template<typename T >
T asw::Vec2< T >::dot ( const Vec2< T > & other) const
inline

Calculate the dot product of two vectors.

Parameters
otherThe vector to dot with.
Returns
T The dot product of the vectors.

Definition at line 119 of file geometry.h.

◆ from_angle()

template<typename T >
static Vec2 asw::Vec2< T >::from_angle ( T angle,
T length = T(1) )
inlinestatic

Create a vector pointing at an angle.

Parameters
angleThe angle in radians, clockwise from the positive x axis like the rest of asw.
lengthThe length of the vector.
Returns
Vec2 The vector.

Definition at line 76 of file geometry.h.

◆ magnitude()

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

Calculate the magnitude of the vector.

Returns
T The magnitude of the vector.

Definition at line 138 of file geometry.h.

◆ normalized()

template<typename T >
Vec2 asw::Vec2< T >::normalized ( ) const
inline

Get a vector with the same direction and a length of one.

Returns
Vec2 The unit vector, or a zero vector if this vector has no length.

Definition at line 148 of file geometry.h.

◆ operator%()

template<typename T >
Vec2 asw::Vec2< T >::operator% ( const T scalar) const
inline

Modulo the Vec2 object by a scalar.

Parameters
scalarThe scalar to modulo by
Returns
Vec2 The result of the modulo operation.

Definition at line 204 of file geometry.h.

◆ operator%=()

template<typename T >
Vec2 & asw::Vec2< T >::operator%= ( const T scalar)
inline

Modulo assignment operator for the Vec2 class.

Parameters
scalarThe scalar to modulo by.
Returns
Vec2& The result of the modulo operation.

Definition at line 262 of file geometry.h.

◆ operator*()

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

Multiplication operator for the Vec2 class.

Parameters
scalarThe scalar to multiply by.
Returns
Vec2 The scaled vector.

Definition at line 184 of file geometry.h.

◆ operator*=()

template<typename T >
Vec2 & asw::Vec2< T >::operator*= ( const T scalar)
inline

Multiplication assignment operator for the Vec2 class.

Parameters
scalarThe scalar to multiply by.
Returns
Vec2& The scaled vector.

Definition at line 238 of file geometry.h.

◆ operator+()

template<typename T >
Vec2 asw::Vec2< T >::operator+ ( const Vec2< T > & other) const
inline

Addition operator for the Vec2 class.

Parameters
otherThe vector to add.
Returns
Vec2 The sum of the vectors.

Definition at line 164 of file geometry.h.

◆ operator+=()

template<typename T >
Vec2 & asw::Vec2< T >::operator+= ( const Vec2< T > & other)
inline

Addition assignment operator for the Vec2 class.

Parameters
otherThe vector to add.
Returns
Vec2& The sum of the vectors.

Definition at line 214 of file geometry.h.

◆ operator-()

template<typename T >
Vec2 asw::Vec2< T >::operator- ( const Vec2< T > & other) const
inline

Subtraction operator for the Vec2 class.

Parameters
otherThe vector to subtract.
Returns
Vec2 The difference of the vectors.

Definition at line 174 of file geometry.h.

◆ operator-=()

template<typename T >
Vec2 & asw::Vec2< T >::operator-= ( const Vec2< T > & other)
inline

Subtraction assignment operator for the Vec2 class.

Parameters
otherThe vector to subtract.
Returns
Vec2& The difference of the vectors.

Definition at line 226 of file geometry.h.

◆ operator/()

template<typename T >
Vec2 asw::Vec2< T >::operator/ ( const T scalar) const
inline

Division operator for the Vec2 class.

Parameters
scalarThe scalar to divide by.
Returns
Vec2 The scaled vector.

Definition at line 194 of file geometry.h.

◆ operator/=()

template<typename T >
Vec2 & asw::Vec2< T >::operator/= ( const T scalar)
inline

Division assignment operator for the Vec2 class.

Parameters
scalarThe scalar to divide by.
Returns
Vec2& The scaled vector.

Definition at line 250 of file geometry.h.

◆ operator==()

template<typename T >
bool asw::Vec2< T >::operator== ( const Vec2< T > & other) const
default

Equality operator for the Vec2 class.

Parameters
otherThe vector to compare.
Returns
bool True if the vectors are equal.

Member Data Documentation

◆ x

template<typename T >
T asw::Vec2< T >::x { 0 }

The x component of the vector.

Definition at line 277 of file geometry.h.

◆ y

template<typename T >
T asw::Vec2< T >::y { 0 }

The y component of the vector.

Definition at line 280 of file geometry.h.


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