Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::Vector2< T > Struct Template Reference

A 2D vector, (x, y). More...

#include "donner/base/Vector2.h"

Collaboration diagram for donner::Vector2< T >:
[legend]

Public Member Functions

 Vector2 ()=default
 Default constructor for a zero vector.
 
constexpr Vector2 (T x, T y)
 Construct a vector from a given x and y component.
 
 ~Vector2 ()=default
 Destructor.
 
 Vector2 (const Vector2< T > &other)=default
 Copy constructor.
 
Vector2< T > & operator= (const Vector2< T > &other)=default
 Copy assignment operator.
 
 Vector2 (Vector2< T > &&other) noexcept=default
 Move constructor.
 
Vector2< T > & operator= (Vector2< T > &&other) noexcept=default
 Move assignment operator.
 
template<typename S >
Vector2< T > & operator= (const Vector2< S > &rhs)
 Assignment operator from a vector of a different type.
 
template<typename S >
 Vector2 (const Vector2< S > &other)
 Construct a vector from a vector of a different type, by implicitly casting each component.
 
template<typename S >
 Vector2 (S x, S y)
 Construct a vector from components of a different type, by implicitly casting each component.
 
length () const
 Returns the length of the vector.
 
lengthSquared () const
 Returns the squared length of the vector.
 
distance (const Vector2< T > &other) const
 Returns the distance between two vectors, assuming that each represents a point in space.
 
distanceSquared (const Vector2< T > &other) const
 Returns the squared distance between two vectors, assuming that each represents a point in space.
 
dot (const Vector2< T > &other) const
 Returns the dot product of this vector and other.
 
cross (const Vector2< T > &other) const
 Returns the Z component of the cross product of this vecctor and other in the 2D plane.
 
Vector2< T > rotate (double radians) const
 Rotate this vector by radians.
 
Vector2< T > rotate (T cosResult, T sinResult) const
 Rotate this vector by a given pre-computed cosine/sine angle.
 
angle () const
 Returns the angle that this vector makes with the +x axis, in radians.
 
angleWith (const Vector2< T > &other) const
 Returns the angle between this vector and the provided vector, or zero if one of the vectors has zero length.
 
Vector2< T > normalize () const
 Returns the normalized form of this vector.
 
Vector2< T > operator- () const
 Unary negation.
 
Vector2< T > operator+ (const Vector2< T > &rhs) const
 Addition operator, add two vectors and return the result.
 
Vector2< T > & operator+= (const Vector2< T > &rhs)
 Addition assignment operator, add rhs to this vector.
 
Vector2< T > operator- (const Vector2< T > &rhs) const
 Subtraction operator, subtract two vectors and return the result.
 
Vector2< T > & operator-= (const Vector2< T > &rhs)
 Subtraction assignment operator, subtract rhs from this vector.
 
Vector2< T > operator* (const Vector2< T > &rhs) const
 Piecewise multiplication operator, multiply the x and y components of two vectors and return the result.
 
Vector2< T > & operator*= (const Vector2< T > &rhs)
 Piecewise multiplication assignment operator, multiply the x and y components of this vector by the x and y components of rhs.
 
Vector2< T > operator* (const T a) const
 Scalar multiplication operator, multiply the x and y components of this vector by a and return the result.
 
Vector2< T > & operator*= (const T a)
 Scalar multiplication assignment operator, multiply the x and y components of this vector by a.
 
Vector2< T > operator/ (const Vector2< T > &rhs) const
 Piecewise division operator, divide the x and y components of two vectors and return the result.
 
Vector2< T > & operator/= (const Vector2< T > &rhs)
 Piecewise division assignment operator, divide the x and y components of this vector by the x and y components of rhs.
 
Vector2< T > operator/ (const T a) const
 Scalar division operator, divide the x and y components of this vector by a and return the result.
 
Vector2< T > & operator/= (const T a)
 Scalar division assignment operator, divide the x and y components of this vector by a.
 
bool operator== (const Vector2< T > &other) const
 Equality operator, check if two vectors are equal.
 
bool operator!= (const Vector2< T > &rhs) const
 Inequality operator, check if two vectors are not equal.
 
