68template <
class T =
int>
87 template <
typename CastingType>
90 if( std::is_same<T, int>::value )
92 CastingType minI =
static_cast<CastingType
>( std::numeric_limits<int>::min() );
93 CastingType maxI =
static_cast<CastingType
>( std::numeric_limits<int>::max() );
95 x =
static_cast<int>(
Clamp( minI, aVec.
x, maxI ) );
96 y =
static_cast<int>(
Clamp( minI, aVec.
y, maxI ) );
100 x =
static_cast<T
>( aVec.
x );
101 y =
static_cast<T
>( aVec.
y );
113 template <
typename CastedType>
116 if( std::is_same<CastedType, int>::value )
118 T minI =
static_cast<T
>( std::numeric_limits<int>::min() );
119 T maxI =
static_cast<T
>( std::numeric_limits<int>::max() );
122 static_cast<int>(
Clamp( minI,
y, maxI ) ) );
282 return perpendicular;
352 if( x == 0 && y == 0 )
359 double newX = std::sqrt(
rescale( newLength_sq, x_sq, l_sq ) );
360 double newY = std::sqrt(
rescale( newLength_sq, y_sq, l_sq ) );
362 if( std::is_integral<T>::value )
366 *
sign( aNewLength );
370 return VECTOR2<T>(
static_cast<T
>( x < 0 ? -newX : newX ),
371 static_cast<T
>( y < 0 ? -newY : newY ) )
372 *
sign( aNewLength );
380 std::stringstream ss;
382 ss <<
"( xy " << x <<
" " << y <<
" )";
398 return VECTOR2<T> ( x + aScalar, y + aScalar );
412 return VECTOR2<T> ( x - aScalar, y - aScalar );
433 VECTOR2<T> vector( x * aFactor, y * aFactor );
441 if( std::is_integral<T>::value )
444 return VECTOR2<T>(
static_cast<T
>( x / aFactor ),
static_cast<T
>( y / aFactor ) );
451 VECTOR2<T> vector( aVector.
x * aFactor, aVector.
y * aFactor );
475 return ( *
this * *
this ) < ( aVector * aVector );
482 return ( *
this * *
this ) <= ( aVector * aVector );
489 return ( *
this * *
this ) > ( aVector * aVector );
496 return ( *
this * *
this ) >= ( aVector * aVector );
503 return ( aVector.
x == x ) && ( aVector.
y == y );
510 return ( aVector.
x != x ) || ( aVector.
y != y );
519 else if( aA.
x == aB.
x && aA.
y > aB.
y )
531 else if( aA.
x == aB.
x && aA.
y < aB.
y )
543 else if( aA.
x > aB.
x )
549 else if( aA.
y > aB.
y )
566typename std::enable_if<!std::numeric_limits<T>::is_integer,
bool>::type
568 T aEpsilon = std::numeric_limits<T>::epsilon() )
570 if( !
equals( aFirst.
x, aSecond.
x, aEpsilon ) )
575 return equals( aFirst.
y, aSecond.
y, aEpsilon );
582 aStream <<
"[ " << aVector.
x <<
" | " << aVector.
y <<
" ]";
Define a general 2D-vector/point.
VECTOR2< T > operator-(const T &aScalar) const
Scalar subtraction operator.
VECTOR2< T > & operator+=(const T &aScalar)
Compound assignment operator.
VECTOR2(const VECTOR2< CastingType > &aVec)
Initializes a vector from another specialization. Beware of rounding issues.
VECTOR2< T > & operator*=(const T &aScalar)
extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
VECTOR2< T > & operator*=(const VECTOR2< T > &aVector)
Compound assignment operator.
const std::string Format() const
Return the vector formatted as a string.
VECTOR2()
Construct a 2D-vector with x, y = 0.
VECTOR2< T > operator/(double aFactor) const
Division with a factor.
VECTOR2(T x, T y)
Construct a vector with given components x, y.
bool operator>=(const VECTOR2< T > &aVector) const
static constexpr extended_type ECOORD_MAX
VECTOR2< T > & operator+=(const VECTOR2< T > &aVector)
Compound assignment operator.
VECTOR2(const VECTOR2< T > &aVec)
Copy a vector.
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
VECTOR2_TRAITS< T >::extended_type extended_type
bool operator==(const VECTOR2< T > &aVector) const
Equality operator.
VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
bool operator<=(const VECTOR2< T > &aVector) const
VECTOR2< T > & operator=(const VECTOR2< T > &aVector)
Assignment operator.
VECTOR2< T > & operator-=(const T &aScalar)
Compound assignment operator.
bool operator!=(const VECTOR2< T > &aVector) const
Not equality operator.
static constexpr extended_type ECOORD_MIN
extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
VECTOR2< CastedType > operator()() const
Cast a vector to another specialized subclass. Beware of rounding issues.
bool operator>(const VECTOR2< T > &aVector) const
Greater than operator.
extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
extended_type operator*(const VECTOR2< T > &aVector) const
Scalar product operator.
VECTOR2< T > operator+(const VECTOR2< T > &aVector) const
Vector addition operator.
VECTOR2< T > operator-(const VECTOR2< T > &aVector) const
Vector subtraction operator.
VECTOR2< T > operator*(const T &aFactor) const
Multiplication with a factor.
VECTOR2< T > operator+(const T &aScalar) const
Scalar addition operator.
VECTOR2< T > & operator-=(const VECTOR2< T > &aVector)
Compound assignment operator.
bool operator<(const VECTOR2< T > &aVector) const
Smaller than operator.
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
VECTOR2< T > operator-()
Negate Vector operator.
Traits class for VECTOR2.
T extended_type
extended range/precision types used by operations involving multiple multiplications to prevent overf...
size_t operator()(const VECTOR2I &k) const =delete
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
constexpr T Clamp(const T &lower, const T &value, const T &upper)
Limit value within the range lower <= value <= upper.
VECTOR2< double > VECTOR2D
std::ostream & operator<<(std::ostream &aStream, const VECTOR2< T > &aVector)
const VECTOR2< T > LexicographicalMax(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type equals(VECTOR2< T > const &aFirst, VECTOR2< T > const &aSecond, T aEpsilon=std::numeric_limits< T >::epsilon())
Template to compare two VECTOR2<T> values for equality within a required epsilon.
VECTOR2< unsigned int > VECTOR2U
const VECTOR2< T > LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
VECTOR2< T > operator*(const T &aFactor, const VECTOR2< T > &aVector)