lib

KoPoint.h
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef koPoint_h
21#define koPoint_h
22
23#include <tqwmatrix.h>
24#include <math.h>
25
30class KoPoint {
31
32public:
33 KoPoint() { m_x = 0; m_y = 0; }
34 KoPoint(const double &x, const double &y) : m_x(x), m_y(y) {}
35 explicit KoPoint(const TQPoint & p) : m_x(p.x()), m_y(p.y()) {}
36 ~KoPoint() {}
37
38 bool operator==(const KoPoint &rhs) const { return TQABS(m_x-rhs.x()) < 1E-10 && TQABS(m_y-rhs.y()) < 1E-10; }
39 bool operator!=(const KoPoint &rhs) const { return TQABS(m_x-rhs.x()) > 1E-10 || TQABS(m_y-rhs.y()) > 1E-10; }
40
41 bool isNull() const { return m_x == 0 && m_y == 0; }
42
43 double x() const { return m_x; }
44 double y() const { return m_y; }
45 void setX(const double &x) { m_x = x; }
46 void setY(const double &y) { m_y = y; }
47
48 double &rx() { return m_x; }
49 double &ry() { return m_y; }
50
51 KoPoint &operator=(const KoPoint &rhs) { m_x = rhs.x(); m_y = rhs.y(); return *this; }
52 KoPoint &operator+=( const KoPoint &rhs ) { m_x += rhs.x(); m_y += rhs.y(); return *this; }
53 KoPoint &operator-=( const KoPoint &rhs ) { m_x -= rhs.x(); m_y -= rhs.y(); return *this; }
54 KoPoint &operator*=( const double &c ) { m_x *= c; m_y *= c; return *this; }
55
56 friend inline KoPoint operator+( const KoPoint &, const KoPoint & );
57 friend inline KoPoint operator-( const KoPoint &, const KoPoint & );
58 friend inline KoPoint operator*( const KoPoint &, const double & );
59 friend inline KoPoint operator*( const double &, const KoPoint & );
60 friend inline double operator*( const KoPoint &a, const KoPoint &b );
61
62 // Not in TQPoint:
63 void setCoords(const double &x, const double &y) { m_x = x; m_y = y; }
64 KoPoint transform (const TQWMatrix &m) const
65 {
66 double x, y;
67 m.map(m_x, m_y, &x, &y);
68 return KoPoint(x, y);
69 };
70
71 bool isNear(const KoPoint &p, double range) const
72 {
73 return (p.x() >= m_x - range && p.x() <= m_x + range && p.y() >= m_y - range && p.y() <= m_y + range);
74 }
75
76 static double getAngle( const KoPoint& p1, const KoPoint& p2 ) {
77 double a = atan2( p2.x() - p1.x(), p2.y() - p1.y() ) + M_PI;
78 return ( ( - ( a * 360 ) / ( 2 * M_PI ) - 90 ) - 180 );
79 }
80
81 double manhattanLength() const
82 {
83 return TQABS( m_x ) + TQABS( m_y );
84 }
85
87 TQPoint toTQPoint() const
88 {
89 return TQPoint( tqRound( m_x ), tqRound( m_y ) );
90 }
91
92private:
93 double m_x, m_y;
94};
95
96inline KoPoint operator+( const KoPoint &p1, const KoPoint &p2 )
97{ return KoPoint( p1.m_x+p2.m_x, p1.m_y+p2.m_y ); }
98
99inline KoPoint operator-( const KoPoint &p1, const KoPoint &p2 )
100{ return KoPoint( p1.m_x-p2.m_x, p1.m_y-p2.m_y ); }
101
102inline KoPoint operator*( const KoPoint &p, const double &c )
103{ return KoPoint( p.m_x*c, p.m_y*c ); }
104
105inline KoPoint operator*( const double &c, const KoPoint &p )
106{ return KoPoint( p.m_x*c, p.m_y*c ); }
107
108inline double operator*( const KoPoint &a, const KoPoint &b )
109{ return a.m_x * b.m_x + a.m_y * b.m_y; }
110
111/******************************
112 kdDebug support
113*******************************/
114
115#include <kdebug.h>
116
118#define DEBUGDOUBLE(d) TQString::number( (d), 'g', 20 )
119
120inline kdbgstream operator<<( kdbgstream str, const KoPoint & r ) {
121 // should this use DEBUGDOUBLE?
122 str << "(" << r.x() << ", " << r.y() << ")";
123 return str;
124}
125
126inline kndbgstream operator<<( kndbgstream str, const KoPoint & ) { return str; }
127
128#endif
A point whose coordinates are floating-point values ( "double"s ).
Definition KoPoint.h:30
TQPoint toTQPoint() const
Convert to a TQPoint - with precision loss!
Definition KoPoint.h:87