65template <
class T =
int>
84 template <
typename CastingType>
87 if constexpr( std::is_floating_point<T>() )
89 x =
static_cast<T>( aVec.
x );
90 y =
static_cast<T>( aVec.
y );
92 else if constexpr( std::is_floating_point<CastingType>() )
94 CastingType minI =
static_cast<CastingType
>( std::numeric_limits<T>::min() );
95 CastingType maxI =
static_cast<CastingType
>( std::numeric_limits<T>::max() );
97 x =
static_cast<T>( std::clamp( aVec.
x, minI, maxI ) );
98 y =
static_cast<T>( std::clamp( aVec.
y, minI, maxI ) );
100 else if constexpr( std::is_integral<T>() && std::is_integral<CastingType>() )
102 int64_t minI =
static_cast<int64_t
>( std::numeric_limits<T>::min() );
103 int64_t maxI =
static_cast<int64_t
>( std::numeric_limits<T>::max() );
105 x =
static_cast<T>( std::clamp(
static_cast<int64_t
>( aVec.
x ), minI, maxI ) );
106 y =
static_cast<T>( std::clamp(
static_cast<int64_t
>( aVec.
y ), minI, maxI ) );
110 x =
static_cast<T>( aVec.
x );
111 y =
static_cast<T>( aVec.
y );
123 template <
typename U>
126 if constexpr( std::is_floating_point<U>::value )
128 return VECTOR2<U>(
static_cast<U
>(
x ),
static_cast<U
>(
y ) );
130 else if constexpr( std::is_floating_point<T>() )
132 constexpr T minI =
static_cast<T>( std::numeric_limits<U>::min() );
133 constexpr T maxI =
static_cast<T>( std::numeric_limits<U>::max() );
134 return VECTOR2<U>(
static_cast<U
>( std::clamp(
x, minI, maxI ) ),
135 static_cast<U
>( std::clamp(
y, minI, maxI ) ) );
137 else if constexpr( std::is_integral<T>() && std::is_integral<U>() )
139 constexpr int64_t minI =
static_cast<int64_t
>( std::numeric_limits<U>::min() );
140 constexpr int64_t maxI =
static_cast<int64_t
>( std::numeric_limits<U>::max() );
141 int64_t x64 =
static_cast<int64_t
>(
x );
142 int64_t y64 =
static_cast<int64_t
>(
y );
145 static_cast<U
>( std::clamp( x64, minI, maxI ) ),
146 static_cast<U
>( std::clamp( y64, minI, maxI ) ) );
150 return VECTOR2<U>(
static_cast<U
>(
x ),
static_cast<U
>(
y ) );
284 if constexpr( std::is_integral<T>::value )
287 return static_cast<T>(
std::abs(
x ) * M_SQRT2 );
295 if constexpr( std::is_integral<T>::value )
298 return static_cast<T>( std::hypot(
x,
y ) );
313 return perpendicular;
383 if(
x == 0 &&
y == 0 )
391 newX = newY =
std::abs( aNewLength ) * M_SQRT1_2;
399 newX = std::sqrt(
rescale( newLength_sq, x_sq, l_sq ) );
400 newY = std::sqrt(
rescale( newLength_sq, y_sq, l_sq ) );
403 if constexpr( std::is_integral<T>::value )
407 *
sign( aNewLength );
411 return VECTOR2<T>(
static_cast<T>(
x < 0 ? -newX : newX ),
412 static_cast<T>(
y < 0 ? -newY : newY ) )
413 *
sign( aNewLength );
421 std::stringstream ss;
423 ss <<
"( xy " <<
x <<
" " <<
y <<
" )";
436template <
class T,
class U>
444template <FloatingPo
int T,
class U>
447 return VECTOR2<T>( aLHS.
x + aScalar, aLHS.
y + aScalar );
451template <Integral T, Integral U>
454 return VECTOR2<T>( aLHS.
x + aScalar, aLHS.
y + aScalar );
458template <Integral T, FloatingPo
int U>
465template <
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>
509template <
class T,
class U>
516template <
class T,
class U>
526 if constexpr( std::is_integral<T>::value )
529 return VECTOR2<T>(
static_cast<T>(
x / aFactor ),
static_cast<T>(
y / aFactor ) );
551 VECTOR2<double> diff(
static_cast<double>( aVector.
x -
x ),
static_cast<double>( aVector.
y -
y ) );
561 return dx * dx + dy * dy;
568 return ( *
this * *
this ) < ( aVector * aVector );
575 return ( *
this * *
this ) <= ( aVector * aVector );
582 return ( *
this * *
this ) > ( aVector * aVector );
589 return ( *
this * *
this ) >= ( aVector * aVector );
596 return ( aVector.
x ==
x ) && ( aVector.
y ==
y );
603 return ( aVector.
x !=
x ) || ( aVector.
y !=
y );
612 else if( aA.
x == aB.
x && aA.
y > aB.
y )
624 else if( aA.
x == aB.
x && aA.
y < aB.
y )
636 else if( aA.
x > aB.
x )
642 else if( aA.
y > aB.
y )
661typename std::enable_if<!std::numeric_limits<T>::is_integer,
bool>::type
663 T aEpsilon = std::numeric_limits<T>::epsilon() )
665 if( !
equals( aFirst.
x, aSecond.
x, aEpsilon ) )
670 return equals( aFirst.
y, aSecond.
y, aEpsilon );
677 aStream <<
"[ " << aVector.
x <<
" | " << aVector.
y <<
" ]";
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Define a general 2D-vector/point.
constexpr bool operator==(const VECTOR2< T > &aVector) const
Equality operator.
constexpr bool operator>=(const VECTOR2< T > &aVector) const
constexpr VECTOR2< U > operator()() const
Cast a vector to another specialized subclass. Beware of rounding issues.
constexpr VECTOR2< T > & operator=(const VECTOR2< T > &aVector)
Assignment operator.
constexpr extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
constexpr extended_type SquaredDistance(const VECTOR2< T > &aVector) const
Compute the squared distance between two vectors.
const std::string Format() const
Return the vector formatted as a string.
constexpr VECTOR2< T > & operator*=(const VECTOR2< T > &aVector)
Compound assignment operator.
static constexpr extended_type ECOORD_MAX
constexpr VECTOR2< T > & operator-=(const T &aScalar)
Compound assignment operator.
constexpr VECTOR2< T > & operator+=(const T &aScalar)
Compound assignment operator.
constexpr bool operator!=(const VECTOR2< T > &aVector) const
Not equality operator.
constexpr VECTOR2< T > & operator-=(const VECTOR2< T > &aVector)
Compound assignment operator.
constexpr bool operator<(const VECTOR2< T > &aVector) const
Smaller than operator.
constexpr VECTOR2< T > operator-()
Negate Vector operator.
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
VECTOR2_TRAITS< size_type >::extended_type extended_type
constexpr VECTOR2< T > operator/(double aFactor) const
Division with a factor.
constexpr VECTOR2(const VECTOR2< CastingType > &aVec)
Initializes a vector from another specialization. Beware of rounding issues.
constexpr VECTOR2< T > Perpendicular() const
Compute the perpendicular vector.
static constexpr extended_type ECOORD_MIN
constexpr VECTOR2(const VECTOR2< T > &aVec)
Copy a vector.
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
constexpr VECTOR2(T x, T y)
Construct a vector with given components x, y.
constexpr VECTOR2< T > & operator*=(const T &aScalar)
constexpr bool operator<=(const VECTOR2< T > &aVector) const
constexpr VECTOR2()
Construct a 2D-vector with x, y = 0.
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
constexpr VECTOR2< T > & operator+=(const VECTOR2< T > &aVector)
Compound assignment operator.
constexpr bool operator>(const VECTOR2< T > &aVector) const
Greater than operator.
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
bool operator()(const VECTOR2I &aA, const VECTOR2I &aB) const
constexpr int sign(T val)
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D
std::ostream & operator<<(std::ostream &aStream, const VECTOR2< T > &aVector)
constexpr const VECTOR2< T > & LexicographicalMax(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
constexpr VECTOR2I KiROUND(const VECTOR2D &vec)
constexpr VECTOR2< std::common_type_t< T, U > > operator+(const VECTOR2< T > &aLHS, const VECTOR2< U > &aRHS)
constexpr VECTOR2< std::common_type_t< T, U > > operator-(const VECTOR2< T > &aLHS, const VECTOR2< U > &aRHS)
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.
constexpr const VECTOR2< T > & LexicographicalMin(const VECTOR2< T > &aA, const VECTOR2< T > &aB)
VECTOR2< int64_t > VECTOR2L
constexpr auto operator*(const VECTOR2< T > &aLHS, const VECTOR2< U > &aRHS)
constexpr int LexicographicalCompare(const VECTOR2< T > &aA, const VECTOR2< T > &aB)