lib Library API Documentation

koPoint.h

00001 /* This file is part of the KDE project 00002 Copyright (C) 2001 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 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 // Not in QPoint: 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 kdDebug support 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 // should this use DEBUGDOUBLE? 00116 str << "(" << r.x() << ", " << r.y() << ")"; 00117 return str; 00118 } 00119 00120 inline kndbgstream operator<<( kndbgstream str, const KoPoint & ) { return str; } 00121 00122 #endif
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 06:54:17 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003