QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgspointxy.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 QGSPOINTXY_H
19 #define QGSPOINTXY_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 QgsPoint;
32 
43 class CORE_EXPORT QgsPointXY
44 {
45  Q_GADGET
46 
47  Q_PROPERTY( double x READ x WRITE setX )
48  Q_PROPERTY( double y READ y WRITE setY )
49 
50  public:
52  QgsPointXY() = default;
53 
55  QgsPointXY( const QgsPointXY &p );
56 
62  QgsPointXY( double x, double y )
63  : mX( x )
64  , mY( y )
65  {}
66 
72  QgsPointXY( QPointF point )
73  : mX( point.x() )
74  , mY( point.y() )
75  {}
76 
82  QgsPointXY( QPoint point )
83  : mX( point.x() )
84  , mY( point.y() )
85  {}
86 
93  QgsPointXY( const QgsPoint &point );
94 
95  // IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
96  // because this class MUST be lightweight and we don't want the cost of the vtable here.
97  // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
98  ~QgsPointXY() = default;
99 
104  void setX( double x )
105  {
106  mX = x;
107  }
108 
113  void setY( double y )
114  {
115  mY = y;
116  }
117 
119  void set( double x, double y )
120  {
121  mX = x;
122  mY = y;
123  }
124 
129  double x() const
130  {
131  return mX;
132  }
133 
138  double y() const
139  {
140  return mY;
141  }
142 
148  QPointF toQPointF() const
149  {
150  return QPointF( mX, mY );
151  }
152 
157  QString toString( int precision = -1 ) const;
158 
163  QString asWkt() const;
164 
169  double sqrDist( double x, double y ) const
170  {
171  return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y );
172  }
173 
178  double sqrDist( const QgsPointXY &other ) const
179  {
180  return sqrDist( other.x(), other.y() );
181  }
182 
190  double distance( double x, double y ) const
191  {
192  return std::sqrt( sqrDist( x, y ) );
193  }
194 
201  double distance( const QgsPointXY &other ) const
202  {
203  return std::sqrt( sqrDist( other ) );
204  }
205 
207  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
208 
210  double azimuth( const QgsPointXY &other ) const;
211 
219  QgsPointXY project( double distance, double bearing ) const;
220 
228  bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const
229  {
230  return ( qgsDoubleNear( mX, other.x(), epsilon ) && qgsDoubleNear( mY, other.y(), epsilon ) );
231  }
232 
234  bool operator==( const QgsPointXY &other )
235  {
236  return ( qgsDoubleNear( mX, other.x() ) && qgsDoubleNear( mY, other.y() ) );
237  }
238 
240  bool operator!=( const QgsPointXY &other ) const
241  {
242  return !( qgsDoubleNear( mX, other.x() ) && qgsDoubleNear( mY, other.y() ) );
243  }
244 
246  void multiply( double scalar )
247  {
248  mX *= scalar;
249  mY *= scalar;
250  }
251 
253  QgsPointXY &operator=( const QgsPointXY &other )
254  {
255  if ( &other != this )
256  {
257  mX = other.x();
258  mY = other.y();
259  }
260 
261  return *this;
262  }
263 
265  QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
266 
268  QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
269 
271  QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
272 
274  QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
275 
277  QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
278 
280  QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
281 
283  QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
284 
286  QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
287 
289  QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
290 
292  operator QVariant() const
293  {
294  return QVariant::fromValue( *this );
295  }
296 
297 #ifdef SIP_RUN
298  SIP_PYOBJECT __repr__();
299  % MethodCode
300  QString str = QStringLiteral( "<QgsPointXY: %1>" ).arg( sipCpp->asWkt() );
301  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
302  % End
303 
304  int __len__();
305  % MethodCode
306  sipRes = 2;
307  % End
308 
309 
310  SIP_PYOBJECT __getitem__( int );
311  % MethodCode
312  if ( a0 == 0 )
313  {
314  sipRes = Py_BuildValue( "d", sipCpp->x() );
315  }
316  else if ( a0 == 1 )
317  {
318  sipRes = Py_BuildValue( "d", sipCpp->y() );
319  }
320  else
321  {
322  QString msg = QString( "Bad index: %1" ).arg( a0 );
323  PyErr_SetString( PyExc_IndexError, msg.toAscii().constData() );
324  }
325  % End
326 
327  long __hash__() const;
328  % MethodCode
329  sipRes = qHash( *sipCpp );
330  % End
331 #endif
332 
333  private:
334 
336  double mX = 0.0;
337 
339  double mY = 0.0;
340 
341  friend uint qHash( const QgsPointXY &pnt );
342 
343 }; // class QgsPointXY
344 
346 
347 inline bool operator==( const QgsPointXY &p1, const QgsPointXY &p2 ) SIP_SKIP
348 {
349  if ( qgsDoubleNear( p1.x(), p2.x() ) && qgsDoubleNear( p1.y(), p2.y() ) )
350  { return true; }
351  else
352  { return false; }
353 }
354 
355 inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
356 {
357  // Use Local8Bit for printouts
358  os << p.toString().toLocal8Bit().data();
359  return os;
360 }
361 
362 inline uint qHash( const QgsPointXY &p ) SIP_SKIP
363 {
364  uint hash;
365  uint h1 = qHash( static_cast< quint64 >( p.mX ) );
366  uint h2 = qHash( static_cast< quint64 >( p.mY ) );
367  hash = h1 ^ ( h2 << 1 );
368  return hash;
369 }
370 
371 
372 #endif //QGSPOINTXY_H
int precision
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:286
uint qHash(const QgsPointXY &p)
Definition: qgspointxy.h:362
double y
Definition: qgspointxy.h:48
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:265
bool operator==(const QgsPointXY &other)
equality operator
Definition: qgspointxy.h:234
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspointxy.h:265
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspointxy.h:169
double distance(const QgsPointXY &other) const
Returns the distance between this point and another point.
Definition: qgspointxy.h:201
bool operator!=(const QgsPointXY &other) const
Inequality operator.
Definition: qgspointxy.h:240
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:148
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:573
bool compare(const QgsPointXY &other, double epsilon=4 *std::numeric_limits< double >::epsilon()) const
Compares this point with another point with a fuzzy tolerance.
Definition: qgspointxy.h:228
#define SIP_SKIP
Definition: qgis_sip.h:119
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:113
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
double x() const
Gets the x value of the point.
Definition: qgspointxy.h:129
void multiply(double scalar)
Multiply x and y by the given value.
Definition: qgspointxy.h:246
double sqrDist(const QgsPointXY &other) const
Returns the squared distance between this point another point.
Definition: qgspointxy.h:178
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspointxy.h:190
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
void setX(double x)
Sets the x value of the point.
Definition: qgspointxy.h:104
QgsPointXY(QPoint point)
Create a point from a QPoint.
Definition: qgspointxy.h:82
double x
Definition: qgspointxy.h:47
A class to represent a vector.
Definition: qgsvector.h:29
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspointxy.h:268
std::ostream & operator<<(std::ostream &os, const QgsPointXY &p)
Definition: qgspointxy.h:355
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspointxy.h:271
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspointxy.h:274
QgsPointXY(QPointF point)
Create a point from a QPointF.
Definition: qgspointxy.h:72
#define SIP_OUT
Definition: qgis_sip.h:51
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspointxy.h:277
double y() const
Gets the y value of the point.
Definition: qgspointxy.h:138
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:280
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspointxy.h:283
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspointxy.h:289
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:76
QgsPointXY & operator=(const QgsPointXY &other)
Assignment.
Definition: qgspointxy.h:253
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:81