PlayRho  2.0.0
An interactive physics engine & library.
playrho::d2::UnitVec Class Reference

2-D unit vector. More...

#include <playrho/d2/UnitVec.hpp>

Public Types

using checker_type = detail::UnitMagnitudeChecker< underlying_type >
 Alias for checker type this class template was instantiated for.
 
using const_iterator = underlying_type::const_iterator
 Constant iterator type.
 
using const_pointer = underlying_type::const_pointer
 Constant pointer type.
 
using const_reference = underlying_type::const_reference
 Constant reference type.
 
using const_reverse_iterator = underlying_type::const_reverse_iterator
 Constant reverse iterator type.
 
using exception_type = InvalidArgument
 Alias for the exception type possibly thrown by this class.
 
template<typename T >
using PolarCoord = std::enable_if_t< IsArithmeticV< T >, std::pair< UnitVec, T > >
 Polar coordinate. More...
 
using size_type = underlying_type::size_type
 Size type.
 
using underlying_type = Vector< value_type, N >
 Alias for the underlying type of this object.
 
using value_type = Real
 Value type used for the coordinate values of this vector.
 

Public Member Functions

constexpr UnitVec () noexcept=default
 Default constructor. More...
 
 UnitVec (const underlying_type &v)
 Initializing constructor. More...
 
constexpr UnitVec Absolute () const noexcept
 Gets the absolute value.
 
constexpr const_reference at (size_type pos) const
 Gets a constant reference to the requested element. More...
 
const_iterator begin () const noexcept
 Gets a "begin" iterator.
 
const_iterator cbegin () const noexcept
 Gets a "begin" iterator.
 
const_iterator cend () const noexcept
 Gets an "end" iterator.
 
const_reverse_iterator crbegin () const noexcept
 Gets a reverse "begin" iterator.
 
const_reverse_iterator crend () const noexcept
 Gets a reverse "end" iterator.
 
constexpr const_pointer data () const noexcept
 Direct access to data.
 
const_iterator end () const noexcept
 Gets an "end" iterator.
 
constexpr UnitVec FlipX () const noexcept
 Flips the X value.
 
constexpr UnitVec FlipXY () const noexcept
 Flips the X and Y values.
 
constexpr UnitVec FlipY () const noexcept
 Flips the Y value.
 
constexpr UnitVec GetFwdPerpendicular () const noexcept
 Gets a vector clockwise (forward-clockwise) perpendicular to this vector. More...
 
constexpr UnitVec GetRevPerpendicular () const noexcept
 Gets a vector counter-clockwise (reverse-clockwise) perpendicular to this vector. More...
 
constexpr auto GetX () const noexcept
 Gets the "X" value.
 
constexpr auto GetY () const noexcept
 Gets the "Y" value.
 
constexpr operator underlying_type () const noexcept
 Implicitly gets the underlying value via a cast or implicit conversion.
 
constexpr UnitVec operator+ () const noexcept
 Positive operator.
 
constexpr UnitVec operator- () const noexcept
 Negation operator.
 
constexpr const_reference operator[] (size_type pos) const noexcept
 Gets a constant reference to the requested element. More...
 
const_reverse_iterator rbegin () const noexcept
 Gets a reverse "begin" iterator.
 
const_reverse_iterator rend () const noexcept
 Gets a reverse "end" iterator.
 
constexpr UnitVec Rotate (const UnitVec &amount) const noexcept
 Rotates the unit vector by the given amount. More...
 

Static Public Member Functions

static constexpr bool empty () noexcept
 Whether empty. More...
 
static UnitVec Get (Angle angle) noexcept
 Gets the given angled unit vector. More...
 
template<typename T >
static PolarCoord< T > Get (const T x, const T y, const UnitVec &fallback=GetDefaultFallback()) noexcept
 Gets the unit vector & magnitude from the given parameters. More...
 
static constexpr UnitVec GetDefaultFallback () noexcept
 Gets the default fallback.
 
static constexpr UnitVec GetDown () noexcept
 Gets the down-ward oriented unit vector. More...
 
static constexpr UnitVec GetDownRight () noexcept
 Gets the -45 degree unit vector. More...
 
