#ifndef __JVERSOR2D__ #define __JVERSOR2D__ #include #include /** * \author mdejong */ namespace JGEOMETRY2D {} namespace JPP { using namespace JGEOMETRY2D; } namespace JGEOMETRY2D { /** * Data structure for normalised vector in two dimensions. */ class JVersor2D { public: /** * Default constructor. * This constructor yields a unit y-vector. */ JVersor2D() : __dx(0.0), __dy(1.0) {} /** * Constructor. * * \param dx dx value * \param dy dy value */ JVersor2D(const double dx, const double dy) : __dx(dx), __dy(dy) { normalise(); } /** * Get x direction. * * \return x direction */ double getDX() const { return __dx; } /** * Get y direction. * * \return y direction */ double getDY() const { return __dy; } /** * Get phi angle. * * \return phi angle [rad] */ double getPhi() const { return atan2(__dy, __dx); } /** * Negate versor. * * \return this versor */ JVersor2D& negate() { __dx = -__dx; __dy = -__dy; return *this; } /** * Check equality. * * \param versor versor * \param precision precision * \return true if versors are equal; else false */ bool equals(const JVersor2D& versor, const double precision = std::numeric_limits::min()) const { return (fabs(getDX() - versor.getDX()) <= precision && fabs(getDY() - versor.getDY()) <= precision); } /** * Get dot product. * * \param versor versor * \return dot product */ double getDot(const JVersor2D& versor) const { return getDX() * versor.getDX() + getDY() * versor.getDY(); } /** * Get perpendicular dot product. * * \param versor versor * \return perpendicular dot product */ double getPerpDot(const JVersor2D& versor) const { return getDX() * versor.getDY() - getDY() * versor.getDX(); } /** * Normalise versor. * This operation may set the result to the unit y-vector. * * \return this versor */ JVersor2D& normalise() { const double v = sqrt(getDX()*getDX() + getDY()*getDY()); if (v != 0.0) { __dx /= v; __dy /= v; } else { __dx = 0.0; __dy = 1.0; } return *this; } protected: double __dx; double __dy; }; static const JVersor2D JVersor2X_t(1,0); //!< unit x-vector static const JVersor2D JVersor2Y_t(0,1); //!< unit y-vector } #endif