QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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"
24
25#include "qgis.h"
26
27#include <iostream>
28#include <QString>
29#include <QPoint>
30#include <QObject>
31#include <qglobal.h>
32
33class QgsPoint;
34
59class CORE_EXPORT QgsPointXY
60{
61 Q_GADGET
62
63 Q_PROPERTY( double x READ x WRITE setX )
64 Q_PROPERTY( double y READ y WRITE setY )
65
66 public:
67
68 QgsPointXY() = default;
69
71
77 QgsPointXY( double x, double y ) SIP_HOLDGIL
78 : mX( x )
79 , mY( y )
80 , mIsEmpty( false )
81 {}
82
87 QgsPointXY( QPointF point ) SIP_HOLDGIL
88 : mX( point.x() )
89 , mY( point.y() )
90 , mIsEmpty( false )
91 {}
92
97 QgsPointXY( QPoint point ) SIP_HOLDGIL
98 : mX( point.x() )
99 , mY( point.y() )
100 , mIsEmpty( false )
101 {}
102
108 QgsPointXY( const QgsPoint &point ) SIP_HOLDGIL;
109
110 // IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
111 // because this class MUST be lightweight and we don't want the cost of the vtable here.
112 // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
113 ~QgsPointXY() = default;
114
119 void setX( double x ) SIP_HOLDGIL
120 {
121 mX = x;
122 mIsEmpty = false;
123 }
124
129 void setY( double y ) SIP_HOLDGIL
130 {
131 mY = y;
132 mIsEmpty = false;
133 }
134
136 void set( double x, double y ) SIP_HOLDGIL
137 {
138 mX = x;
139 mY = y;
140 mIsEmpty = false;
141 }
142
147 double x() const SIP_HOLDGIL
148 {
149 return mX;
150 }
151
156 double y() const SIP_HOLDGIL
157 {
158 return mY;
159 }
160
165 QPointF toQPointF() const
166 {
167 return QPointF( mX, mY );
168 }
169
174 QString toString( int precision = -1 ) const;
175
180 QString asWkt() const;
181
186 double sqrDist( double x, double y ) const SIP_HOLDGIL
187 {
188 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, x, y );
189 }
190
195 double sqrDist( const QgsPointXY &other ) const SIP_HOLDGIL
196 {
197 return QgsGeometryUtilsBase::sqrDistance2D( mX, mY, other.x(), other.y() );
198 }
199
206 double distance( double x, double y ) const SIP_HOLDGIL
207 {
208 return QgsGeometryUtilsBase::distance2D( mX, mY, x, y );
209 }
210
216 double distance( const QgsPointXY &other ) const SIP_HOLDGIL
217 {
218 return QgsGeometryUtilsBase::distance2D( mX, mY, other.x(), other.y() );
219 }
220
222 double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint SIP_OUT, double epsilon = DEFAULT_SEGMENT_EPSILON ) const SIP_HOLDGIL;
223
225 double azimuth( const QgsPointXY &other ) const SIP_HOLDGIL;
226
233 QgsPointXY project( double distance, double bearing ) const SIP_HOLDGIL;
234
242 bool isEmpty() const SIP_HOLDGIL { return mIsEmpty; }
243
253 bool compare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
254 {
255 return QgsGeometryUtilsBase::fuzzyEqual( epsilon, mX, mY, other.x(), other.y() );
256 }
257
268 bool distanceCompare( const QgsPointXY &other, double epsilon = 4 * std::numeric_limits<double>::epsilon() ) const SIP_HOLDGIL
269 {
270 return QgsGeometryUtilsBase::fuzzyDistanceEqual( epsilon, mX, mY, other.x(), other.y() );
271 }
272
273 bool operator==( const QgsPointXY &other ) const SIP_HOLDGIL
274 {
275 if ( isEmpty() && other.isEmpty() )
276 return true;
277 if ( isEmpty() && !other.isEmpty() )
278 return false;
279 if ( ! isEmpty() && other.isEmpty() )
280 return false;
281
282 return QgsGeometryUtilsBase::fuzzyEqual( 1E-8, mX, mY, other.x(), other.y() );
283 }
284
285 bool operator!=( const QgsPointXY &other ) const SIP_HOLDGIL
286 {
287 if ( isEmpty() && other.isEmpty() )
288 return false;
289 if ( isEmpty() && !other.isEmpty() )
290 return true;
291 if ( ! isEmpty() && other.isEmpty() )
292 return true;
293
294 return !QgsGeometryUtilsBase::fuzzyEqual( 1E-8, mX, mY, other.x(), other.y() );
295 }
296
298 void multiply( double scalar ) SIP_HOLDGIL
299 {
300 mX *= scalar;
301 mY *= scalar;
302 }
303
305 {
306 if ( &other != this )
307 {
308 mX = other.x();
309 mY = other.y();
310 mIsEmpty = other.isEmpty();
311 }
312
313 return *this;
314 }
315
317 QgsVector operator-( const QgsPointXY &p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
318
320 QgsPointXY &operator+=( QgsVector v ) { *this = *this + v; return *this; }
321
323 QgsPointXY &operator-=( QgsVector v ) { *this = *this - v; return *this; }
324
326 QgsPointXY operator+( QgsVector v ) const { return QgsPointXY( mX + v.x(), mY + v.y() ); }
327
329 QgsPointXY operator-( QgsVector v ) const { return QgsPointXY( mX - v.x(), mY - v.y() ); }
330
332 QgsPointXY operator*( double scalar ) const { return QgsPointXY( mX * scalar, mY * scalar ); }
333
335 QgsPointXY operator/( double scalar ) const { return QgsPointXY( mX / scalar, mY / scalar ); }
336
338 QgsPointXY &operator*=( double scalar ) { mX *= scalar; mY *= scalar; return *this; }
339
341 QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }
342
344 operator QVariant() const
345 {
346 return QVariant::fromValue( *this );
347 }
348
349#ifdef SIP_RUN
350 SIP_PYOBJECT __repr__();
351 % MethodCode
352 QString str = QStringLiteral( "<QgsPointXY: %1>" ).arg( sipCpp->asWkt() );
353 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
354 % End
355
356 int __len__();
357 % MethodCode
358 sipRes = 2;
359 % End
360
361
362 SIP_PYOBJECT __getitem__( int );
363 % MethodCode
364 if ( a0 == 0 )
365 {
366 sipRes = Py_BuildValue( "d", sipCpp->x() );
367 }
368 else if ( a0 == 1 )
369 {
370 sipRes = Py_BuildValue( "d", sipCpp->y() );
371 }
372 else
373 {
374 QString msg = QString( "Bad index: %1" ).arg( a0 );
375 PyErr_SetString( PyExc_IndexError, msg.toLatin1().constData() );
376 }
377 % End
378
379 long __hash__() const;
380 % MethodCode
381 sipRes = qHash( *sipCpp );
382 % End
383#endif
384
385 private:
386
388 double mX = 0; //std::numeric_limits<double>::quiet_NaN();
389
391 double mY = 0; //std::numeric_limits<double>::quiet_NaN();
392
394 bool mIsEmpty = true;
395
396 friend uint qHash( const QgsPointXY &pnt );
397
398}; // class QgsPointXY
399
401
402inline std::ostream &operator << ( std::ostream &os, const QgsPointXY &p ) SIP_SKIP
403{
404 // Use Local8Bit for printouts
405 os << p.toString().toLocal8Bit().data();
406 return os;
407}
408
409inline uint qHash( const QgsPointXY &p ) SIP_SKIP
410{
411 uint hash;
412 const uint h1 = qHash( static_cast< quint64 >( p.mX ) );
413 const uint h2 = qHash( static_cast< quint64 >( p.mY ) );
414 hash = h1 ^ ( h2 << 1 );
415 return hash;
416}
417
418
419#endif //QGSPOINTXY_H
static double sqrDistance2D(double x1, double y1, double x2, double y2)
Returns the squared 2D distance between (x1, y1) and (x2, y2).
static double distance2D(double x1, double y1, double x2, double y2)
Returns the 2D distance between (x1, y1) and (x2, y2).
static bool fuzzyEqual(T epsilon, const Args &... args) noexcept
Performs fuzzy comparison between pairs of values within a specified epsilon.
static bool fuzzyDistanceEqual(T epsilon, const Args &... args) noexcept
Compare equality between multiple pairs of values with a specified epsilon.
A class to represent a 2D point.
Definition qgspointxy.h:60
double x() const
Gets the x value of the point.
Definition qgspointxy.h:147
QgsPointXY(QPoint point)
Create a point from a QPoint.
Definition qgspointxy.h:97
QgsPointXY operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:332
QgsPointXY & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition qgspointxy.h:323
QgsPointXY(QPointF point)
Create a point from a QPointF.
Definition qgspointxy.h:87
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition qgspointxy.h:186
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition qgspointxy.h:206
QgsPointXY operator-(QgsVector v) const
Subtracts a vector from this point.
Definition qgspointxy.h:329
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:129
double distance(const QgsPointXY &other) const
Returns the distance between this point and another point.
Definition qgspointxy.h:216
void multiply(double scalar)
Multiply x and y by the given value.
Definition qgspointxy.h:298
QgsPointXY & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:338
QgsPointXY operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition qgspointxy.h:335
QgsPointXY & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition qgspointxy.h:341
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:253
bool operator==(const QgsPointXY &other) const
Definition qgspointxy.h:273
bool operator!=(const QgsPointXY &other) const
Definition qgspointxy.h:285
void set(double x, double y)
Sets the x and y value of the point.
Definition qgspointxy.h:136
QgsPointXY & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition qgspointxy.h:320
double y
Definition qgspointxy.h:64
QgsPointXY & operator=(const QgsPointXY &other)
Definition qgspointxy.h:304
QgsVector operator-(const QgsPointXY &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition qgspointxy.h:317
double y() const
Gets the y value of the point.
Definition qgspointxy.h:156
double x
Definition qgspointxy.h:63
double sqrDist(const QgsPointXY &other) const
Returns the squared distance between this point another point.
Definition qgspointxy.h:195
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:119
bool distanceCompare(const QgsPointXY &other, double epsilon=4 *std::numeric_limits< double >::epsilon()) const
Compares this point with another point with a fuzzy tolerance using distance comparison.
Definition qgspointxy.h:268
~QgsPointXY()=default
bool isEmpty() const
Returns true if the geometry is empty.
Definition qgspointxy.h:242
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:165
QgsPointXY operator+(QgsVector v) const
Adds a vector to this point.
Definition qgspointxy.h:326
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
A class to represent a vector.
Definition qgsvector.h:30
double y() const
Returns the vector's y-component.
Definition qgsvector.h:152
double x() const
Returns the vector's x-component.
Definition qgsvector.h:143
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:199
#define str(x)
Definition qgis.cpp:39
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition qgis.h:6453
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_HOLDGIL
Definition qgis_sip.h:171
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
uint qHash(const QgsPointXY &p)
Definition qgspointxy.h:409
int precision