68template <
class T =
int>
87 template <
typename CastingType>
90 if( std::is_floating_point<T>() )
92 x =
static_cast<T
>( aVec.
x );
93 y =
static_cast<T
>( aVec.
y );
95 else if( std::is_floating_point<CastingType>() )
97 CastingType minI =
static_cast<CastingType
>( std::numeric_limits<T>::min() );
98 CastingType maxI =
static_cast<CastingType
>( std::numeric_limits<T>::max() );
100 x =
static_cast<T
>(
Clamp( minI, aVec.
x, maxI ) );
101 y =
static_cast<T
>(
Clamp( minI, aVec.
y, maxI ) );
103 else if( std::is_integral<T>() && std::is_integral<CastingType>() )
105 int64_t minI =
static_cast<int64_t
>( std::numeric_limits<T>::min() );
106 int64_t maxI =
static_cast<int64_t
>( std::numeric_limits<T>::max() );
108 x =
static_cast<T
>(
Clamp( minI,
static_cast<int64_t
>( aVec.
x ), maxI ) );
109 y =
static_cast<T
>(
Clamp( minI,
static_cast<int64_t
>(aVec.
y), maxI ) );
113 x =
static_cast<T
>( aVec.
x );
114 y =
static_cast<T
>( aVec.
y );
126 template <
typename U>
129 if( std::is_floating_point<U>::value )
131 return VECTOR2<U>(
static_cast<U
>(
x ),
static_cast<U
>(
y ) );
133 else if( std::is_floating_point<T>() )
135 T minI =
static_cast<T
>( std::numeric_limits<U>::min() );
136 T maxI =
static_cast<T
>( std::numeric_limits<U>::max() );
138 static_cast<U
>(
Clamp( minI,
y, maxI ) ) );
140 else if( std::is_integral<T>() && std::is_integral<U>() )
142 int64_t minI =
static_cast<int64_t
>( std::numeric_limits<U>::min() );
143 int64_t maxI =
static_cast<int64_t
>( std::numeric_limits<U>::max() );
146 static_cast<U
>(
Clamp( minI,
static_cast<int64_t
>(
x ), maxI ) ),
147 static_cast<U
>(
Clamp( minI,
static_cast<int64_t
>(
y ), maxI ) ) );
151 return VECTOR2<U>(
static_cast<U
>(
x ),
static_cast<U
>(
y ) );
283 if( std::is_integral<T>::value )
284 return KiROUND<double, T>(
std::abs( x ) * M_SQRT2 );
286 return static_cast<T
>(
std::abs( x ) * M_SQRT2 );
290 return static_cast<T
>(
std::abs( y ) );
292 return static_cast<T
>(
std::abs( x ) );
294 if( std::is_integral<T>::value )
295 return KiROUND<double, T>( std::hypot( x, y ) );
297 return static_cast<T
>( std::hypot( x, y ) );
312 return perpendicular;
382 if( x == 0 && y == 0 )
390 newX = newY =
std::abs( aNewLength ) * M_SQRT1_2;
398 newX = std::sqrt(
rescale( newLength_sq, x_sq, l_sq ) );
399 newY = std::sqrt(
rescale( newLength_sq, y_sq, l_sq ) );
402 if( std::is_integral<T>::value )
406 *
sign( aNewLength );
410 return VECTOR2<T>(
static_cast<T
>( x < 0 ? -newX : newX ),
411 static_cast<T
>( y < 0 ? -newY : newY ) )
412 *
sign( aNewLength );
420 std::stringstream ss;
422 ss <<
"( xy " << x <<
" " << y <<
" )";
435template <
class T,
class U>
442template <FloatingPo
int T,
class U>
445 return VECTOR2<T>( aLHS.
x + aScalar, aLHS.
y + aScalar );
449template <Integral T, Integral U>
452 return VECTOR2<T>( aLHS.
x + aScalar, aLHS.
y + aScalar );
456template <Integral T, FloatingPo
int U>
463template <
class T,
class U>
470template <FloatingPo
int T,
class U>
473 return VECTOR2<T>( aLHS.
x - aScalar, aLHS.
y - aScalar );
477template <Integral T, Integral U>
480 return VECTOR2<T>( aLHS.
x - aScalar, aLHS.
y - aScalar );
484template <Integral T, FloatingPo
int U>
498template <
class T,
class U>
506 return (extended_type)aLHS.
x * aRHS.
x + (extended_type)aLHS.
y * aRHS.
y;
510template <
class T,
class U>
517template <
class T,
class U>
527 if( std::is_integral<T>::value )
530 return VECTOR2<T>(
static_cast<T
>( x / aFactor ),
static_cast<T
>( y / aFactor ) );
560 return ( *
this * *
this ) < ( aVector * aVector );
567 return ( *
this * *
this ) <= ( aVector * aVector );
574 return ( *
this * *
this ) > ( aVector * aVector );
581 return ( *
this * *
this ) >= ( aVector * aVector );
588 return ( aVector.
x == x ) && ( aVector.
y == y );
595 return ( aVector.
x != x ) || ( aVector.
y != y );
604 else if( aA.
x == aB.
x && aA.
y > aB.
y )
616 else if( aA.
x == aB.
x && aA.
y < aB.
y )
628 else if( aA.
x > aB.
x )
634 else if( aA.
y > aB.
y )
651typename std::enable_if<!std::numeric_limits<T>::is_integer,
bool>::type
653 T aEpsilon = std::numeric_limits<T>::epsilon() )
655 if( !
equals( aFirst.
x, aSecond.
x, aEpsilon ) )
660 return equals( aFirst.
y, aSecond.
y, aEpsilon );
667 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).
constexpr T Clamp(const T &lower, const T &value, const T &upper)
Limit value within the range lower <= value <= upper.
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)