QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  : mX( p.x() )
30  , mY( p.y() )
31  , mIsEmpty( p.isEmpty() )
32 {
33 }
34 
36 {
37  if ( point.isEmpty() )
38  {
39  mX = 0.0;
40  mY = 0.0;
41  mIsEmpty = true;
42  }
43  else
44  {
45  mX = point.x();
46  mY = point.y();
47  mIsEmpty = false;
48  }
49 }
50 
51 QString QgsPointXY::toString( int precision ) const
52 {
53  if ( precision < 0 )
54  {
55  QString rep;
56  QTextStream ot( &rep );
57  ot.setRealNumberPrecision( 12 );
58  ot << mX << ", " << mY;
59  return rep;
60  }
61  else
62  {
63  QString x = std::isfinite( mX ) ? QString::number( mX, 'f', precision ) : QObject::tr( "infinite" );
64  QString y = std::isfinite( mY ) ? QString::number( mY, 'f', precision ) : QObject::tr( "infinite" );
65  return QStringLiteral( "%1,%2" ).arg( x, y );
66  }
67 }
68 
69 QString QgsPointXY::asWkt() const
70 {
71  QString wkt = QStringLiteral( "POINT" );
72  if ( isEmpty() )
73  wkt += QStringLiteral( " EMPTY" );
74  else
75  wkt += QStringLiteral( "(%1 %2)" ).arg( qgsDoubleToString( mX ), qgsDoubleToString( mY ) );
76 
77  return wkt;
78 }
79 
80 double QgsPointXY::azimuth( const QgsPointXY &other ) const
81 {
82  double dx = other.x() - mX;
83  double dy = other.y() - mY;
84  return ( std::atan2( dx, dy ) * 180.0 / M_PI );
85 }
86 
87 QgsPointXY QgsPointXY::project( double distance, double bearing ) const
88 {
89  double rads = bearing * M_PI / 180.0;
90  double dx = distance * std::sin( rads );
91  double dy = distance * std::cos( rads );
92  return QgsPointXY( mX + dx, mY + dy );
93 }
94 
95 double QgsPointXY::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon ) const
96 {
97  double nx, ny; //normal vector
98 
99  nx = y2 - y1;
100  ny = -( x2 - x1 );
101 
102  double t;
103  t = ( mX * ny - mY * nx - x1 * ny + y1 * nx ) / ( ( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
104 
105  if ( t < 0.0 )
106  {
107  minDistPoint.setX( x1 );
108  minDistPoint.setY( y1 );
109  }
110  else if ( t > 1.0 )
111  {
112  minDistPoint.setX( x2 );
113  minDistPoint.setY( y2 );
114  }
115  else
116  {
117  minDistPoint.setX( x1 + t * ( x2 - x1 ) );
118  minDistPoint.setY( y1 + t * ( y2 - y1 ) );
119  }
120 
121  double dist = sqrDist( minDistPoint );
122  //prevent rounding errors if the point is directly on the segment
123  if ( qgsDoubleNear( dist, 0.0, epsilon ) )
124  {
125  minDistPoint.setX( mX );
126  minDistPoint.setY( mY );
127  return 0.0;
128  }
129  return dist;
130 }
int precision
double y
Definition: qgspoint.h:42
double y
Definition: qgspointxy.h:48
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:280
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
Definition: qgspointxy.cpp:51
double azimuth(const QgsPointXY &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north) ...
Definition: qgspointxy.cpp:80
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspointxy.h:175
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:95
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:240
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:117
double x() const
Gets the x value of the point.
Definition: qgspointxy.h:135
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:87
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspointxy.h:196
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:107
double x
Definition: qgspointxy.h:47
QgsPointXY()=default
Default constructor.
bool isEmpty() const override
Returns true if the geometry is empty.
Definition: qgspoint.cpp:702
double y() const
Gets the y value of the point.
Definition: qgspointxy.h:144
bool isEmpty() const
Returns true if the geometry is empty.
Definition: qgspointxy.h:234
QString asWkt() const
Returns the well known text representation for the point (e.g.
Definition: qgspointxy.cpp:69
double x
Definition: qgspoint.h:41