static constexpr UnitVec GetLeft () noexcept
 Gets the left-ward oriented unit vector. More...
 
static constexpr UnitVec GetRight () noexcept
 Gets the right-ward oriented unit vector. More...
 
static constexpr UnitVec GetUp () noexcept
 Gets the up-ward oriented unit vector. More...
 
static constexpr UnitVec GetUpRight () noexcept
 Gets the 45 degree unit vector. More...
 
static constexpr UnitVec GetZero () noexcept
 Gets the non-oriented unit vector.
 
static constexpr size_type max_size () noexcept
 Gets the max size.
 
static constexpr size_type size () noexcept
 Gets the size.
 
static auto ThrowIfInvalid (const underlying_type &value) -> void
 Throws this class's exception type if the given value is invalid. More...
 
static auto Validate (const underlying_type &value) -> underlying_type
 

Static Public Attributes

static constexpr auto N = std::size_t{2}
 Dimensionality of this type.
 

Detailed Description

2-D unit vector.

This is a 2-dimensional directional vector.

Invariant
The magnitude of a non-oriented unit vector is zero. The magnitude of an oriented unit vector is approximately one, and is always more than zero.

Member Typedef Documentation

◆ PolarCoord

template<typename T >
using playrho::d2::UnitVec::PolarCoord = std::enable_if_t<IsArithmeticV<T>, std::pair<UnitVec, T> >

Polar coordinate.

This is a direction and magnitude pair defined by the unit vector class.

Note
A magnitude of 0 indicates that no conclusive direction could be determined. The magnitude will otherwise be a normal value.

Constructor & Destructor Documentation

◆ UnitVec() [1/2]

constexpr playrho::d2::UnitVec::UnitVec ( )
constexprdefaultnoexcept

Default constructor.

Constructs a non-oriented unit vector.

Postcondition
GetX() and GetY() return zero.

◆ UnitVec() [2/2]

playrho::d2::UnitVec::UnitVec ( const underlying_type v)
inlineexplicit

Initializing constructor.

Explicitly converts a vector of unit magnitude to an object or throws.

Note
This function is intended for use with values having a magnitude believed to already be within two ULPs of the real value of 1. Use a function like the polar coordinate returning Get function instead for arbitrary values.
Postcondition
GetX and GetY return the X and Y components respectively of the given value, exactly.
Exceptions
exception_typeConstructed with the returned error - if the checker returned a non-null explanatory string.

Member Function Documentation

◆ at()

constexpr const_reference playrho::d2::UnitVec::at ( size_type  pos) const
inlineconstexpr

Gets a constant reference to the requested element.

Exceptions
InvalidArgumentif given a position that's >= size().

◆ empty()

static constexpr bool playrho::d2::UnitVec::empty ( )
inlinestaticconstexprnoexcept

Whether empty.

Note
Always false for N > 0.

◆ Get() [1/2]

UnitVec playrho::d2::UnitVec::Get ( Angle  angle)
staticnoexcept

Gets the given angled unit vector.

Note
For angles that are meant to be at exact multiples of the quarter turn, better accuracy will be had by using one of the four oriented unit vector returning methods - for the right, up, left, down orientations.

◆ Get() [2/2]

◆ GetDown()

static constexpr UnitVec playrho::d2::UnitVec::GetDown ( )
inlinestaticconstexprnoexcept

Gets the down-ward oriented unit vector.

Note
This is the actual value for the 3/4 turned (270 degree angled) unit vector.
This is the reverse perpendicular unit vector of the left oriented vector.
This is the forward perpendicular unit vector of the right oriented vector.
Examples
Compositor.cpp, and PulleyJoint.cpp.

Referenced by Get(), and playrho::d2::RayCast().

◆ GetDownRight()

static constexpr UnitVec playrho::d2::UnitVec::GetDownRight ( )
inlinestaticconstexprnoexcept

Gets the -45 degree unit vector.

This is the unit vector in the positive X and negative Y quadrant where |X| == |Y|.

◆ GetFwdPerpendicular()

constexpr UnitVec playrho::d2::UnitVec::GetFwdPerpendicular ( ) const
inlineconstexprnoexcept

Gets a vector clockwise (forward-clockwise) perpendicular to this vector.

