QGIS API Documentation  3.0.2-Girona (307d082)
qgspointxy.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspoint.cpp - 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 
19 #include "qgspointxy.h"
20 #include "qgspoint.h"
21 
22 #include <cmath>
23 #include <QTextStream>
24 #include <QObject> // for tr()
25 
26 #include "qgsexception.h"
27 
29 {
30  mX = p.x();
31  mY = p.y();
32 }
33 
35  : mX( point.x() )
36  , mY( point.y() )
37 {
38 }
39 
40 QPointF QgsPointXY::toQPointF() const
41 {
42  return QPointF( mX, mY );
43 }
44 
45 QString QgsPointXY::toString( int precision ) const
46 {
47  if ( precision < 0 )
48  {
49  QString rep;
50  QTextStream ot( &rep );
51  ot.setRealNumberPrecision( 12 );
52  ot << mX << ", " << mY;
53  return rep;
54  }
55  else
56  {
57  QString x = std::isfinite( mX ) ? QString::number( mX, 'f', precision ) : QObject::tr( "infinite" );
58  QString y = std::isfinite( mY ) ? QString::number( mY, 'f', precision ) : QObject::tr( "infinite" );
59  return QStringLiteral( "%1,%2" ).arg( x, y );
60  }
61 }
62 
63 QString QgsPointXY::asWkt() const
64 {
65  return QStringLiteral( "POINT(%1 %2)" ).arg( qgsDoubleToString( mX ), qgsDoubleToString( mY ) );
66 }
67 
68 double QgsPointXY::sqrDist( double x, double y ) const
69 {
70  return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y );
71 }
72 
73 double QgsPointXY::sqrDist( const QgsPointXY &other ) const
74 {
75  return sqrDist( other.x(), other.y() );
76 }
77 
78 double QgsPointXY::distance( double x, double y ) const
79 {
80  return std::sqrt( sqrDist( x, y ) );
81 }
82 
83 double QgsPointXY::distance( const QgsPointXY &other ) const
84 {
85  return std::sqrt( sqrDist( other ) );
86 }
87 
88 double QgsPointXY::azimuth( const QgsPointXY &other ) const
89 {
90  double dx = other.x() - mX;
91  double dy = other.y() - mY;
92  return ( std::atan2( dx, dy ) * 180.0 / M_PI );
93 }
94 
95 QgsPointXY QgsPointXY::project( double distance, double bearing ) const
96 {
97  double rads = bearing * M_PI / 180.0;
98  double dx = distance * std::sin( rads );
99  double dy = distance * std::cos( rads );
100  return QgsPointXY( mX + dx, mY + dy );
101 }
102 
103 bool QgsPointXY::compare( const QgsPointXY &other, double epsilon ) const
104 {
105  return ( qgsDoubleNear( mX, other.x(), epsilon ) && qgsDoubleNear( mY, other.y(), epsilon ) );
106 }
107 
108 // operators
109 bool QgsPointXY::operator==( const QgsPointXY &other )
110 {
111  return ( qgsDoubleNear( mX, other.x() ) && qgsDoubleNear( mY, other.y() ) );
112 }
113 
114 bool QgsPointXY::operator!=( const QgsPointXY &other ) const
115 {
116  return !( qgsDoubleNear( mX, other.x() ) && qgsDoubleNear( mY, other.y() ) );
117 }
118 
120 {
121  if ( &other != this )
122  {
123  mX = other.x();
124  mY = other.y();
125  }
126 
127  return *this;
128 }
129 
130 void QgsPointXY::multiply( double scalar )
131 {
132  mX *= scalar;
133  mY *= scalar;
134 }
135 
136 double QgsPointXY::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon ) const
137 {
138  double nx, ny; //normal vector
139 
140  nx = y2 - y1;
141  ny = -( x2 - x1 );
142 
143  double t;
144  t = ( mX * ny - mY * nx - x1 * ny + y1 * nx ) / ( ( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
145 
146  if ( t < 0.0 )
147  {
148  minDistPoint.setX( x1 );
149  minDistPoint.setY( y1 );
150  }
151  else if ( t > 1.0 )
152  {
153  minDistPoint.setX( x2 );
154  minDistPoint.setY( y2 );
155  }
156  else
157  {
158  minDistPoint.setX( x1 + t * ( x2 - x1 ) );
159  minDistPoint.setY( y1 + t * ( y2 - y1 ) );
160  }
161 
162  double dist = sqrDist( minDistPoint );
163  //prevent rounding errors if the point is directly on the segment
164  if ( qgsDoubleNear( dist, 0.0, epsilon ) )
165  {
166  minDistPoint.setX( mX );
167  minDistPoint.setY( mY );
168  return 0.0;
169  }
170  return dist;
171 }
double y
Definition: qgspointxy.h:48
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool operator==(const QgsPointXY &other)
equality operator
Definition: qgspointxy.cpp:109
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
Definition: qgspointxy.cpp:45
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:251
double azimuth(const QgsPointXY &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north) ...
Definition: qgspointxy.cpp:88
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspointxy.cpp:68
bool compare(const QgsPointXY &other, double epsilon=4 *DBL_EPSILON) const
Compares this point with another point with a fuzzy tolerance.
Definition: qgspointxy.cpp:103
QgsPointXY & operator=(const QgsPointXY &other)
Assignment.
Definition: qgspointxy.cpp:119
bool operator!=(const QgsPointXY &other) const
Inequality operator.
Definition: qgspointxy.cpp:114
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.cpp:40
double sqrDistToSegment(double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon=DEFAULT_SEGMENT_EPSILON) const
Returns the minimum distance between this point and a segment.
Definition: qgspointxy.cpp:136
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:237
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:113
double x() const
Get the x value of the point.
Definition: qgspointxy.h:129
void multiply(double scalar)
Multiply x and y by the given value.
Definition: qgspointxy.cpp:130
QgsPointXY project(double distance, double bearing) const
Returns a new point which corresponds to this point projected by a specified distance in a specified ...
Definition: qgspointxy.cpp:95
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspointxy.cpp:78
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
void setX(double x)
Sets the x value of the point.
Definition: qgspointxy.h:104
double x
Definition: qgspointxy.h:47
QgsPointXY()=default
Default constructor.
double y() const
Get the y value of the point.
Definition: qgspointxy.h:138
QString asWkt() const
Return the well known text representation for the point (e.g.
Definition: qgspointxy.cpp:63