#ifndef __JVECTOR2D__ #define __JVECTOR2D__ #include #include #include #include "JMath/JMath.hh" #include "JGeometry2D/JMatrix2D.hh" /** * \author mdejong */ namespace JGEOMETRY2D {} namespace JPP { using namespace JGEOMETRY2D; } namespace JGEOMETRY2D { using JMATH::JMath; typedef std::pair JRangeX; //!< Type definition of range along x-axis. typedef std::pair JRangeY; //!< Type definition of range along y-axis. /** * Data structure for vector in two dimensions. * * This class implements the JMATH::JMath interface. */ class JVector2D : public JMath { 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) {} /** * Get x position. * * \return x position */ double getX() const { return __x; } /** * Get y position. * * \return y position */ double getY() const { return __y; } /** * Negate vector. * * \return this vector */ JVector2D& negate() { __x = -__x; __y = -__y; return *this; } /** * Add vector. * * \param vector vector * \return this vector */ JVector2D& add(const JVector2D& vector) { __x += vector.getX(); __y += vector.getY(); return *this; } /** * Subtract vector. * * \param vector vector * \return this vector */ JVector2D& sub(const JVector2D& vector) { __x -= vector.getX(); __y -= vector.getY(); return *this; } /** * Scale vector. * * \param factor multiplication factor * \return this vector */ JVector2D& mul(const double factor) { __x *= factor; __y *= factor; return *this; } /** * Scale vector. * * \param factor division factor * \return this vector */ JVector2D& div(const double factor) { __x /= factor; __y /= factor; return *this; } /** * Transform. * * \param T matrix * \return this vector */ JVector2D& transform(const JMatrix2D& T) { T.transform(__x, __y); return *this; } /** * Check equality. * * \param vector vector * \param precision precision * \return true if vectors are equal; else false */ bool equals(const JVector2D& vector, const double precision = std::numeric_limits::min()) const { return (fabs(getX() - vector.getX()) <= precision && fabs(getY() - vector.getY()) <= precision); } /** * 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()); } /** * Get squared of distance to point. * * \param point point * \return square of distance */ double getDistanceSquared(const JVector2D& point) const { return JVector2D(point).sub(*this).getLengthSquared(); } /** * Get distance to point. * * \param point point * \return distance */ double getDistance(const JVector2D& point) const { return sqrt(getDistanceSquared(point)); } /** * Get dot product. * * \param point vector * \return dot product */ double getDot(const JVector2D& point) const { return getX() * point.getX() + getY() * point.getY(); } /** * Get perpendicular dot product. * * \param point vector * \return perpendicular dot product */ double getPerpDot(const JVector2D& point) const { return getX() * point.getY() - getY() * point.getX(); } protected: double __x; double __y; }; /** * Check sequence of three points. * * \param a 1st point * \param b 2nd point * \param c 3rd point * \return true if points a, b, c are counter clockwise; else false */ inline bool getCCW(const JVector2D& a, const JVector2D& b, const JVector2D& c) { const double A = a.getX() - b.getX(); const double B = a.getY() - b.getY(); const double C = c.getX() - b.getX(); const double D = c.getY() - b.getY(); return (A*D - B*C) <= 0.0; } } #endif