QGIS API Documentation  2.99.0-Master (37c43df)
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 
35  public:
36 
39  QgsVector();
40 
45  QgsVector( double x, double y );
46 
48  QgsVector operator-() const;
49 
53  QgsVector operator*( double scalar ) const;
54 
58  QgsVector operator/( double scalar ) const;
59 
63  double operator*( QgsVector v ) const;
64 
67  double length() const;
68 
72  double x() const;
73 
77  double y() const;
78 
81  QgsVector perpVector() const;
82 
85  double angle() const;
86 
89  double angle( QgsVector v ) const;
90 
94  QgsVector rotateBy( double rot ) const;
95 
99  QgsVector normalized() const;
100 
101  private:
102 
103  double mX, mY;
104 
105 };
106 
111 class CORE_EXPORT QgsPoint
112 {
113  Q_GADGET
114 
115  Q_PROPERTY( double x READ x WRITE setX )
116  Q_PROPERTY( double y READ y WRITE setY )
117 
118  public:
121  : m_x( 0.0 )
122  , m_y( 0.0 )
123  {}
124 
126  QgsPoint( const QgsPoint& p );
127 
132  QgsPoint( double x, double y )
133  : m_x( x )
134  , m_y( y )
135  {}
136 
141  QgsPoint( QPointF point )
142  : m_x( point.x() )
143  , m_y( point.y() )
144  {}
145 
150  QgsPoint( QPoint point )
151  : m_x( point.x() )
152  , m_y( point.y() )
153  {}
154 
156  {}
157 
161  void setX( double x )
162  {
163  m_x = x;
164  }
165 
169  void setY( double y )
170  {
171  m_y = y;
172  }
173 
175  void set( double x, double y )
176  {
177  m_x = x;
178  m_y = y;
179  }
180 
184  double x() const
185  {
186  return m_x;
187  }
188 
192  double y() const
193  {
194  return m_y;
195  }
196 
201  QPointF toQPointF() const;
202 
204  QString toString() const;
205 
207  QString toString( int thePrecision ) const;
208 
218  QString toDegreesMinutesSeconds( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
219 
229  QString toDegreesMinutes( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
230 
231 
236  QString wellKnownText() const;
237 
241  double sqrDist( double x, double y ) const;
242 
246  double sqrDist( const QgsPoint& other ) const;
247 
254  double distance( double x, double y ) const;
255 
261  double distance( const QgsPoint& other ) const;
262 
264  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
265 
267  double azimuth( const QgsPoint& other ) const;
268 
275  QgsPoint project( double distance, double bearing ) const;
276 
283  bool compare( const QgsPoint &other, double epsilon = 4 * DBL_EPSILON ) const;
284 
286  bool operator==( const QgsPoint &other );
287 
289  bool operator!=( const QgsPoint &other ) const;
290 
292  void multiply( double scalar );
293 
298  int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
299 
301  QgsPoint & operator=( const QgsPoint &other );
302 
304  QgsVector operator-( const QgsPoint& p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
305 
307  QgsPoint &operator+=( QgsVector v ) { *this = *this + v; return *this; }
308 
310  QgsPoint &operator-=( QgsVector v ) { *this = *this - v; return *this; }
311 
313  QgsPoint operator+( QgsVector v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
314 
316  QgsPoint operator-( QgsVector v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
317 
319  QgsPoint operator*( double scalar ) const { return QgsPoint( m_x * scalar, m_y * scalar ); }
320 
322  QgsPoint operator/( double scalar ) const { return QgsPoint( m_x / scalar, m_y / scalar ); }
323 
325  QgsPoint &operator*=( double scalar ) { m_x *= scalar; m_y *= scalar; return *this; }
326 
328  QgsPoint &operator/=( double scalar ) { m_x /= scalar; m_y /= scalar; return *this; }
329 
330  private:
331 
333  double m_x;
334 
336  double m_y;
337 
338  friend uint qHash( const QgsPoint& pnt );
339 
340 }; // class QgsPoint
341 
342 
343 inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )
344 {
345  if ( qgsDoubleNear( p1.x(), p2.x() ) && qgsDoubleNear( p1.y(), p2.y() ) )
346  { return true; }
347  else
348  { return false; }
349 }
350 
351 inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
352 {
353  // Use Local8Bit for printouts
354  os << p.toString().toLocal8Bit().data();
355  return os;
356 }
357 
358 inline uint qHash( const QgsPoint& p )
359 {
360  uint hash;
361  uint h1 = qHash( static_cast< quint64 >( p.m_x ) );
362  uint h2 = qHash( static_cast< quint64 >( p.m_y ) );
363  hash = h1 ^( h2 << 1 );
364  return hash;
365 }
366 
367 #endif //QGSPOINT_H
double y
Definition: qgspoint.h:116
QgsVector operator-(const QgsPoint &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspoint.h:304
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:196
double y() const
Get the y value of the point.
Definition: qgspoint.h:192
QgsPoint(QPointF point)
Create a point from a QPointF.
Definition: qgspoint.h:141
QgsPoint & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspoint.h:310
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:326
QgsPoint operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:322
QgsPoint operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspoint.h:313
QgsPoint & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:325
A class to represent a point.
Definition: qgspoint.h:111
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:129
QgsPoint & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:328
A class to represent a vector.
Definition: qgspoint.h:32
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:161
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:169
std::ostream & operator<<(std::ostream &os, const QgsPoint &p)
Definition: qgspoint.h:351
~QgsPoint()
Definition: qgspoint.h:155
QgsPoint(QPoint point)
Create a point from a QPoint.
Definition: qgspoint.h:150
QgsPoint(double x, double y)
Create a point from x,y coordinates.
Definition: qgspoint.h:132
QgsPoint operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspoint.h:316
double x() const
Returns the vector&#39;s x-component.
Definition: qgspoint.cpp:68
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:73
QgsPoint operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:319
double x() const
Get the x value of the point.
Definition: qgspoint.h:184
QgsPoint & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspoint.h:307
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:358
bool operator==(const QgsPoint &p1, const QgsPoint &p2)
Definition: qgspoint.h:343
double x
Definition: qgspoint.h:115