#ifndef __JVECTOR2D__ #define __JVECTOR2D__ #include #include "JIO/JSerialisable.hh" namespace JGEOMETRY2D { namespace { using JIO::JReader; using JIO::JWriter; } /** * Data structure for vector in two dimensions. */ class JVector2D { public: /** * Default constructor. */ JVector2D() : __x(0.0), __y(0.0) {} /** * Constructor. * * \param x x value * \param y y value */ JVector2D(const double x, const double y) : __x(x), __y(y) {} /** * Read JVector2D from input. * * \param in JReader * \param vector JVector2D * \return JReader */ friend inline JReader& operator>>(JReader& in, JVector2D& vector) { in >> vector.__x; in >> vector.__y; return in; } /** * Write JVector2D to output. * * \param out JWriter * \param vector JVector2D * \return JWriter */ friend inline JWriter& operator<<(JWriter& out, const JVector2D& vector) { out << vector.__x; out << vector.__y; return out; } /** * Get x position. * * \return x position */ double getX() const { return __x; } /** * Get y position. * * \return y position */ double getY() const { return __y; } /** * Add vector. * * \param vector * \return this vector */ JVector2D& add(const JVector2D& vector) { __x += vector.getX(); __y += vector.getY(); return *this; } /** * Subtract vector. * * \param vector * \return this vector */ JVector2D& sub(const JVector2D& vector) { __x -= vector.getX(); __y -= vector.getY(); return *this; } /** * Scale vector. * * \param factor scale factor * \return this vector */ JVector2D& scale(const double factor) { __x *= factor; __y *= factor; return *this; } /** * Negate vector. * * \return this vector */ JVector2D& negate() { __x = -__x; __y = -__y; return *this; } /** * Add vector. * * \param vector * \return this vector */ JVector2D& operator+=(const JVector2D& vector) { __x += vector.getX(); __y += vector.getY(); return *this; } /** * Subtract vector. * * \param vector * \return this vector */ JVector2D& operator-=(const JVector2D& vector) { __x -= vector.getX(); __y -= vector.getY(); return *this; } /** * Scale vector. * * \param factor scale factor * \return this vector */ JVector2D& operator*=(const double factor) { __x *= factor; __y *= factor; return *this; } /** * Scale vector. * * \param factor scale factor * \return this vector */ JVector2D& operator/=(const double factor) { __x /= factor; __y /= factor; return *this; } /** * Get length squared. * * \return square of length */ double getLengthSquared() const { return getX()*getX() + getY()*getY(); } /** * Get length. * * \return length */ double getLength() const { return sqrt(getLengthSquared()); } protected: double __x; double __y; }; /** * Add operator. * * \param a JVector2D * \param b JVector2D * \result a + b */ inline JVector2D operator+(const JVector2D& a, const JVector2D& b) { return JVector2D(a).add(b); } /** * Subtract operator. * * \param a JVector2D * \param b JVector2D * \result a - b */ inline JVector2D operator-(const JVector2D& a, const JVector2D& b) { return JVector2D(a).sub(b); } /** * Multiply operator. * * \param a constant * \param b JVector2D * \result a * b */ inline JVector2D operator*(const double a, const JVector2D& b) { return JVector2D(b).scale(a); } /** * Divide operator. * * \param a JVector2D * \param b constant * \result a / b */ inline JVector2D operator/(const JVector2D& a, const double b) { return JVector2D(a).scale(1.0/b); } } #endif