template<typename U >
bool operator== (const Vector2< U > &other) const
 Equality operator with a vector of a different type, check if two vectors are equal.
 
template<typename U >
bool operator!= (const Vector2< U > &rhs) const
 Inequality operator with a vector of a different type, check if two vectors are not equal.
 

Static Public Member Functions

static Vector2< T > Zero ()
 Returns a vector with all components set to zero.
 
static Vector2< T > XAxis ()
 Returns a vector with for the x-axis, i.e. (1, 0).
 
static Vector2< T > YAxis ()
 Returns a vector with for the y-axis, i.e. (0, 1).
 

Public Attributes

x = T(0)
 The x component of the vector.
 
y = T(0)
 The y component of the vector.
 

Friends

Vector2< T > operator* (const T a, const Vector2< T > &other)
 Reversed scalar multiplication operator, multiply the x and y components of this vector by a and return the result.
 
Vector2< T > operator/ (const T a, const Vector2< T > &other)
 Reversed scalar division operator, divide the x and y components of this vector by a and return the result.
 
std::ostream & operator<< (std::ostream &os, const Vector2< T > &vec)
 Ostream output operator.
 

Detailed Description

template<typename T>
struct donner::Vector2< T >

A 2D vector, (x, y).

Template Parameters
TThe type of the vector's components.

Constructor & Destructor Documentation

◆ Vector2() [1/2]

template<typename T >
template<typename S >
donner::Vector2< T >::Vector2 ( const Vector2< S > & other)
inline

Construct a vector from a vector of a different type, by implicitly casting each component.

Template Parameters
SThe type of the other vector's components.
Parameters
otherThe other vector.

◆ Vector2() [2/2]

template<typename T >
template<typename S >
donner::Vector2< T >::Vector2 ( S x,
S y )
inline

Construct a vector from components of a different type, by implicitly casting each component.

Template Parameters
SThe type of the components.
Parameters
xThe x component.
yThe y component.

Member Function Documentation

◆ angle()

template<typename T >
T donner::Vector2< T >::angle ( ) const
inline

Returns the angle that this vector makes with the +x axis, in radians.

Returns
Angle in the range of [-π, π].

◆ angleWith()

template<typename T >
T donner::Vector2< T >::angleWith ( const Vector2< T > & other) const
inline

Returns the angle between this vector and the provided vector, or zero if one of the vectors has zero length.

Parameters
otherOther vector.
Returns
Angle in the range of [0, π].

◆ cross()

template<typename T >
T donner::Vector2< T >::cross ( const Vector2< T > & other) const
inline

Returns the Z component of the cross product of this vecctor and other in the 2D plane.

The result always points perpendicular outside the plane.

Parameters
otherThe other vector.

◆ distance()

template<typename T >
T donner::Vector2< T >::distance ( const Vector2< T > & other) const
inline

Returns the distance between two vectors, assuming that each represents a point in space.

Parameters
otherThe other vector.

◆ distanceSquared()

template<typename T >
T donner::Vector2< T >::distanceSquared ( const Vector2< T > & other) const
inline

Returns the squared distance between two vectors, assuming that each represents a point in space.

Parameters
otherThe other vector.

◆ dot()

template<typename T >
T donner::Vector2< T >::dot ( const Vector2< T > & other) const
inline

Returns the dot product of this vector and other.

Parameters
otherThe other vector.

◆ operator=()

template<typename T >
template<typename S >
Vector2< T > & donner::Vector2< T >::operator= ( const Vector2< S > & rhs)
inline

Assignment operator from a vector of a different type.

Template Parameters
SThe type of the other vector's components.
Parameters
rhsThe vector to copy.

◆ rotate() [1/2]

template<typename T >
Vector2< T > donner::Vector2< T >::rotate ( double radians) const
inline

Rotate this vector by radians.

Parameters
radiansAngle in radians.

◆ rotate() [2/2]

template<typename T >
Vector2< T > donner::Vector2< T >::rotate ( T cosResult,
T sinResult ) const
inline

Rotate this vector by a given pre-computed cosine/sine angle.

Parameters
cosResultThe result of cos(angle)
sinResultThe result of sin(angle)

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