17#ifndef IGNITION_MATH_VECTOR2_HH_
18#define IGNITION_MATH_VECTOR2_HH_
21#include <ignition/math/config.hh>
27 inline namespace IGNITION_MATH_VERSION_NAMESPACE
60 this->data[0] = _v[0];
61 this->data[1] = _v[1];
72 return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
73 (this->data[1]-_pt[1])*(this->data[1]-_pt[1]));
87 return std::pow(this->data[0], 2)
88 + std::pow(this->data[1], 2);
96 if (!
equal<T>(d,
static_cast<T
>(0.0)))
106 public:
void Set(T _x, T _y)
117 return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
125 this->data[0] = _v[0];
126 this->data[1] = _v[1];
147 return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
155 this->data[0] += _v[0];
156 this->data[1] += _v[1];
195 return Vector2(-this->data[0], -this->data[1]);
203 return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
211 this->data[0] -= _v[0];
212 this->data[1] -= _v[1];
233 return {_s - _v.
X(), _s - _v.
Y()};
253 return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
262 this->data[0] /= _v[0];
263 this->data[1] /= _v[1];
273 return Vector2(this->data[0] / _v, this->data[1] / _v);
292 return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
301 this->data[0] *= _v[0];
302 this->data[1] *= _v[1];
312 return Vector2(this->data[0] * _v, this->data[1] * _v);
343 return equal<T>(this->data[0], _v[0], _tol)
344 &&
equal<T>(this->data[1], _v[1], _tol);
353 return this->
Equal(_v,
static_cast<T
>(1e-6));
360 return !(*
this == _v);
369 return std::isfinite(
static_cast<double>(this->data[0])) &&
370 std::isfinite(
static_cast<double>(this->data[1]));
391 public:
inline T
X()
const
393 return this->data[0];
398 public:
inline T
Y()
const
400 return this->data[1];
405 public:
inline T &
X()
407 return this->data[0];
412 public:
inline T &
Y()
414 return this->data[1];
419 public:
inline void X(
const T &_v)
426 public:
inline void Y(
const T &_v)
435 public:
friend std::ostream
438 _out << _pt[0] <<
" " << _pt[1];
448 return this->data[0] < _pt[0] || this->data[1] < _pt[1];
455 public:
friend std::istream
460 _in.setf(std::ios_base::skipws);
Two dimensional (x, y) vector.
Definition Vector2.hh:33
friend std::ostream & operator<<(std::ostream &_out, const Vector2< T > &_pt)
Stream extraction operator.
Definition Vector2.hh:436
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition Vector2.hh:384
const Vector2< T > & operator+=(const T _s)
Addition assignment operator.
Definition Vector2.hh:183
Vector2< T > operator-(const T _s) const
Subtraction operators.
Definition Vector2.hh:220
friend const Vector2 operator*(const T _s, const Vector2 &_v)
Scalar left multiplication operators.
Definition Vector2.hh:319
const Vector2 operator*(T _v) const
Multiplication operators.
Definition Vector2.hh:310
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition Vector2.hh:85
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition Vector2.hh:351
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition Vector2.hh:260
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition Vector2.hh:70
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition Vector2.hh:328
bool operator<(const Vector2< T > &_pt) const
Less than operator.
Definition Vector2.hh:446
virtual ~Vector2()
Destructor.
Definition Vector2.hh:65
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition Vector2.hh:209
Vector2()
Default Constructor.
Definition Vector2.hh:41
T & operator[](const std::size_t _index)
Array subscript operator.
Definition Vector2.hh:376
bool Equal(const Vector2 &_v, const T &_tol) const
Equality test with tolerance.
Definition Vector2.hh:341
void Normalize()
Normalize the vector length.
Definition Vector2.hh:92
T & Y()
Return a mutable y value.
Definition Vector2.hh:412
static const Vector2< T > One
math::Vector2(1, 1)
Definition Vector2.hh:38
const Vector2< T > & operator-=(T _s)
Subtraction assignment operator.
Definition Vector2.hh:239
Vector2(const T &_x, const T &_y)
Constructor.
Definition Vector2.hh:50
const Vector2 & operator/=(T _v)
Division operator.
Definition Vector2.hh:279
void X(const T &_v)
Set the x value.
Definition Vector2.hh:419
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition Vector2.hh:456
friend Vector2< T > operator-(const T _s, const Vector2< T > &_v)
Subtraction operators.
Definition Vector2.hh:230
T Y() const
Return the y value.
Definition Vector2.hh:398
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition Vector2.hh:251
friend Vector2< T > operator+(const T _s, const Vector2< T > &_v)
Addition operators.
Definition Vector2.hh:174
T & X()
Return a mutable x value.
Definition Vector2.hh:405
void Y(const T &_v)
Set the y value.
Definition Vector2.hh:426
Vector2 & operator=(const Vector2 &_v)
Assignment operator.
Definition Vector2.hh:123
Vector2(const Vector2< T > &_v)
Copy constructor.
Definition Vector2.hh:58
const Vector2 & operator+=(const Vector2 &_v)
Addition assignment operator.
Definition Vector2.hh:153
void Set(T _x, T _y)
Set the contents of the vector.
Definition Vector2.hh:106
Vector2< T > operator+(const T _s) const
Addition operators.
Definition Vector2.hh:164
T Length() const
Returns the length (magnitude) of the vector.
Definition Vector2.hh:78
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition Vector2.hh:145
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition Vector2.hh:299
static const Vector2< T > Zero
math::Vector2(0, 0)
Definition Vector2.hh:35
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition Vector2.hh:290
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition Vector2.hh:365
T X() const
Return the x value.
Definition Vector2.hh:391
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition Vector2.hh:201
const Vector2 & operator=(T _v)
Assignment operator.
Definition Vector2.hh:134
const Vector2 operator/(T _v) const
Division operator.
Definition Vector2.hh:271
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition Vector2.hh:115
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition Vector2.hh:358
Vector2 operator-() const
Negation operator.
Definition Vector2.hh:193
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition Helpers.hh:395
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition Helpers.hh:545
Vector2< double > Vector2d
Definition Vector2.hh:477
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition Helpers.hh:216
Vector2< int > Vector2i
Definition Vector2.hh:476
Vector2< float > Vector2f
Definition Vector2.hh:478
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition Helpers.hh:219