QGIS API Documentation  2.4.0-Chugiak
qgspoint.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspoint.h - description
3  -------------------
4  begin : Sat Jun 22 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
7  ***************************************************************************/
8
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17
18 #ifndef QGSPOINT_H
19 #define QGSPOINT_H
20
21 #include <qgis.h>
22
23 #include <iostream>
24 #include <QString>
25 #include <QPoint>
26
32 class CORE_EXPORT QgsVector
33 {
34  double m_x, m_y;
35
36  public:
37  QgsVector();
38  QgsVector( double x, double y );
39
40  QgsVector operator-( void ) const;
41  QgsVector operator*( double scalar ) const;
42  QgsVector operator/( double scalar ) const;
43  double operator*( QgsVector v ) const;
44  double length() const;
45
46  double x() const;
47  double y() const;
48
49  // perpendicular vector (rotated 90° counter-clockwise)
50  QgsVector perpVector() const;
51
52  double angle( void ) const;
53  double angle( QgsVector v ) const;
54  QgsVector rotateBy( double rot ) const;
55  QgsVector normal() const;
56
57 };
58
63 class CORE_EXPORT QgsPoint
64 {
65  public:
67  QgsPoint() : m_x( 0.0 ), m_y( 0.0 )
68  {}
69
71  QgsPoint( const QgsPoint& p );
72
77  QgsPoint( double x, double y )
78  : m_x( x ), m_y( y )
79  {}
80
82  {}
83
87  void setX( double x )
88  {
89  m_x = x;
90  }
91
95  void setY( double y )
96  {
97  m_y = y;
98  }
99
101  void set( double x, double y )
102  {
103  m_x = x;
104  m_y = y;
105  }
106
110  double x() const
111  {
112  return m_x;
113  }
114
118  double y() const
119  {
120  return m_y;
121  }
122
124  QString toString() const;
125
127  QString toString( int thePrecision ) const;
128
134  QString toDegreesMinutesSeconds( int thePrecision ) const;
135
141  QString toDegreesMinutes( int thePrecision ) const;
142
143
148  QString wellKnownText() const;
149
151  double sqrDist( double x, double y ) const;
152
154  double sqrDist( const QgsPoint& other ) const;
155
158  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
159
162  double azimuth( const QgsPoint& other );
163
165  bool operator==( const QgsPoint &other );
166
168  bool operator!=( const QgsPoint &other ) const;
169
171  void multiply( const double& scalar );
172
177  int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
178
180  QgsPoint & operator=( const QgsPoint &other );
181
182  QgsVector operator-( QgsPoint p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
183  QgsPoint &operator+=( const QgsVector &v ) { *this = *this + v; return *this; }
184  QgsPoint &operator-=( const QgsVector &v ) { *this = *this - v; return *this; }
185  QgsPoint operator+( const QgsVector &v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
186  QgsPoint operator-( const QgsVector &v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
187
188  private:
189
191  double m_x;
192
194  double m_y;
195
196  friend uint qHash( const QgsPoint& pnt );
197
198 }; // class QgsPoint
199
200
201 inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )
202 {
203  if (( p1.x() == p2.x() ) && ( p1.y() == p2.y() ) )
204  { return true; }
205  else
206  { return false; }
207 }
208
209 inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
210 {
211  // Use Local8Bit for printouts
212  os << p.toString().toLocal8Bit().data();
213  return os;
214 }
215
216 inline uint qHash( const QgsPoint& p )
217 {
218  uint hash;
219  uint h1 = qHash(( quint64 )p.m_x );
220  uint h2 = qHash(( quint64 )p.m_y );
221  hash = h1 ^( h2 << 1 );
222  return hash;
223 }
224
225 #endif //QGSPOINT_H
QgsVector operator-(QgsPoint p) const
Definition: qgspoint.h:182
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double m_y
y coordinate
Definition: qgspoint.h:194
QgsPoint & operator-=(const QgsVector &v)
Definition: qgspoint.h:184
double y() const
Definition: qgspoint.cpp:69
double x() const
Definition: qgspoint.h:110
QgsPoint operator+(const QgsVector &v) const
Definition: qgspoint.h:185
QgsPoint()
Default constructor.
Definition: qgspoint.h:67
double m_x
x coordinate
Definition: qgspoint.h:191
const double DEFAULT_SEGMENT_EPSILON
default snapping tolerance for segments (
Definition: qgis.h:414
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:121
void set(double x, double y)
Definition: qgspoint.h:101
A class to represent a point geometry.
Definition: qgspoint.h:63
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
A class to represent a vector.
Definition: qgspoint.h:32
void setX(double x)
Definition: qgspoint.h:87
void setY(double y)
Definition: qgspoint.h:95
std::ostream & operator<<(std::ostream &os, const QgsPoint &p)
Definition: qgspoint.h:209
~QgsPoint()
Definition: qgspoint.h:81
QgsPoint operator-(const QgsVector &v) const
Definition: qgspoint.h:186
QgsPoint(double x, double y)
Definition: qgspoint.h:77
double y() const
Definition: qgspoint.h:118
double x() const
Definition: qgspoint.cpp:64
QgsPoint & operator+=(const QgsVector &v)
Definition: qgspoint.h:183
double m_y
Definition: qgspoint.h:34
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:216
bool operator==(const QgsPoint &p1, const QgsPoint &p2)
Definition: qgspoint.h:201