QGIS API Documentation
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 
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:
121  QgsPoint() : m_x( 0.0 ), m_y( 0.0 )
122  {}
123 
125  QgsPoint( const QgsPoint& p );
126 
131  QgsPoint( double x, double y )
132  : m_x( x ), m_y( y )
133  {}
134 
139  QgsPoint( QPointF point )
140  : m_x( point.x() ), m_y( point.y() )
141  {}
142 
147  QgsPoint( QPoint point )
148  : m_x( point.x() ), m_y( point.y() )
149  {}
150 
152  {}
153 
157  void setX( double x )
158  {
159  m_x = x;
160  }
161 
165  void setY( double y )
166  {
167  m_y = y;
168  }
169 
171  void set( double x, double y )
172  {
173  m_x = x;
174  m_y = y;
175  }
176 
180  double x() const
181  {
182  return m_x;
183  }
184 
188  double y() const
189  {
190  return m_y;
191  }
192 
197  QPointF toQPointF() const;
198 
200  QString toString() const;
201 
203  QString toString( int thePrecision ) const;
204 
214  QString toDegreesMinutesSeconds( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
215 
225  QString toDegreesMinutes( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
226 
227 
232  QString wellKnownText() const;
233 
237  double sqrDist( double x, double y ) const;
238 
242  double sqrDist( const QgsPoint& other ) const;
243 
250  double distance( double x, double y ) const;
251 
257  double distance( const QgsPoint& other ) const;
258 
260  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
261 
263  double azimuth( const QgsPoint& other ) const;
264 
271  QgsPoint project( double distance, double bearing ) const;
272 
279  bool compare( const QgsPoint &other, double epsilon = 4 * DBL_EPSILON ) const;
280 
282  bool operator==( const QgsPoint &other );
283 
285  bool operator!=( const QgsPoint &other ) const;
286 
288  void multiply( double scalar );
289 
294  int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
295 
297  QgsPoint & operator=( const QgsPoint &other );
298 
300  QgsVector operator-( const QgsPoint& p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
301 
303  QgsPoint &operator+=( QgsVector v ) { *this = *this + v; return *this; }
304 
306  QgsPoint &operator-=( QgsVector v ) { *this = *this - v; return *this; }
307 
309  QgsPoint operator+( QgsVector v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
310 
312  QgsPoint operator-( QgsVector v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
313 
315  QgsPoint operator*( double scalar ) const { return QgsPoint( m_x * scalar, m_y * scalar ); }
316 
318  QgsPoint operator/( double scalar ) const { return QgsPoint( m_x / scalar, m_y / scalar ); }
319 
321  QgsPoint &operator*=( double scalar ) { m_x *= scalar; m_y *= scalar; return *this; }
322 
324  QgsPoint &operator/=( double scalar ) { m_x /= scalar; m_y /= scalar; return *this; }
325 
326  private:
327 
329  double m_x;
330 
332  double m_y;
333 
334  friend uint qHash( const QgsPoint& pnt );
335 
336 }; // class QgsPoint
337 
338 
339 inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )
340 {
341  if ( qgsDoubleNear( p1.x(), p2.x() ) && qgsDoubleNear( p1.y(), p2.y() ) )
342  { return true; }
343  else
344  { return false; }
345 }
346 
347 inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
348 {
349  // Use Local8Bit for printouts
350  os << p.toString().toLocal8Bit().data();
351  return os;
352 }
353 
354 inline uint qHash( const QgsPoint& p )
355 {
356  uint hash;
357  uint h1 = qHash( static_cast< quint64 >( p.m_x ) );
358  uint h2 = qHash( static_cast< quint64 >( p.m_y ) );
359  hash = h1 ^( h2 << 1 );
360  return hash;
361 }
362 
363 #endif //QGSPOINT_H
QgsPoint operator+(QgsVector v) const
Adds a vector to this point.
Definition: qgspoint.h:309
QgsPoint operator-(QgsVector v) const
Subtracts a vector from this point.
Definition: qgspoint.h:312
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double y() const
Returns the vector&#39;s y-component.
Definition: qgspoint.cpp:73
QgsVector operator-(const QgsPoint &p) const
Calculates the vector obtained by subtracting a point from this point.
Definition: qgspoint.h:300
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:348
double x() const
Get the x value of the point.
Definition: qgspoint.h:180
QgsPoint()
Default constructor.
Definition: qgspoint.h:121
QgsPoint(QPointF point)
Create a point from a QPointF.
Definition: qgspoint.h:139
QgsPoint & operator-=(QgsVector v)
Subtracts a vector from this point in place.
Definition: qgspoint.h:306
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:449
QgsPoint & operator*=(double scalar)
Multiplies the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:321
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:134
A class to represent a point.
Definition: qgspoint.h:117
QgsPoint & operator/=(double scalar)
Divides the coordinates in this point by a scalar quantity in place.
Definition: qgspoint.h:324
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
void setX(double x)
Sets the x value of the point.
Definition: qgspoint.h:157
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:165
std::ostream & operator<<(std::ostream &os, const QgsPoint &p)
Definition: qgspoint.h:347
~QgsPoint()
Definition: qgspoint.h:151
QgsPoint(QPoint point)
Create a point from a QPoint.
Definition: qgspoint.h:147
QgsPoint(double x, double y)
Create a point from x,y coordinates.
Definition: qgspoint.h:131
char * data()
double y() const
Get the y value of the point.
Definition: qgspoint.h:188
QgsPoint operator*(double scalar) const
Multiplies the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:315
double x() const
Returns the vector&#39;s x-component.
Definition: qgspoint.cpp:68
QgsPoint & operator+=(QgsVector v)
Adds a vector to this point in place.
Definition: qgspoint.h:303
QgsPoint operator/(double scalar) const
Divides the coordinates in this point by a scalar quantity.
Definition: qgspoint.h:318
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:354
bool operator==(const QgsPoint &p1, const QgsPoint &p2)
Definition: qgspoint.h:339