69template <
class T =
int>
88 template <
typename CastingType>
91 if( std::is_floating_point<T>() )
93 x =
static_cast<T
>( aVec.
x );
94 y =
static_cast<T
>( aVec.
y );
96 else if( std::is_floating_point<CastingType>() )
98 CastingType minI =
static_cast<CastingType
>( std::numeric_limits<T>::min() );
99 CastingType maxI =
static_cast<CastingType
>( std::numeric_limits<T>::max() );
101 x =
static_cast<T
>( std::clamp( aVec.
x, minI, maxI ) );
102 y =
static_cast<T
>( std::clamp( aVec.
y, minI, maxI ) );
104 else if( std::is_integral<T>() && std::is_integral<CastingType>() )
106 int64_t minI =
static_cast<int64_t
>( std::numeric_limits<T>::min() );
107 int64_t maxI =
static_cast<int64_t
>( std::numeric_limits<T>::max() );
109 x =
static_cast<T
>( std::clamp(
static_cast<int64_t
>( aVec.
x ), minI, maxI ) );
110 y =
static_cast<T
>( std::clamp(
static_cast<int64_t
>( aVec.
y ), minI, maxI ) );
114 x =
static_cast<T
>( aVec.
x );
115 y =
static_cast<T
>( aVec.
y );
127 template <
typename U>
130 if( std::is_floating_point<U>::value )
132 return VECTOR2<U>(
static_cast<U
>(
x ),
static_cast<U
>(
y ) );
134 else if( std::is_floating_point<T>() )
136 T minI =
static_cast<T
>( std::numeric_limits<U>::min() );
137 T maxI =
static_cast<T
>( std::numeric_limits<U>::max() );
138 return VECTOR2<U>(
static_cast<U
>( std::clamp(
x, minI, maxI ) ),
139 static_cast<U
>( std::clamp(
y, minI, maxI ) ) );
141 else if( std::is_integral<T>() && std::is_integral<U>() )
143 int64_t minI =
static_cast<int64_t
>( std::numeric_limits<U>::min() );
144 int64_t maxI =
static_cast<int64_t
>( std::numeric_limits<U>::max() );
145 int64_t x64 =
static_cast<int64_t
>(
x );
146 int64_t y64 =
static_cast<int64_t
>(
y );
149 static_cast<U
>( std::clamp( x64, minI, maxI ) ),
150 static_cast<U
>( std::clamp( y64, minI, maxI ) ) );
154 return VECTOR2<U>(
static_cast<U
>(
x ),
static_cast<U
>(
y ) );
286 if( std::is_integral<T>::value )
287 return KiROUND<double, T>(
std::abs( x ) * M_SQRT2 );
289 return static_cast<T
>(
std::abs( x ) * M_SQRT2 );
293 return static_cast<T
>(
std::abs( y ) );
295 return static_cast<T
>(
std::abs( x ) );
297 if( std::is_integral<T>::value )
298 return KiROUND<double, T>( std::hypot( x, y ) );
300 return static_cast<T
>( std::hypot( x, y ) );
315 return perpendicular;
385 if( x == 0 && y == 0 )
393 newX = newY =
std::abs( aNewLength ) * M_SQRT1_2;
401 newX = std::sqrt(
rescale( newLength_sq, x_sq, l_sq ) );
402 newY = std::sqrt(
rescale( newLength_sq, y_sq, l_sq ) );
405 if( std::is_integral<T>::value )
409 *
sign( aNewLength );
413 return VECTOR2<T>(
static_cast<T
>( x < 0 ? -newX : newX ),
414 static_cast<T
>( y < 0 ? -newY : newY ) )
415 *
sign( aNewLength );
423 std::stringstream ss;
425 ss <<
"( xy " << x <<
" " << y <<
" )";
438template <
class T,
class U>
445template <FloatingPo
int T,
class U>
448 return VECTOR2<T>( aLHS.
x + aScalar, aLHS.
y + aScalar );
452template <Integral T, Integral U>
455 return VECTOR2<T>( aLHS.
x + aScalar, aLHS.
y + aScalar );
459template <Integral T, FloatingPo
int U>
466template <
class T,
class U>
473template <FloatingPo
int T,
class U>
476 return VECTOR2<T>( aLHS.
x - aScalar, aLHS.
y - aScalar );
480template <Integral T, Integral U>
483 return VECTOR2<T>( aLHS.
x - aScalar, aLHS.
y - aScalar );
487template <Integral T, FloatingPo
int U>
501template <
class T,
class U>
509 return (extended_type)aLHS.
x * aRHS.
x + (extended_type)aLHS.
y * aRHS.
y;
513template <
class T,
class U>
520template <
class T,
class U>
530 if( std::is_integral<T>::value )
533 return VECTOR2<T>(
static_cast<T
>( x / aFactor ),
static_cast<T
>( y / aFactor ) );
563 return ( *
this * *
this ) < ( aVector * aVector );
570 return ( *
this * *
this ) <= ( aVector * aVector );
577 return ( *
this * *
this ) > ( aVector * aVector );
584 return ( *
this * *
this ) >= ( aVector * aVector );
591 return ( aVector.
x == x ) && ( aVector.
y == y );
598 return ( aVector.
x != x ) || ( aVector.
y != y );
607 else if( aA.
x == aB.
x && aA.
y > aB.
y )
619 else if( aA.
x == aB.
x && aA.
y < aB.
y )
631 else if( aA.
x > aB.
x )
637 else if( aA.
y > aB.
y )
654typename std::enable_if<!std::numeric_limits<T>::is_integer,
bool>::type
656 T aEpsilon = std::numeric_limits<T>::epsilon() )
658 if( !
equals( aFirst.
x, aSecond.
x, aEpsilon ) )
663 return equals( aFirst.
y, aSecond.
y, aEpsilon );
670 aStream <<
"[ " << aVector.
x <<
" | " << aVector.
y <<
" ]";
Define a general 2D-vector/point.
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).
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
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.
VECTOR2< U > operator()() const
Cast a vector to another specialized subclass. Beware of rounding issues.
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.
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.
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.
std::ostream & operator<<(std::ostream &aStream, const EDA_TEXT &aText)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
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
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D
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< std::common_type_t< T, U > > operator+(const VECTOR2< T > &aLHS, const VECTOR2< U > &aRHS)
auto operator*(const VECTOR2< T > &aLHS, const VECTOR2< U > &aRHS)
VECTOR2< int64_t > VECTOR2L
VECTOR2< std::common_type_t< T, U > > operator-(const VECTOR2< T > &aLHS, const VECTOR2< U > &aRHS)
VECTOR2I KiROUND(const VECTOR2D &vec)
const VECTOR2< T > LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)