QGIS API Documentation  2.99.0-Master (cb63e82)
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_core.h"
22 #include "qgsvector.h"
23 
24 #include "qgis.h"
25 
26 #include <iostream>
27 #include <QString>
28 #include <QPoint>
29 #include <QObject>
30 
31 class QgsPointV2;
32 
37 class CORE_EXPORT QgsPoint
38 {
39  Q_GADGET
40 
41  Q_PROPERTY( double x READ x WRITE setX )
42  Q_PROPERTY( double y READ y WRITE setY )
43 
44  public:
47  : mX( 0.0 )
48  , mY( 0.0 )
49  {}
50 
52  QgsPoint( const QgsPoint &p );
53 
58  QgsPoint( double x, double y )
59  : mX( x )
60  , mY( y )
61  {}
62 
67  QgsPoint( QPointF point )
68  : mX( point.x() )
69  , mY( point.y() )
70  {}
71 
76  QgsPoint( QPoint point )
77  : mX( point.x() )
78  , mY( point.y() )
79  {}
80 
87  QgsPoint( const QgsPointV2 &point );
88 
90  {}
91 
95  void setX( double x )
96  {
97  mX = x;
98  }
99 
103  void setY( double y )
104  {
105  mY = y;
106  }
107 
109  void set( double x, double y )
110  {
111  mX = x;
112  mY = y;
113  }
114 
118  double x() const
119  {
120  return mX;
121  }
122 
126  double y() const
127  {
128  return mY;
129  }
130 
135  QPointF toQPointF() const;
136 
138  QString toString() const;
139 
141  QString toString( int precision ) const;
142 
152  QString toDegreesMinutesSeconds( int precision, const bool useSuffix = true, const bool padded = false ) const;
153 
163  QString toDegreesMinutes( int precision, const bool useSuffix = true, const bool padded = false ) const;
164 
165 
170  QString wellKnownText() const;
171 
175  double sqrDist( double x, double y ) const;
176 
180  double sqrDist( const QgsPoint &other ) const;
181 
188  double distance( double x, double y ) const;
189 
195  double distance( const QgsPoint &other ) const;
196 
198  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint &minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
199 
201  double azimuth( const QgsPoint &other ) const;
202 
209  QgsPoint project( double distance, double bearing ) const;
210 
217  bool compare( const QgsPoint &other, double epsilon = 4 * DBL_EPSILON ) const;
218 
220  bool operator==( const QgsPoint &other );
221 
223  bool operator!=( const QgsPoint &other ) const;
224 
226  void multiply( double scalar );
227 
232  int onSegment( const QgsPoint &a, const QgsPoint &b ) const;
233 
235  QgsPoint &operator=( const QgsPoint &other );
236 
238  QgsVector operator-( const QgsPoint &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
239 
241  QgsPoint &operator+=( QgsVector v ) { *this = *this + v; return *this; }
242 
244  QgsPoint &operator-=( QgsVector v ) { *this = *this - v; return *this; }
245 
247  QgsPoint operator+( QgsVector v ) const { return QgsPoint( mX + v.x(), mY + v.y() ); }
248 
250  QgsPoint operator-( QgsVector v ) const { return QgsPoint( mX - v.x(), mY - v.y() ); }
251 
253  QgsPoint operator*( double scalar ) const { return QgsPoint( mX * scalar, mY * scalar ); }
254 
256  QgsPoint operator/( double scalar ) const { return QgsPoint( mX / scalar, mY / scalar ); }
257 
259  QgsPoint &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
260 
262  QgsPoint &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
263 
264  private:
265 
267  double mX;
268 
270  double mY;
271 
272  friend uint qHash( const QgsPoint &pnt );
273 
274 }; // class QgsPoint
275 
276 
277 inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )
278 {
279  if ( qgsDoubleNear( p1.x(), p2.x() ) && qgsDoubleNear( p1.y(), p2.y() ) )
280  { return true; }
281  else
282  { return false; }
283 }
284 
285 inline std::ostream &operator << ( std::ostream &os, const QgsPoint &p )
286 {
287  // Use Local8Bit for printouts
288  os << p.toString().toLocal8Bit().data();
289  return os;
290 }
291 
292 inline uint qHash( const QgsPoint &p )
293 {
294  uint hash;
295  uint h1 = qHash( static_cast< quint64 >( p.mX ) );
296  uint h2 = qHash( static_cast< quint64 >( p.mY ) );
297  hash = h1 ^ ( h2 << 1 );
298  return hash;
299 }
300 
301 #endif //QGSPOINT_H
double y
Definition: qgspoint.h:42
QgsVector operator-(const QgsPoint &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspoint.h:238
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:201
double y() const
Get the y value of the point.
Definition: qgspoint.h:126
QgsPoint(QPointF point)
Create a point from a QPointF.
Definition: qgspoint.h:67
QgsPoint & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspoint.h:244
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:342
QgsPoint operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:256
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:36
QgsPoint operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspoint.h:247
QgsPoint & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:259
A class to represent a point.
Definition: qgspoint.h:37
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:45
QgsPoint & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:262
A class to represent a vector.
Definition: qgsvector.h:26
void setX(double x)
Sets the x value of the point.
Definition: qgspoint.h:95
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:103
std::ostream & operator<<(std::ostream &os, const QgsPoint &p)
Definition: qgspoint.h:285
~QgsPoint()
Definition: qgspoint.h:89
QgsPoint(QPoint point)
Create a point from a QPoint.
Definition: qgspoint.h:76
QgsPoint(double x, double y)
Create a point from x,y coordinates.
Definition: qgspoint.h:58
QgsPoint operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspoint.h:250
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:82
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:87
QgsPoint operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:253
double x() const
Get the x value of the point.
Definition: qgspoint.h:118
QgsPoint & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspoint.h:241
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:292
bool operator==(const QgsPoint &p1, const QgsPoint &p2)
Definition: qgspoint.h:277
double x
Definition: qgspoint.h:41