QGIS API Documentation  2.12.0-Lyon
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 *
12  * it under the terms of the GNU General Public License as published by *
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 degrees 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
85  QgsPoint( const QPointF& point )
86  : m_x( point.x() ), m_y( point.y() )
87  {}
88
93  QgsPoint( const QPoint& point )
94  : m_x( point.x() ), m_y( point.y() )
95  {}
96
98  {}
99
103  void setX( double x )
104  {
105  m_x = x;
106  }
107
111  void setY( double y )
112  {
113  m_y = y;
114  }
115
117  void set( double x, double y )
118  {
119  m_x = x;
120  m_y = y;
121  }
122
126  double x() const
127  {
128  return m_x;
129  }
130
134  double y() const
135  {
136  return m_y;
137  }
138
143  QPointF toQPointF() const;
144
146  QString toString() const;
147
149  QString toString( int thePrecision ) const;
150
160  QString toDegreesMinutesSeconds( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
161
171  QString toDegreesMinutes( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
172
173
178  QString wellKnownText() const;
179
181  double sqrDist( double x, double y ) const;
182
184  double sqrDist( const QgsPoint& other ) const;
185
187  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
188
190  double azimuth( const QgsPoint& other );
191
198  bool compare( const QgsPoint &other, double epsilon = 4 * DBL_EPSILON ) const;
199
201  bool operator==( const QgsPoint &other );
202
204  bool operator!=( const QgsPoint &other ) const;
205
207  void multiply( const double& scalar );
208
213  int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
214
216  QgsPoint & operator=( const QgsPoint &other );
217
218  QgsVector operator-( const QgsPoint& p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
219  QgsPoint &operator+=( const QgsVector &v ) { *this = *this + v; return *this; }
220  QgsPoint &operator-=( const QgsVector &v ) { *this = *this - v; return *this; }
221  QgsPoint operator+( const QgsVector &v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
222  QgsPoint operator-( const QgsVector &v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
223
224  private:
225
227  double m_x;
228
230  double m_y;
231
232  friend uint qHash( const QgsPoint& pnt );
233
234 }; // class QgsPoint
235
236
237 inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )
238 {
239  if (( p1.x() == p2.x() ) && ( p1.y() == p2.y() ) )
240  { return true; }
241  else
242  { return false; }
243 }
244
245 inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
246 {
247  // Use Local8Bit for printouts
248  os << p.toString().toLocal8Bit().data();
249  return os;
250 }
251
252 inline uint qHash( const QgsPoint& p )
253 {
254  uint hash;
255  uint h1 = qHash(( quint64 )p.m_x );
256  uint h2 = qHash(( quint64 )p.m_y );
257  hash = h1 ^( h2 << 1 );
258  return hash;
259 }
260
261 #endif //QGSPOINT_H
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsPoint & operator-=(const QgsVector &v)
Definition: qgspoint.h:220
double y() const
Definition: qgspoint.cpp:69
QgsVector operator-(const QgsPoint &p) const
Definition: qgspoint.h:218
double x() const
Get the x value of the point.
Definition: qgspoint.h:126
QgsPoint operator+(const QgsVector &v) const
Definition: qgspoint.h:221
QgsPoint()
Default constructor.
Definition: qgspoint.h:67
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:365
QgsPoint(const QPoint &point)
Create a point from a QPoint.
Definition: qgspoint.h:93
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:126
void set(double x, double y)
Sets the x and y value of the point.
Definition: qgspoint.h:117
A class to represent a point.
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)
QByteArray toLocal8Bit() const
A class to represent a vector.
Definition: qgspoint.h:32
void setX(double x)
Sets the x value of the point.
Definition: qgspoint.h:103
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:111
std::ostream & operator<<(std::ostream &os, const QgsPoint &p)
Definition: qgspoint.h:245
QgsPoint(const QPointF &point)
Create a point from a QPointF.
Definition: qgspoint.h:85
~QgsPoint()
Definition: qgspoint.h:97
QgsPoint operator-(const QgsVector &v) const
Definition: qgspoint.h:222
QgsPoint(double x, double y)
Create a point from x,y coordinates.
Definition: qgspoint.h:77
char * data()
double y() const
Get the y value of the point.
Definition: qgspoint.h:134
double x() const
Definition: qgspoint.cpp:64
QgsPoint & operator+=(const QgsVector &v)
Definition: qgspoint.h:219
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:252
bool operator==(const QgsPoint &p1, const QgsPoint &p2)
Definition: qgspoint.h:237