17 #ifndef _IGNITION_VECTOR2_HH_
18 #define _IGNITION_VECTOR2_HH_
57 this->data[0] = _v[0];
58 this->data[1] = _v[1];
69 return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
70 (this->data[1]-_pt[1])*(this->data[1]-_pt[1]));
84 return std::pow(this->data[0], 2)
85 + std::pow(this->data[1], 2);
93 if (!equal<T>(d, static_cast<T>(0.0)))
103 public:
void Set(T _x, T _y)
114 return (this->data[0] * _v[0]) + (this->data[1] * _v[1]);
122 this->data[0] = _v[0];
123 this->data[1] = _v[1];
144 return Vector2(this->data[0] + _v[0], this->data[1] + _v[1]);
152 this->data[0] += _v[0];
153 this->data[1] += _v[1];
192 return Vector2(-this->data[0], -this->data[1]);
200 return Vector2(this->data[0] - _v[0], this->data[1] - _v[1]);
208 this->data[0] -= _v[0];
209 this->data[1] -= _v[1];
250 return Vector2(this->data[0] / _v[0], this->data[1] / _v[1]);
259 this->data[0] /= _v[0];
260 this->data[1] /= _v[1];
270 return Vector2(this->data[0] / _v, this->data[1] / _v);
289 return Vector2(this->data[0] * _v[0], this->data[1] * _v[1]);
298 this->data[0] *= _v[0];
299 this->data[1] *= _v[1];
309 return Vector2(this->data[0] * _v, this->data[1] * _v);
340 return equal<T>(this->data[0], _v[0], _tol)
341 && equal<T>(this->data[1], _v[1], _tol);
350 return this->
Equal(_v, static_cast<T>(1e-6));
357 return !(*
this == _v);
366 return std::isfinite(static_cast<double>(this->data[0])) &&
367 std::isfinite(static_cast<double>(this->data[1]));
379 return this->data[_index];
385 public:
inline T
X()
const
387 return this->data[0];
393 public:
inline T
Y()
const
395 return this->data[1];
401 public:
inline T &
X()
403 return this->data[0];
409 public:
inline T &
Y()
411 return this->data[1];
416 public:
inline void X(
const T &_v)
423 public:
inline void Y(
const T &_v)
433 public:
friend std::ostream
434 &operator<<(std::ostream &_out, const Vector2<T> &_pt)
436 _out << _pt[0] <<
" " << _pt[1];
444 public:
bool operator<(const Vector2<T> &_pt)
const
446 return this->data[0] < _pt[0] || this->data[1] < _pt[1];
454 public:
friend std::istream
459 _in.setf(std::ios_base::skipws);
friend Vector2< T > operator-(const T _s, const Vector2< T > &_v)
Subtraction operators.
Definition: Vector2.hh:227
const Vector2 & operator+=(const Vector2 &_v)
Addition assignment operator.
Definition: Vector2.hh:150
Vector2 operator-(const Vector2 &_v) const
Subtraction operator.
Definition: Vector2.hh:198
const Vector2 & operator*=(const Vector2 &_v)
Multiplication assignment operator.
Definition: Vector2.hh:296
void Set(T _x, T _y)
Set the contents of the vector.
Definition: Vector2.hh:103
T & X()
Return a mutable x value.
Definition: Vector2.hh:401
const Vector2< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector2.hh:180
T Y() const
Return the y value.
Definition: Vector2.hh:393
friend Vector2< T > operator+(const T _s, const Vector2< T > &_v)
Addition operators.
Definition: Vector2.hh:171
T X() const
Return the x value.
Definition: Vector2.hh:385
const Vector2< T > & operator-=(T _s)
Subtraction assignment operator.
Definition: Vector2.hh:236
Two dimensional (x, y) vector.
Definition: Vector2.hh:29
const Vector2 operator/(const Vector2 &_v) const
Division operator.
Definition: Vector2.hh:248
const Vector2 operator*(T _v) const
Multiplication operators.
Definition: Vector2.hh:307
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector2.hh:75
Vector2< float > Vector2f
Definition: Vector2.hh:477
friend std::istream & operator>>(std::istream &_in, Vector2< T > &_pt)
Stream extraction operator.
Definition: Vector2.hh:455
T operator[](size_t _index) const
Array subscript operator.
Definition: Vector2.hh:375
const Vector2 & operator/=(T _v)
Division operator.
Definition: Vector2.hh:276
double Distance(const Vector2 &_pt) const
Calc distance to the given point.
Definition: Vector2.hh:67
Vector2 & operator=(const Vector2 &_v)
Assignment operator.
Definition: Vector2.hh:120
const Vector2 & operator/=(const Vector2 &_v)
Division operator.
Definition: Vector2.hh:257
T & Y()
Return a mutable y value.
Definition: Vector2.hh:409
const Vector2 operator/(T _v) const
Division operator.
Definition: Vector2.hh:268
bool Equal(const Vector2 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector2.hh:338
Vector2()
Default Constructor.
Definition: Vector2.hh:38
void Y(const T &_v)
Set the y value.
Definition: Vector2.hh:423
Vector2 operator+(const Vector2 &_v) const
Addition operator.
Definition: Vector2.hh:142
static const Vector2< T > Zero
math::Vector2(0, 0)
Definition: Vector2.hh:32
T Dot(const Vector2< T > &_v) const
Get the dot product of this vector and _v.
Definition: Vector2.hh:112
bool operator!=(const Vector2 &_v) const
Not equal to operator.
Definition: Vector2.hh:355
const Vector2 & operator=(T _v)
Assignment operator.
Definition: Vector2.hh:131
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:37
Vector2< int > Vector2i
Definition: Vector2.hh:475
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector2.hh:362
void X(const T &_v)
Set the x value.
Definition: Vector2.hh:416
bool operator==(const Vector2 &_v) const
Equal to operator.
Definition: Vector2.hh:348
const Vector2 & operator-=(const Vector2 &_v)
Subtraction assignment operator.
Definition: Vector2.hh:206
void Normalize()
Normalize the vector length.
Definition: Vector2.hh:89
Vector2< T > operator+(const T _s) const
Addition operators.
Definition: Vector2.hh:161
virtual ~Vector2()
Destructor.
Definition: Vector2.hh:62
Vector2(const Vector2< T > &_v)
Copy constructor.
Definition: Vector2.hh:55
Vector2< double > Vector2d
Definition: Vector2.hh:476
Vector2(const T &_x, const T &_y)
Constructor.
Definition: Vector2.hh:47
static const Vector2< T > One
math::Vector2(1, 1)
Definition: Vector2.hh:35
Definition: AffineException.hh:30
const Vector2 operator*(const Vector2 &_v) const
Multiplication operators.
Definition: Vector2.hh:287
const Vector2 & operator*=(T _v)
Multiplication assignment operator.
Definition: Vector2.hh:325
T SquaredLength() const
Returns the square of the length (magnitude) of the vector.
Definition: Vector2.hh:82
Vector2 operator-() const
Negation operator.
Definition: Vector2.hh:190
Vector2< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector2.hh:217
friend const Vector2 operator*(const T _s, const Vector2 &_v)
Scalar left multiplication operators.
Definition: Vector2.hh:316