QGIS API Documentation  2.14.0-Essen
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  double m_x, m_y;
35 
36  public:
37  QgsVector();
38  QgsVector( double x, double y );
39 
41  QgsVector operator-( void ) const;
42  QgsVector operator*( double scalar ) const;
43  QgsVector operator/( double scalar ) const;
44  double operator*( QgsVector v ) const;
45  double length() const;
46 
47  double x() const;
48  double y() const;
49 
50  // perpendicular vector (rotated 90 degrees counter-clockwise)
51  QgsVector perpVector() const;
52 
54  double angle( void ) const;
55  double angle( QgsVector v ) const;
56  QgsVector rotateBy( double rot ) const;
57  QgsVector normal() const;
58 
59 };
60 
65 class CORE_EXPORT QgsPoint
66 {
67  public:
69  QgsPoint() : m_x( 0.0 ), m_y( 0.0 )
70  {}
71 
73  QgsPoint( const QgsPoint& p );
74 
79  QgsPoint( double x, double y )
80  : m_x( x ), m_y( y )
81  {}
82 
87  QgsPoint( QPointF point )
88  : m_x( point.x() ), m_y( point.y() )
89  {}
90 
95  QgsPoint( QPoint point )
96  : m_x( point.x() ), m_y( point.y() )
97  {}
98 
100  {}
101 
105  void setX( double x )
106  {
107  m_x = x;
108  }
109 
113  void setY( double y )
114  {
115  m_y = y;
116  }
117 
119  void set( double x, double y )
120  {
121  m_x = x;
122  m_y = y;
123  }
124 
128  double x() const
129  {
130  return m_x;
131  }
132 
136  double y() const
137  {
138  return m_y;
139  }
140 
145  QPointF toQPointF() const;
146 
148  QString toString() const;
149 
151  QString toString( int thePrecision ) const;
152 
162  QString toDegreesMinutesSeconds( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
163 
173  QString toDegreesMinutes( int thePrecision, const bool useSuffix = true, const bool padded = false ) const;
174 
175 
180  QString wellKnownText() const;
181 
183  double sqrDist( double x, double y ) const;
184 
186  double sqrDist( const QgsPoint& other ) const;
187 
189  double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
190 
192  double azimuth( const QgsPoint& other );
193 
200  bool compare( const QgsPoint &other, double epsilon = 4 * DBL_EPSILON ) const;
201 
203  bool operator==( const QgsPoint &other );
204 
206  bool operator!=( const QgsPoint &other ) const;
207 
209  void multiply( double scalar );
210 
215  int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
216 
218  QgsPoint & operator=( const QgsPoint &other );
219 
220  QgsVector operator-( const QgsPoint& p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
221  QgsPoint &operator+=( QgsVector v ) { *this = *this + v; return *this; }
222  QgsPoint &operator-=( QgsVector v ) { *this = *this - v; return *this; }
223  QgsPoint operator+( QgsVector v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
224  QgsPoint operator-( QgsVector v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
225 
226  private:
227 
229  double m_x;
230 
232  double m_y;
233 
234  friend uint qHash( const QgsPoint& pnt );
235 
236 }; // class QgsPoint
237 
238 
239 inline bool operator==( const QgsPoint &p1, const QgsPoint &p2 )
240 {
241  if ( qgsDoubleNear( p1.x(), p2.x() ) && qgsDoubleNear( p1.y(), p2.y() ) )
242  { return true; }
243  else
244  { return false; }
245 }
246 
247 inline std::ostream& operator << ( std::ostream& os, const QgsPoint &p )
248 {
249  // Use Local8Bit for printouts
250  os << p.toString().toLocal8Bit().data();
251  return os;
252 }
253 
254 inline uint qHash( const QgsPoint& p )
255 {
256  uint hash;
257  uint h1 = qHash( static_cast< quint64 >( p.m_x ) );
258  uint h2 = qHash( static_cast< quint64 >( p.m_y ) );
259  hash = h1 ^( h2 << 1 );
260  return hash;
261 }
262 
263 #endif //QGSPOINT_H
QgsPoint operator+(QgsVector v) const
Definition: qgspoint.h:223
QgsPoint operator-(QgsVector v) const
Definition: qgspoint.h:224
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
double y() const
Definition: qgspoint.cpp:69
QgsVector operator-(const QgsPoint &p) const
Definition: qgspoint.h:220
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:285
double x() const
Get the x value of the point.
Definition: qgspoint.h:128
QgsPoint()
Default constructor.
Definition: qgspoint.h:69
QgsPoint(QPointF point)
Create a point from a QPointF.
Definition: qgspoint.h:87
QgsPoint & operator-=(QgsVector v)
Definition: qgspoint.h:222
const double DEFAULT_SEGMENT_EPSILON
Default snapping tolerance for segments.
Definition: qgis.h:382
QString toString() const
String representation of the point (x,y)
Definition: qgspoint.cpp:126
A class to represent a point.
Definition: qgspoint.h:65
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:105
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:113
std::ostream & operator<<(std::ostream &os, const QgsPoint &p)
Definition: qgspoint.h:247
~QgsPoint()
Definition: qgspoint.h:99
QgsPoint(QPoint point)
Create a point from a QPoint.
Definition: qgspoint.h:95
QgsPoint(double x, double y)
Create a point from x,y coordinates.
Definition: qgspoint.h:79
char * data()
double y() const
Get the y value of the point.
Definition: qgspoint.h:136
double x() const
Definition: qgspoint.cpp:64
QgsPoint & operator+=(QgsVector v)
Definition: qgspoint.h:221
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:254
bool operator==(const QgsPoint &p1, const QgsPoint &p2)
Definition: qgspoint.h:239