aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/geo/Vector2D.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/geo/Vector2D.h')
-rw-r--r--meowpp/geo/Vector2D.h67
1 files changed, 67 insertions, 0 deletions
diff --git a/meowpp/geo/Vector2D.h b/meowpp/geo/Vector2D.h
new file mode 100644
index 0000000..0b18138
--- /dev/null
+++ b/meowpp/geo/Vector2D.h
@@ -0,0 +1,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__