aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/geo/Vector2D.h
blob: 0b18138c0bc64636bd927a395334672ab7912a1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef   Vector2D_H__
#define   Vector2D_H__

#include <cmath>
#include "../utility.h"

namespace meow{

  template<class Scalar>
  class Vector2D{
    private:
      Scalar _x, _y, _w;
    public:
      //
      Vector2D(): _x(0), _y(0), _w(0){ }
      Vector2D(Scalar const& x,
               Scalar const& y)  : _x(   x), _y(   y), _w(   0){ }
      Vector2D(Scalar const& x,
               Scalar const& y,
               Scalar const& w)  : _x(   x), _y(   y), _w(   w){ }
      Vector2D(Vector2D const& v): _x(v._x), _y(v._y), _w(v._w){ }
      //
      Scalar const& x() const{ return _x; }
      Scalar const& y() const{ return _y; }
      Scalar const& w() const{ return _w; }
      Scalar      & x(Scalar const& s){ _x = s; return _x; }
      Scalar      & y(Scalar const& s){ _y = s; return _y; }
      Scalar      & w(Scalar const& s){ _w = s; return _w; }
      Vector2D& operator()(Scalar const& x, Scalar const& y)                 { _x = x; _y = y;         return *this; }
      Vector2D& operator()(Scalar const& x, Scalar const& y, Scalar const& w){ _x = x; _y = y; _w = w; return *this; }
      Scalar&       operator[](size_t n)      { return (n == 0 ? _x : (n == 1 ? _y : _w)); }
      Scalar const& operator[](size_t n) const{ return (n == 0 ? _x : (n == 1 ? _y : _w)); }
      //
      Vector2D operator-() const{ return Vector2D(-_x, -_y, _w); }
      Vector2D operator~() const{ return Vector2D(-_y,  _x, _w); }
      //
      Vector2D operator+(Vector2D const& v) const{ return Vector2D(_x + v._x, _y + v._y, _w); }
      Vector2D operator-(Vector2D const& v) const{ return Vector2D(_x - v._x, _y - v._y, _w); }
      Vector2D operator*(Scalar   const& s) const{ return Vector2D(_x *    s, _y *    s, _w); }
      Vector2D operator/(Scalar   const& s) const{ return Vector2D(_x /    s, _y /    s, _w); }
      //
      Vector2D& operator+=(Vector2D const& v){ _x += v._x; _y += v._y; return *this; }
      Vector2D& operator-=(Vector2D const& v){ _x -= v._x; _y -= v._y; return *this; }
      Vector2D& operator*=(Scalar   const& s){ _x *=    s; _y *=    s; return *this; }
      Vector2D& operator/=(Scalar   const& s){ _x /=    s; _y /=    s; return *this; }
      //
      Scalar dot  (Vector2D const& v) const{ return _x * v._x + _y * v._y; }
      Scalar cross(Vector2D const& v) const{ return _x * v._y - _y * v._x; }
      Scalar length () const{ return sqrt(squ(_x) + squ(_y)); }
      Scalar length2() const{ return      squ(_x) + squ(_y) ; }
      //
      Vector2D normal() const{
        Scalar len(length());
        return Vector2D(_x / len, _y / len, _w);
      }
      Vector2D rotation(Scalar theta) const{
        Vector2D i(cos(-theta), sin(-theta));
        return Vector2D(i.dot(*this), i.cross(*this), _w);
      }
      Vector2D reflection(Vector2D const& v) const{
        return v * v.dot(*this) * 2 / v.length2() - *this;
      }
  };

}

#endif // Vector2D_H__