QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 *
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
100  Q_DECL_DEPRECATED QgsVector normal() const;
101
105  QgsVector normalized() const;
106
107  private:
108
109  double mX, mY;
110
111 };
112
117 class CORE_EXPORT QgsPoint
118 {
119  public:
122  : m_x( 0.0 )
123  , m_y( 0.0 )
124  {}
125
127  QgsPoint( const QgsPoint& p );
128
133  QgsPoint( double x, double y )
134  : m_x( x )
135  , m_y( y )
136  {}
137
142  QgsPoint( QPointF point )
143  : m_x( point.x() )
144  , m_y( point.y() )
145  {}
146
151  QgsPoint( QPoint point )
152  : m_x( point.x() )
153  , m_y( point.y() )
154  {}
155
157  {}
158
162  void setX( double x )
163  {
164  m_x = x;
165  }
166
170  void setY( double y )
171  {
172  m_y = y;
173  }
174
176  void set( double x, double y )
177  {
178  m_x = x;
179  m_y = y;
180  }
181
185  double x() const
186  {
187  return m_x;
188  }
189
193  double y() const
194  {
195  return m_y;
196  }
197
202  QPointF toQPointF() const;
203
205  QString toString() const;
206
208  QString toString( int thePrecision ) const;
209
219  QString toDegreesMinutesSeconds( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
220
230  QString toDegreesMinutes( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
231
232
237  QString wellKnownText() const;
238
242  double sqrDist( double x, double y ) const;
243
247  double sqrDist( const QgsPoint& other ) const;
248
255  double distance( double x, double y ) const;
256
262  double distance( const QgsPoint& other ) const;
263
265  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
266
268  double azimuth( const QgsPoint& other ) const;
269
276  QgsPoint project( double distance, double bearing ) const;
277
284  bool compare( const QgsPoint &other, double epsilon = 4 * DBL_EPSILON ) const;
285
287  bool operator==( const QgsPoint &other );
288
290  bool operator!=( const QgsPoint &other ) const;
291
293  void multiply( double scalar );
294
299  int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
300
302  QgsPoint & operator=( const QgsPoint &other );
303
305  QgsVector operator-( const QgsPoint& p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
306
308  QgsPoint &operator+=( QgsVector v ) { *this = *this + v; return *this; }
309
311  QgsPoint &operator-=( QgsVector v ) { *this = *this - v; return *this; }
312
314  QgsPoint operator+( QgsVector v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
315
317  QgsPoint operator-( QgsVector v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
318
320  QgsPoint operator*( double scalar ) const { return QgsPoint( m_x * scalar, m_y * scalar ); }
321
323  QgsPoint operator/( double scalar ) const { return QgsPoint( m_x / scalar, m_y / scalar ); }
324
326  QgsPoint &operator*=( double scalar ) { m_x *= scalar; m_y *= scalar; return *this; }
327
329  QgsPoint &operator/=( double scalar ) { m_x /= scalar; m_y /= scalar; return *this; }
330
331  private:
332
334  double m_x;
335
337  double m_y;
338
339  friend uint qHash( const QgsPoint& pnt );
340
341 }; // class QgsPoint
342
343
344 inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )
345 {
346  if ( qgsDoubleNear( p1.x(), p2.x() ) && qgsDoubleNear( p1.y(), p2.y() ) )
347  { return true; }
348  else
349  { return false; }
350 }
351
352 inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
353 {
354  // Use Local8Bit for printouts
355  os << p.toString().toLocal8Bit().data();
356  return os;
357 }
358
359 inline uint qHash( const QgsPoint& p )
360 {
361  uint hash;
362  uint h1 = qHash( static_cast< quint64 >( p.m_x ) );
363  uint h2 = qHash( static_cast< quint64 >( p.m_y ) );
364  hash = h1 ^( h2 << 1 );
365  return hash;
366 }
367
368 #endif //QGSPOINT_H
QgsVector operator-(const QgsPoint &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspoint.h:305
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:353
double y() const
Get the y value of the point.
Definition: qgspoint.h:193
QgsPoint()
Default constructor.
Definition: qgspoint.h:121
QgsPoint(QPointF point)
Create a point from a QPointF.
Definition: qgspoint.h:142
QgsPoint & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspoint.h:311
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:490
QgsPoint operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:323
QgsPoint operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspoint.h:314
QgsPoint & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:326
A class to represent a point.
Definition: qgspoint.h:117
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:134
QgsPoint & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:329
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)
QByteArray toLocal8Bit() const
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:162
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:170
std::ostream & operator<<(std::ostream &os, const QgsPoint &p)
Definition: qgspoint.h:352
~QgsPoint()
Definition: qgspoint.h:156
QgsPoint(QPoint point)
Create a point from a QPoint.
Definition: qgspoint.h:151
QgsPoint(double x, double y)
Create a point from x,y coordinates.
Definition: qgspoint.h:133
char * data()
QgsPoint operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspoint.h:317
double x() const
Returns the vector&#39;s x-component.
Definition: qgspoint.cpp:68
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:320
double x() const
Get the x value of the point.
Definition: qgspoint.h:185
QgsPoint & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspoint.h:308
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:359
bool operator==(const QgsPoint &p1, const QgsPoint &p2)
Definition: qgspoint.h:344