F:/KPlato/koffice/libs/kofficecore/KoPoint.h

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 David Faure <faure@kde.org>
00003    Copyright (c) 2004 Adrian Page <adrian@pagenet.plus.com>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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     // Not in QPoint:
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     // In QPoint, but not used
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   kDebug support
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     // should this use DEBUGDOUBLE?
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

Généré le Wed Nov 22 23:41:03 2006 pour KPlato par  doxygen 1.5.1-p1