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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#ifndef MATHTOOLS_H
#define MATHTOOLS_H
#include <cmath>
//----------------------------------------------------------------------------
/**
* This namespace contains some common needed functions
* related to mathematical and geometric operations.
*/
namespace MATH_TOOLS
{
//------------------------------------------------------------------------
class Vector
{
public:
//--------------------------------------------------------------------
Vector();
Vector(double x, double y);
~Vector();
//--------------------------------------------------------------------
inline void set(double x, double y) { m_x = x; m_y = y; }
inline void setX(double x) { m_x = x; }
inline double getX() const { return m_x; }
inline void setY(double y) { m_y = y; }
inline double getY() const { return m_y; }
//--------------------------------------------------------------------
inline void flipX() { m_x = -m_x; }
inline void flipY() { m_y = -m_y; }
//--------------------------------------------------------------------
double getAbsValue() const;
double getAngle() const;
//--------------------------------------------------------------------
inline void add(double x, double y) { m_x += x; m_y += y; }
inline void addX(double x) { m_x += x; }
inline void addY(double y) { m_y += y; }
inline void sub(double x, double y) { m_x -= x; m_y -= y; }
inline void subX(double x) { m_x -= x; }
inline void subY(double y) { m_y -= y; }
//--------------------------------------------------------------------
Vector &setExponential(double absValue, double angle);
Vector &addExponential(double absValue, double angle);
//--------------------------------------------------------------------
Vector &operator+=(const Vector &other);
Vector &operator-=(const Vector &other);
Vector &operator*=(const double scale);
Vector &operator/=(const double scale);
//--------------------------------------------------------------------
friend Vector operator+(const Vector &v1, const Vector &v2);
friend Vector operator-(const Vector &v1, const Vector &v2);
private:
//--------------------------------------------------------------------
double m_x;
double m_y;
};
//------------------------------------------------------------------------
/**
* Returns the angle between (0,0)(x,y) and the y axis,
* counted in clockwise order and inside the value range from [0..360).
*/
double getAngle(double x, double y);
/**
* Returns the angle between (x1,y1)(x2,y2) and the y axis,
* counted in clockwise order and inside the value range from [0..360).
*/
inline double getAngle(double x1, double y1, double x2, double y2)
{
return getAngle(x2-x1, y2-y1);
}
inline int getAngle(int x, int y)
{
return (int)rint(getAngle(1.0*x, 1.0*y));
}
inline int getAngle(int x1, int y1, int x2, int y2)
{
return getAngle(x2-x1, y2-y1);
}
}
#endif //MATHTOOLS_H
|