QGIS API Documentation  2.99.0-Master (0a63d1f)
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 <qgis.h>
23 
24 #include <iostream>
25 #include <QString>
26 #include <QPoint>
27 
33 class CORE_EXPORT QgsVector
34 {
35 
36  public:
37 
40  QgsVector();
41 
46  QgsVector( double x, double y );
47 
49  QgsVector operator-() const;
50 
54  QgsVector operator*( double scalar ) const;
55 
59  QgsVector operator/( double scalar ) const;
60 
65  double operator*( QgsVector v ) const;
66 
71  QgsVector operator+( QgsVector other ) const;
72 
77  QgsVector& operator+=( QgsVector other );
78 
83  QgsVector operator-( QgsVector other ) const;
84 
89  QgsVector& operator-=( QgsVector other );
90 
93  double length() const;
94 
98  double x() const;
99 
103  double y() const;
104 
107  QgsVector perpVector() const;
108 
111  double angle() const;
112 
115  double angle( QgsVector v ) const;
116 
120  QgsVector rotateBy( double rot ) const;
121 
125  QgsVector normalized() const;
126 
128  bool operator==( QgsVector other ) const;
129 
131  bool operator!=( QgsVector other ) const;
132 
133  private:
134 
135  double mX, mY;
136 
137 };
138 
143 class CORE_EXPORT QgsPoint
144 {
145  Q_GADGET
146 
147  Q_PROPERTY( double x READ x WRITE setX )
148  Q_PROPERTY( double y READ y WRITE setY )
149 
150  public:
153  : m_x( 0.0 )
154  , m_y( 0.0 )
155  {}
156 
158  QgsPoint( const QgsPoint& p );
159 
164  QgsPoint( double x, double y )
165  : m_x( x )
166  , m_y( y )
167  {}
168 
173  QgsPoint( QPointF point )
174  : m_x( point.x() )
175  , m_y( point.y() )
176  {}
177 
182  QgsPoint( QPoint point )
183  : m_x( point.x() )
184  , m_y( point.y() )
185  {}
186 
188  {}
189 
193  void setX( double x )
194  {
195  m_x = x;
196  }
197 
201  void setY( double y )
202  {
203  m_y = y;
204  }
205 
207  void set( double x, double y )
208  {
209  m_x = x;
210  m_y = y;
211  }
212 
216  double x() const
217  {
218  return m_x;
219  }
220 
224  double y() const
225  {
226  return m_y;
227  }
228 
233  QPointF toQPointF() const;
234 
236  QString toString() const;
237 
239  QString toString( int thePrecision ) const;
240 
250  QString toDegreesMinutesSeconds( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
251 
261  QString toDegreesMinutes( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
262 
263 
268  QString wellKnownText() const;
269 
273  double sqrDist( double x, double y ) const;
274 
278  double sqrDist( const QgsPoint& other ) const;
279 
286  double distance( double x, double y ) const;
287 
293  double distance( const QgsPoint& other ) const;
294 
296  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
297 
299  double azimuth( const QgsPoint& other ) const;
300 
307  QgsPoint project( double distance, double bearing ) const;
308 
315  bool compare( const QgsPoint &other, double epsilon = 4 * DBL_EPSILON ) const;
316 
318  bool operator==( const QgsPoint &other );
319 
321  bool operator!=( const QgsPoint &other ) const;
322 
324  void multiply( double scalar );
325 
330  int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
331 
333  QgsPoint & operator=( const QgsPoint &other );
334 
336  QgsVector operator-( const QgsPoint& p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
337 
339  QgsPoint &operator+=( QgsVector v ) { *this = *this + v; return *this; }
340 
342  QgsPoint &operator-=( QgsVector v ) { *this = *this - v; return *this; }
343 
345  QgsPoint operator+( QgsVector v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
346 
348  QgsPoint operator-( QgsVector v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
349 
351  QgsPoint operator*( double scalar ) const { return QgsPoint( m_x * scalar, m_y * scalar ); }
352 
354  QgsPoint operator/( double scalar ) const { return QgsPoint( m_x / scalar, m_y / scalar ); }
355 
357  QgsPoint &operator*=( double scalar ) { m_x *= scalar; m_y *= scalar; return *this; }
358 
360  QgsPoint &operator/=( double scalar ) { m_x /= scalar; m_y /= scalar; return *this; }
361 
362  private:
363 
365  double m_x;
366 
368  double m_y;
369 
370  friend uint qHash( const QgsPoint& pnt );
371 
372 }; // class QgsPoint
373 
374 
375 inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )
376 {
377  if ( qgsDoubleNear( p1.x(), p2.x() ) && qgsDoubleNear( p1.y(), p2.y() ) )
378  { return true; }
379  else
380  { return false; }
381 }
382 
383 inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
384 {
385  // Use Local8Bit for printouts
386  os << p.toString().toLocal8Bit().data();
387  return os;
388 }
389 
390 inline uint qHash( const QgsPoint& p )
391 {
392  uint hash;
393  uint h1 = qHash( static_cast< quint64 >( p.m_x ) );
394  uint h2 = qHash( static_cast< quint64 >( p.m_y ) );
395  hash = h1 ^( h2 << 1 );
396  return hash;
397 }
398 
399 #endif //QGSPOINT_H
double y
Definition: qgspoint.h:148
QgsVector operator-(const QgsPoint &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspoint.h:336
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:193
double y() const
Get the y value of the point.
Definition: qgspoint.h:224
QgsPoint(QPointF point)
Create a point from a QPointF.
Definition: qgspoint.h:173
QgsPoint & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspoint.h:342
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:323
QgsPoint operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:354
QgsPoint operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspoint.h:345
QgsPoint & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:357
QDateTime operator+(const QDateTime &start, QgsInterval interval)
A class to represent a point.
Definition: qgspoint.h:143
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:163
QgsPoint & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:360
A class to represent a vector.
Definition: qgspoint.h:33
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
void setX(double x)
Sets the x value of the point.
Definition: qgspoint.h:193
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:201
std::ostream & operator<<(std::ostream &os, const QgsPoint &p)
Definition: qgspoint.h:383
~QgsPoint()
Definition: qgspoint.h:187
QgsPoint(QPoint point)
Create a point from a QPoint.
Definition: qgspoint.h:182
QgsPoint(double x, double y)
Create a point from x,y coordinates.
Definition: qgspoint.h:164
QgsPoint operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspoint.h:348
double x() const
Returns the vector&#39;s x-component.
Definition: qgspoint.cpp:92
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)
Definition: MathUtils.cc:857
double y() const
Returns the vector&#39;s y-component.
Definition: qgspoint.cpp:97
QgsPoint operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:351
double x() const
Get the x value of the point.
Definition: qgspoint.h:216
QgsPoint & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspoint.h:339
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:390
bool operator==(const QgsPoint &p1, const QgsPoint &p2)
Definition: qgspoint.h:375
double x
Definition: qgspoint.h:147