This returns the unit vector (y, -x).

Returns
A clockwise 90-degree rotation of this vector.
See also
GetRevPerpendicular.

Referenced by playrho::d2::GetFwdPerpendicular().

◆ GetLeft()

static constexpr UnitVec playrho::d2::UnitVec::GetLeft ( )
inlinestaticconstexprnoexcept

Gets the left-ward oriented unit vector.

Note
This is the actual value for the 2/4 turned (180 degree angled) unit vector.
This is the reverse perpendicular unit vector of the up oriented vector.
This is the forward perpendicular unit vector of the down oriented vector.
Examples
Compositor.cpp, and WorldBody.cpp.

Referenced by Get(), and playrho::d2::RayCast().

◆ GetRevPerpendicular()

constexpr UnitVec playrho::d2::UnitVec::GetRevPerpendicular ( ) const
inlineconstexprnoexcept

Gets a vector counter-clockwise (reverse-clockwise) perpendicular to this vector.

This returns the unit vector (-y, x).

Returns
A counter-clockwise 90-degree rotation of this vector.
See also
GetFwdPerpendicular.

Referenced by playrho::d2::GetRevPerpendicular(), and playrho::d2::GetYAxis().

◆ GetRight()

static constexpr UnitVec playrho::d2::UnitVec::GetRight ( )
inlinestaticconstexprnoexcept

Gets the right-ward oriented unit vector.

Note
This is the value for the 0/4 turned (0 angled) unit vector.
This is the reverse perpendicular unit vector of the down oriented vector.
This is the forward perpendicular unit vector of the up oriented vector.
Examples
Compositor.cpp, PrismaticJoint.cpp, Shape.cpp, WheelJoint.cpp, World.cpp, WorldBody.cpp, and WorldContact.cpp.

Referenced by Get(), playrho::d2::RayCast(), playrho::d2::part::Compositor< P1, P2, P3, P4, P5, P6 >::Rotate(), and playrho::d2::detail::Rotate().

◆ GetUp()

static constexpr UnitVec playrho::d2::UnitVec::GetUp ( )
inlinestaticconstexprnoexcept

Gets the up-ward oriented unit vector.

Note
This is the actual value for the 1/4 turned (90 degree angled) unit vector.
This is the reverse perpendicular unit vector of the right oriented vector.
This is the forward perpendicular unit vector of the left oriented vector.
Examples
Compositor.cpp, GearJoint.cpp, Shape.cpp, and WorldShape.cpp.

Referenced by Get(), and playrho::d2::RayCast().

◆ GetUpRight()

static constexpr UnitVec playrho::d2::UnitVec::GetUpRight ( )
inlinestaticconstexprnoexcept

Gets the 45 degree unit vector.

This is the unit vector in the positive X and Y quadrant where X == Y.

Examples
WorldBody.cpp.

◆ operator[]()

constexpr const_reference playrho::d2::UnitVec::operator[] ( size_type  pos) const
inlineconstexprnoexcept

Gets a constant reference to the requested element.

Note
No bounds checking is performed.
Parameters
posValid element index to get value for.
Precondition
The given position parameter (pos), is less than size().

◆ Rotate()

constexpr UnitVec playrho::d2::UnitVec::Rotate ( const UnitVec amount) const
inlineconstexprnoexcept

Rotates the unit vector by the given amount.

Parameters
amountExpresses the angular difference from the right-ward oriented unit vector to rotate this unit vector by.
Returns
Result of rotating this unit vector by the given amount.

◆ ThrowIfInvalid()

static auto playrho::d2::UnitVec::ThrowIfInvalid ( const underlying_type value) -> void
inlinestatic

Throws this class's exception type if the given value is invalid.

Exceptions
exception_typeConstructed with the returned error - if the checker returned a non-null explanatory string.
See also
exception_type.

Referenced by Validate().

◆ Validate()

static auto playrho::d2::UnitVec::Validate ( const underlying_type value) -> underlying_type
inlinestatic

Validates the given value using the checker_type type and returns the value given if it checks out.

Exceptions
exception_typeConstructed with the returned error - if the checker returned a non-null explanatory string.
Returns
Value given.

Referenced by UnitVec().


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