koPoint.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef koPoint_h
00021 #define koPoint_h
00022
00023 #include <qwmatrix.h>
00024 #include <math.h>
00025
00030 class KoPoint {
00031
00032 public:
00033 KoPoint() { m_x = 0; m_y = 0; }
00034 KoPoint(const double &x, const double &y) : m_x(x), m_y(y) {}
00035 KoPoint(const QPoint & p) : m_x(p.x()), m_y(p.y()) {}
00036 ~KoPoint() {}
00037
00038 bool operator==(const KoPoint &rhs) const { return QABS(m_x-rhs.x()) < 1E-10 && QABS(m_y-rhs.y()) < 1E-10; }
00039 bool operator!=(const KoPoint &rhs) const { return QABS(m_x-rhs.x()) > 1E-10 || QABS(m_y-rhs.y()) > 1E-10; }
00040
00041 bool isNull() const { return m_x == 0 && m_y == 0; }
00042
00043 double x() const { return m_x; }
00044 double y() const { return m_y; }
00045 void setX(const double &x) { m_x = x; }
00046 void setY(const double &y) { m_y = y; }
00047
00048 double &rx() { return m_x; }
00049 double &ry() { return m_y; }
00050
00051 KoPoint &operator=(const KoPoint &rhs) { m_x = rhs.x(); m_y = rhs.y(); return *this; }
00052 KoPoint &operator+=( const KoPoint &rhs ) { m_x += rhs.x(); m_y += rhs.y(); return *this; }
00053 KoPoint &operator-=( const KoPoint &rhs ) { m_x -= rhs.x(); m_y -= rhs.y(); return *this; }
00054 KoPoint &operator*=( const double &c ) { m_x *= c; m_y *= c; return *this; }
00055
00056 friend inline KoPoint operator+( const KoPoint &, const KoPoint & );
00057 friend inline KoPoint operator-( const KoPoint &, const KoPoint & );
00058 friend inline KoPoint operator*( const KoPoint &, const double & );
00059 friend inline KoPoint operator*( const double &, const KoPoint & );
00060 friend inline double operator*( const KoPoint &a, const KoPoint &b );
00061
00062
00063 void setCoords(const double &x, const double &y) { m_x = x; m_y = y; }
00064 KoPoint transform (const QWMatrix &m) const
00065 {
00066 double x, y;
00067 m.map(m_x, m_y, &x, &y);
00068 return KoPoint(x, y);
00069 };
00070
00071 bool isNear(const KoPoint &p, double range) const
00072 {
00073 return (p.x() >= m_x - range && p.x() <= m_x + range && p.y() >= m_y - range && p.y() <= m_y + range);
00074 }
00075
00076 static double getAngle( const KoPoint& p1, const KoPoint& p2 ) {
00077 double a = atan2( p2.x() - p1.x(), p2.y() - p1.y() ) + M_PI;
00078 return ( ( - ( a * 360 ) / ( 2 * M_PI ) - 90 ) - 180 );
00079 }
00080
00081 double manhattanLength() const
00082 {
00083 return QABS( m_x ) + QABS( m_y );
00084 }
00085
00086 private:
00087 double m_x, m_y;
00088 };
00089
00090 inline KoPoint operator+( const KoPoint &p1, const KoPoint &p2 )
00091 { return KoPoint( p1.m_x+p2.m_x, p1.m_y+p2.m_y ); }
00092
00093 inline KoPoint operator-( const KoPoint &p1, const KoPoint &p2 )
00094 { return KoPoint( p1.m_x-p2.m_x, p1.m_y-p2.m_y ); }
00095
00096 inline KoPoint operator*( const KoPoint &p, const double &c )
00097 { return KoPoint( p.m_x*c, p.m_y*c ); }
00098
00099 inline KoPoint operator*( const double &c, const KoPoint &p )
00100 { return KoPoint( p.m_x*c, p.m_y*c ); }
00101
00102 inline double operator*( const KoPoint &a, const KoPoint &b )
00103 { return a.m_x * b.m_x + a.m_y * b.m_y; }
00104
00105
00106
00107
00108
00109 #include <kdebug.h>
00110
00112 #define DEBUGDOUBLE(d) QString::number( (d), 'g', 20 )
00113
00114 inline kdbgstream operator<<( kdbgstream str, const KoPoint & r ) {
00115
00116 str << "(" << r.x() << ", " << r.y() << ")";
00117 return str;
00118 }
00119
00120 inline kndbgstream operator<<( kndbgstream str, const KoPoint & ) { return str; }
00121
00122 #endif
This file is part of the documentation for lib Library Version 1.3.5.