00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef koPoint_h
00022 #define koPoint_h
00023
00024 #include <math.h>
00025
00026 #include <QVector>
00027 #include <QMatrix>
00028
00029
00037 class KoPoint {
00038
00039 public:
00040
00041 KoPoint() { m_x = 0; m_y = 0; }
00042 KoPoint(const double &x, const double &y) : m_x(x), m_y(y) {}
00043 KoPoint(const QPoint & p) : m_x(p.x()), m_y(p.y()) {}
00044 KoPoint(const QPointF& pt) : m_x(pt.x()), m_y(pt.y()) { }
00045
00046 ~KoPoint() {}
00047
00048 bool operator==(const KoPoint &rhs) const { return QABS(m_x-rhs.x()) < 1E-10 && QABS(m_y-rhs.y()) < 1E-10; }
00049 bool operator!=(const KoPoint &rhs) const { return QABS(m_x-rhs.x()) > 1E-10 || QABS(m_y-rhs.y()) > 1E-10; }
00050
00051 bool isNull() const { return m_x == 0 && m_y == 0; }
00052
00053 double x() const { return m_x; }
00054 double y() const { return m_y; }
00055 void setX(const double &x) { m_x = x; }
00056 void setY(const double &y) { m_y = y; }
00057
00058 double &rx() { return m_x; }
00059 double &ry() { return m_y; }
00060
00061 KoPoint &operator=(const KoPoint &rhs) { m_x = rhs.x(); m_y = rhs.y(); return *this; }
00062 KoPoint &operator+=( const KoPoint &rhs ) { m_x += rhs.x(); m_y += rhs.y(); return *this; }
00063 KoPoint &operator-=( const KoPoint &rhs ) { m_x -= rhs.x(); m_y -= rhs.y(); return *this; }
00064 KoPoint &operator*=( const double &c ) { m_x *= c; m_y *= c; return *this; }
00065
00066 friend inline KoPoint operator+( const KoPoint &, const KoPoint & );
00067 friend inline KoPoint operator-( const KoPoint &, const KoPoint & );
00068 friend inline KoPoint operator*( const KoPoint &, const double & );
00069 friend inline KoPoint operator*( const double &, const KoPoint & );
00070 friend inline double operator*( const KoPoint &a, const KoPoint &b );
00071
00072
00073 void setCoords(const double &x, const double &y) { m_x = x; m_y = y; }
00074
00075 int floorX() const { return static_cast<int>(x()); }
00076 int floorY() const { return static_cast<int>(y()); }
00077 int roundX() const { return qRound(x()); }
00078 int roundY() const { return qRound(y()); }
00079
00080 QPoint floorQPoint() const { return QPoint(static_cast<int>(x()), static_cast<int>(y())); }
00081 QPoint roundQPoint() const { return QPoint(qRound(x()), qRound(y())); }
00082
00083 KoPoint transform (const QMatrix &m) const
00084 {
00085 double x, y;
00086 m.map(m_x, m_y, &x, &y);
00087 return KoPoint(x, y);
00088 };
00089
00090 bool isNear(const KoPoint &p, double range) const
00091 {
00092 return (p.x() >= m_x - range && p.x() <= m_x + range && p.y() >= m_y - range && p.y() <= m_y + range);
00093 }
00094
00095 static double getAngle( const KoPoint& p1, const KoPoint& p2 ) {
00096 double a = atan2( p2.x() - p1.x(), p2.y() - p1.y() ) + M_PI;
00097 return ( ( - ( a * 360 ) / ( 2 * M_PI ) - 90 ) - 180 );
00098 }
00099
00100
00101
00102 double manhattanLength() const
00103 {
00104 return QABS( m_x ) + QABS( m_y );
00105 }
00106
00108 QPoint toQPoint() const
00109 {
00110 return QPoint( qRound( m_x ), qRound( m_y ) );
00111 }
00112
00114 QPointF toPointF() const { return QPointF(x(),y()); }
00115
00116 private:
00117 double m_x, m_y;
00118 };
00119
00120 inline KoPoint operator+( const KoPoint &p1, const KoPoint &p2 )
00121 { return KoPoint( p1.m_x+p2.m_x, p1.m_y+p2.m_y ); }
00122
00123 inline KoPoint operator-( const KoPoint &p1, const KoPoint &p2 )
00124 { return KoPoint( p1.m_x-p2.m_x, p1.m_y-p2.m_y ); }
00125
00126 inline KoPoint operator*( const KoPoint &p, const double &c )
00127 { return KoPoint( p.m_x*c, p.m_y*c ); }
00128
00129 inline KoPoint operator*( const double &c, const KoPoint &p )
00130 { return KoPoint( p.m_x*c, p.m_y*c ); }
00131
00132 inline double operator*( const KoPoint &a, const KoPoint &b )
00133 { return a.m_x * b.m_x + a.m_y * b.m_y; }
00134
00135
00136
00137
00138
00139 #include <kdebug.h>
00140
00142 #define DEBUGDOUBLE(d) QString::number( (d), 'g', 20 )
00143
00144 inline kdbgstream operator<<( kdbgstream str, const KoPoint & r ) {
00145
00146 str << "(" << r.x() << ", " << r.y() << ")";
00147 return str;
00148 }
00149
00150 inline kndbgstream operator<<( kndbgstream str, const KoPoint & ) { return str; }
00151
00152 typedef QVector<KoPoint> vKoPoint;
00153
00154 #endif