2013-03-28 16:30:09 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Virtenio GmbH, Torsten Hueter, torsten.hueter <at> virtenio.de
|
|
|
|
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2021-01-25 12:42:36 +00:00
|
|
|
* Copyright (C) 2012-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-04-11 08:04:38 +00:00
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2013-03-28 16:30:09 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef VECTOR2D_H_
|
|
|
|
#define VECTOR2D_H_
|
|
|
|
|
2017-11-05 21:03:30 +00:00
|
|
|
#include <limits>
|
2013-03-28 16:30:09 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2021-01-09 21:04:42 +00:00
|
|
|
#include <type_traits>
|
2013-03-28 16:30:09 +00:00
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/util.h>
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
#ifdef WX_COMPATIBILITY
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <wx/gdicmn.h>
|
2013-03-28 16:30:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Traits class for VECTOR2.
|
2013-03-28 16:30:09 +00:00
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
struct VECTOR2_TRAITS
|
|
|
|
{
|
2021-01-25 12:42:36 +00:00
|
|
|
///< extended range/precision types used by operations involving multiple
|
|
|
|
///< multiplications to prevent overflow.
|
2013-03-28 16:30:09 +00:00
|
|
|
typedef T extended_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct VECTOR2_TRAITS<int>
|
|
|
|
{
|
|
|
|
typedef int64_t extended_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Forward declarations for template friends
|
|
|
|
template <class T>
|
|
|
|
class VECTOR2;
|
|
|
|
template <class T>
|
2013-10-14 18:40:36 +00:00
|
|
|
std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector );
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Define a general 2D-vector/point.
|
2013-03-28 16:30:09 +00:00
|
|
|
*
|
2013-04-05 13:10:31 +00:00
|
|
|
* This class uses templates to be universal. Several operators are provided to help
|
|
|
|
* easy implementing of linear algebra equations.
|
2013-03-28 16:30:09 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
template <class T = int>
|
2017-11-05 21:03:30 +00:00
|
|
|
class VECTOR2
|
2013-03-28 16:30:09 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename VECTOR2_TRAITS<T>::extended_type extended_type;
|
|
|
|
typedef T coord_type;
|
|
|
|
|
2017-11-05 21:03:30 +00:00
|
|
|
static constexpr extended_type ECOORD_MAX = std::numeric_limits<extended_type>::max();
|
|
|
|
static constexpr extended_type ECOORD_MIN = std::numeric_limits<extended_type>::min();
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
T x, y;
|
|
|
|
|
|
|
|
/// Construct a 2D-vector with x, y = 0
|
|
|
|
VECTOR2();
|
|
|
|
|
|
|
|
#ifdef WX_COMPATIBILITY
|
|
|
|
/// Constructor with a wxPoint as argument
|
|
|
|
VECTOR2( const wxPoint& aPoint );
|
|
|
|
|
|
|
|
/// Constructor with a wxSize as argument
|
|
|
|
VECTOR2( const wxSize& aSize );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Construct a vector with given components x, y
|
|
|
|
VECTOR2( T x, T y );
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
/// Initializes a vector from another specialization. Beware of rounding issues.
|
2013-03-28 16:30:09 +00:00
|
|
|
template <typename CastingType>
|
|
|
|
VECTOR2( const VECTOR2<CastingType>& aVec )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x = (T) aVec.x;
|
|
|
|
y = (T) aVec.y;
|
2013-03-28 16:30:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 01:49:11 +00:00
|
|
|
/// Copy a vector
|
|
|
|
VECTOR2( const VECTOR2<T>& aVec )
|
|
|
|
{
|
|
|
|
x = aVec.x;
|
|
|
|
y = aVec.y;
|
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
/// Cast a vector to another specialized subclass. Beware of rounding issues.
|
2013-03-28 16:30:09 +00:00
|
|
|
template <typename CastedType>
|
|
|
|
VECTOR2<CastedType> operator()() const
|
|
|
|
{
|
|
|
|
return VECTOR2<CastedType>( (CastedType) x, (CastedType) y );
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Implement the cast to wxPoint.
|
|
|
|
*
|
|
|
|
* @return the vector cast to wxPoint.
|
2017-03-07 12:06:00 +00:00
|
|
|
*/
|
|
|
|
explicit operator wxPoint() const
|
|
|
|
{
|
|
|
|
return wxPoint( x, y );
|
2021-12-29 21:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement the cast to wxPoint.
|
|
|
|
*
|
|
|
|
* @return the vector cast to wxPoint.
|
|
|
|
*/
|
|
|
|
explicit operator wxSize() const
|
|
|
|
{
|
|
|
|
return wxSize( x, y );
|
2017-03-07 12:06:00 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
// virtual ~VECTOR2();
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
|
|
|
|
*
|
2013-03-28 16:30:09 +00:00
|
|
|
* It is used to calculate the length of the vector.
|
2021-01-25 12:42:36 +00:00
|
|
|
*
|
2013-03-28 16:30:09 +00:00
|
|
|
* @return Scalar, the euclidean norm
|
|
|
|
*/
|
2013-04-05 13:10:31 +00:00
|
|
|
T EuclideanNorm() const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
|
|
|
|
*
|
2013-09-09 14:56:18 +00:00
|
|
|
* It is used to calculate the length of the vector.
|
2021-01-25 12:42:36 +00:00
|
|
|
*
|
2013-09-09 14:56:18 +00:00
|
|
|
* @return Scalar, the euclidean norm
|
2013-03-28 16:30:09 +00:00
|
|
|
*/
|
2013-10-14 14:13:35 +00:00
|
|
|
extended_type SquaredEuclideanNorm() const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Compute the perpendicular vector.
|
|
|
|
*
|
2013-09-09 14:56:18 +00:00
|
|
|
* @return Perpendicular vector
|
2013-03-28 16:30:09 +00:00
|
|
|
*/
|
2013-09-09 14:56:18 +00:00
|
|
|
VECTOR2<T> Perpendicular() const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return a vector of the same direction, but length specified in \a aNewLength.
|
|
|
|
*
|
|
|
|
* @param aNewLength is the length of the rescaled vector.
|
|
|
|
* @return the rescaled vector.
|
2013-03-28 16:30:09 +00:00
|
|
|
*/
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T> Resize( T aNewLength ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return the vector formatted as a string.
|
|
|
|
*
|
2013-03-28 16:30:09 +00:00
|
|
|
* @return the formatted string
|
|
|
|
*/
|
2013-04-05 13:10:31 +00:00
|
|
|
const std::string Format() const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Compute cross product of self with \a aVector.
|
2013-03-28 16:30:09 +00:00
|
|
|
*/
|
2013-04-05 13:10:31 +00:00
|
|
|
extended_type Cross( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Compute dot product of self with \a aVector.
|
2013-03-28 16:30:09 +00:00
|
|
|
*/
|
2013-04-05 13:10:31 +00:00
|
|
|
extended_type Dot( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Operators
|
|
|
|
|
|
|
|
/// Assignment operator
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T>& operator=( const VECTOR2<T>& aVector );
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/// Vector addition operator
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T> operator+( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
2013-07-26 16:12:22 +00:00
|
|
|
/// Scalar addition operator
|
|
|
|
VECTOR2<T> operator+( const T& aScalar ) const;
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
/// Compound assignment operator
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T>& operator+=( const VECTOR2<T>& aVector );
|
2013-03-28 16:30:09 +00:00
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
/// Compound assignment operator
|
|
|
|
VECTOR2<T>& operator*=( const VECTOR2<T>& aVector );
|
|
|
|
|
2013-07-26 16:12:22 +00:00
|
|
|
/// Compound assignment operator
|
|
|
|
VECTOR2<T>& operator+=( const T& aScalar );
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
/// Vector subtraction operator
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T> operator-( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
2013-07-26 16:12:22 +00:00
|
|
|
/// Scalar subtraction operator
|
|
|
|
VECTOR2<T> operator-( const T& aScalar ) const;
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
/// Compound assignment operator
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T>& operator-=( const VECTOR2<T>& aVector );
|
2013-03-28 16:30:09 +00:00
|
|
|
|
2013-07-26 16:12:22 +00:00
|
|
|
/// Compound assignment operator
|
|
|
|
VECTOR2<T>& operator-=( const T& aScalar );
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
/// Negate Vector operator
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T> operator-();
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/// Scalar product operator
|
2013-04-05 13:10:31 +00:00
|
|
|
extended_type operator*( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/// Multiplication with a factor
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T> operator*( const T& aFactor ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/// Division with a factor
|
2013-04-05 13:10:31 +00:00
|
|
|
VECTOR2<T> operator/( const T& aFactor ) const;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
/// Equality operator
|
2015-07-15 12:08:50 +00:00
|
|
|
bool operator==( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/// Not equality operator
|
2015-07-15 12:08:50 +00:00
|
|
|
bool operator!=( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/// Smaller than operator
|
2013-04-05 13:10:31 +00:00
|
|
|
bool operator<( const VECTOR2<T>& aVector ) const;
|
|
|
|
bool operator<=( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
|
|
|
/// Greater than operator
|
2013-04-05 13:10:31 +00:00
|
|
|
bool operator>( const VECTOR2<T>& aVector ) const;
|
|
|
|
bool operator>=( const VECTOR2<T>& aVector ) const;
|
2013-03-28 16:30:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
// --- Implementation ---
|
|
|
|
// ----------------------
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
VECTOR2<T>::VECTOR2()
|
|
|
|
{
|
|
|
|
x = y = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WX_COMPATIBILITY
|
|
|
|
template <class T>
|
2021-07-26 23:47:26 +00:00
|
|
|
VECTOR2<T>::VECTOR2( const wxPoint& aPoint )
|
2013-03-28 16:30:09 +00:00
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x = T( aPoint.x );
|
|
|
|
y = T( aPoint.y );
|
2013-03-28 16:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
2021-07-26 23:47:26 +00:00
|
|
|
VECTOR2<T>::VECTOR2( const wxSize& aSize )
|
2013-03-28 16:30:09 +00:00
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x = T( aSize.x );
|
|
|
|
y = T( aSize.y );
|
2013-03-28 16:30:09 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
VECTOR2<T>::VECTOR2( T aX, T aY )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x = aX;
|
|
|
|
y = aY;
|
2013-03-28 16:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T VECTOR2<T>::EuclideanNorm() const
|
|
|
|
{
|
|
|
|
return sqrt( (extended_type) x * x + (extended_type) y * y );
|
|
|
|
}
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-09 14:56:18 +00:00
|
|
|
template <class T>
|
|
|
|
typename VECTOR2<T>::extended_type VECTOR2<T>::SquaredEuclideanNorm() const
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
return (extended_type) x * x + (extended_type) y * y;
|
2013-09-09 14:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::Perpendicular() const
|
|
|
|
{
|
|
|
|
VECTOR2<T> perpendicular( -y, x );
|
|
|
|
return perpendicular;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
VECTOR2<T>& VECTOR2<T>::operator=( const VECTOR2<T>& aVector )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x = aVector.x;
|
|
|
|
y = aVector.y;
|
2013-03-28 16:30:09 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
VECTOR2<T>& VECTOR2<T>::operator+=( const VECTOR2<T>& aVector )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x += aVector.x;
|
|
|
|
y += aVector.y;
|
2013-03-28 16:30:09 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-04 23:00:00 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T>& VECTOR2<T>::operator*=( const VECTOR2<T>& aVector )
|
|
|
|
{
|
|
|
|
x *= aVector.x;
|
|
|
|
y *= aVector.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 16:12:22 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T>& VECTOR2<T>::operator+=( const T& aScalar )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x += aScalar;
|
|
|
|
y += aScalar;
|
2013-07-26 16:12:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T>& VECTOR2<T>::operator-=( const VECTOR2<T>& aVector )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x -= aVector.x;
|
|
|
|
y -= aVector.y;
|
2013-03-28 16:30:09 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 16:12:22 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T>& VECTOR2<T>::operator-=( const T& aScalar )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
x -= aScalar;
|
|
|
|
y -= aScalar;
|
2013-07-26 16:12:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-10-14 18:40:36 +00:00
|
|
|
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::Resize( T aNewLength ) const
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
if( x == 0 && y == 0 )
|
|
|
|
return VECTOR2<T> ( 0, 0 );
|
2013-03-28 16:30:09 +00:00
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
extended_type l_sq_current = (extended_type) x * x + (extended_type) y * y;
|
|
|
|
extended_type l_sq_new = (extended_type) aNewLength * aNewLength;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
2021-01-09 21:04:42 +00:00
|
|
|
if( std::is_integral<T>::value )
|
|
|
|
{
|
|
|
|
return VECTOR2<T> (
|
|
|
|
( x < 0 ? -1 : 1 ) *
|
|
|
|
KiROUND( std::sqrt( rescale( l_sq_new, (extended_type) x * x, l_sq_current ) ) ),
|
|
|
|
( y < 0 ? -1 : 1 ) *
|
2021-01-16 09:30:39 +00:00
|
|
|
KiROUND( std::sqrt( rescale( l_sq_new, (extended_type) y * y, l_sq_current ) ) ) )
|
|
|
|
* sign( aNewLength );
|
2021-01-09 21:04:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return VECTOR2<T> (
|
|
|
|
( x < 0 ? -1 : 1 ) *
|
|
|
|
std::sqrt( rescale( l_sq_new, (extended_type) x * x, l_sq_current ) ),
|
|
|
|
( y < 0 ? -1 : 1 ) *
|
|
|
|
std::sqrt( rescale( l_sq_new, (extended_type) y * y, l_sq_current ) ) )
|
|
|
|
* sign( aNewLength );
|
|
|
|
}
|
2013-03-28 16:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
const std::string VECTOR2<T>::Format() const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ss << "( xy " << x << " " << y << " )";
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::operator+( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
|
|
|
return VECTOR2<T> ( x + aVector.x, y + aVector.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 16:12:22 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::operator+( const T& aScalar ) const
|
|
|
|
{
|
|
|
|
return VECTOR2<T> ( x + aScalar, y + aScalar );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::operator-( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
|
|
|
return VECTOR2<T> ( x - aVector.x, y - aVector.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-26 16:12:22 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::operator-( const T& aScalar ) const
|
|
|
|
{
|
|
|
|
return VECTOR2<T> ( x - aScalar, y - aScalar );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::operator-()
|
|
|
|
{
|
|
|
|
return VECTOR2<T> ( -x, -y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
typename VECTOR2<T>::extended_type VECTOR2<T>::operator*( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
2017-08-06 11:31:25 +00:00
|
|
|
return (extended_type)aVector.x * x + (extended_type)aVector.y * y;
|
2013-03-28 16:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::operator*( const T& aFactor ) const
|
|
|
|
{
|
|
|
|
VECTOR2<T> vector( x * aFactor, y * aFactor );
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> VECTOR2<T>::operator/( const T& aFactor ) const
|
|
|
|
{
|
2021-01-18 17:39:13 +00:00
|
|
|
if( std::is_integral<T>::value )
|
|
|
|
return VECTOR2<T>( KiROUND( x / aFactor ), KiROUND( y / aFactor ) );
|
|
|
|
else
|
|
|
|
return VECTOR2<T>( x / aFactor, y / aFactor );
|
2013-03-28 16:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
VECTOR2<T> operator*( const T& aFactor, const VECTOR2<T>& aVector )
|
|
|
|
{
|
|
|
|
VECTOR2<T> vector( aVector.x * aFactor, aVector.y * aFactor );
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
typename VECTOR2<T>::extended_type VECTOR2<T>::Cross( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
2013-04-05 13:10:31 +00:00
|
|
|
return (extended_type) x * (extended_type) aVector.y -
|
2013-03-28 16:30:09 +00:00
|
|
|
(extended_type) y * (extended_type) aVector.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
typename VECTOR2<T>::extended_type VECTOR2<T>::Dot( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
2013-04-05 13:10:31 +00:00
|
|
|
return (extended_type) x * (extended_type) aVector.x +
|
2013-03-28 16:30:09 +00:00
|
|
|
(extended_type) y * (extended_type) aVector.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool VECTOR2<T>::operator<( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
|
|
|
return ( *this * *this ) < ( aVector * aVector );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool VECTOR2<T>::operator<=( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
|
|
|
return ( *this * *this ) <= ( aVector * aVector );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool VECTOR2<T>::operator>( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
|
|
|
return ( *this * *this ) > ( aVector * aVector );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
bool VECTOR2<T>::operator>=( const VECTOR2<T>& aVector ) const
|
|
|
|
{
|
|
|
|
return ( *this * *this ) >= ( aVector * aVector );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
2015-07-15 12:08:50 +00:00
|
|
|
bool VECTOR2<T>::operator==( VECTOR2<T> const& aVector ) const
|
2013-03-28 16:30:09 +00:00
|
|
|
{
|
|
|
|
return ( aVector.x == x ) && ( aVector.y == y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
2015-07-15 12:08:50 +00:00
|
|
|
bool VECTOR2<T>::operator!=( VECTOR2<T> const& aVector ) const
|
2013-03-28 16:30:09 +00:00
|
|
|
{
|
|
|
|
return ( aVector.x != x ) || ( aVector.y != y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
const VECTOR2<T> LexicographicalMax( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
|
|
|
|
{
|
|
|
|
if( aA.x > aB.x )
|
|
|
|
return aA;
|
|
|
|
else if( aA.x == aB.x && aA.y > aB.y )
|
|
|
|
return aA;
|
|
|
|
|
|
|
|
return aB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
const VECTOR2<T> LexicographicalMin( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
|
|
|
|
{
|
|
|
|
if( aA.x < aB.x )
|
|
|
|
return aA;
|
|
|
|
else if( aA.x == aB.x && aA.y < aB.y )
|
|
|
|
return aA;
|
|
|
|
|
|
|
|
return aB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
const int LexicographicalCompare( const VECTOR2<T>& aA, const VECTOR2<T>& aB )
|
|
|
|
{
|
|
|
|
if( aA.x < aB.x )
|
|
|
|
return -1;
|
|
|
|
else if( aA.x > aB.x )
|
|
|
|
return 1;
|
|
|
|
else // aA.x == aB.x
|
|
|
|
{
|
|
|
|
if( aA.y < aB.y )
|
|
|
|
return -1;
|
|
|
|
else if( aA.y > aB.y )
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
std::ostream& operator<<( std::ostream& aStream, const VECTOR2<T>& aVector )
|
|
|
|
{
|
|
|
|
aStream << "[ " << aVector.x << " | " << aVector.y << " ]";
|
|
|
|
return aStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Default specializations */
|
2016-12-22 17:58:29 +00:00
|
|
|
typedef VECTOR2<double> VECTOR2D;
|
|
|
|
typedef VECTOR2<int> VECTOR2I;
|
|
|
|
typedef VECTOR2<unsigned int> VECTOR2U;
|
2013-03-28 16:30:09 +00:00
|
|
|
|
2022-01-02 01:28:28 +00:00
|
|
|
/* STL specializations */
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
// Required to enable correct use in std::map/unordered_map
|
|
|
|
template <>
|
|
|
|
struct hash<VECTOR2I>
|
|
|
|
{
|
|
|
|
size_t operator()( const VECTOR2I& k ) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Required to enable use of std::hash with maps
|
|
|
|
template <>
|
|
|
|
struct less<VECTOR2I>
|
|
|
|
{
|
|
|
|
bool operator()( const VECTOR2I& aA, const VECTOR2I& aB ) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:30:09 +00:00
|
|
|
#endif // VECTOR2D_H_
|