QGIS API Documentation  3.6.0-Noosa (5873452)
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 QString QgsPointXY::toString( int precision ) const
41 {
42  if ( precision < 0 )
43  {
44  QString rep;
45  QTextStream ot( &rep );
46  ot.setRealNumberPrecision( 12 );
47  ot << mX << ", " << mY;
48  return rep;
49  }
50  else
51  {
52  QString x = std::isfinite( mX ) ? QString::number( mX, 'f', precision ) : QObject::tr( "infinite" );
53  QString y = std::isfinite( mY ) ? QString::number( mY, 'f', precision ) : QObject::tr( "infinite" );
54  return QStringLiteral( "%1,%2" ).arg( x, y );
55  }
56 }
57 
58 QString QgsPointXY::asWkt() const
59 {
60  return QStringLiteral( "POINT(%1 %2)" ).arg( qgsDoubleToString( mX ), qgsDoubleToString( mY ) );
61 }
62 
63 double QgsPointXY::azimuth( const QgsPointXY &other ) const
64 {
65  double dx = other.x() - mX;
66  double dy = other.y() - mY;
67  return ( std::atan2( dx, dy ) * 180.0 / M_PI );
68 }
69 
70 QgsPointXY QgsPointXY::project( double distance, double bearing ) const
71 {
72  double rads = bearing * M_PI / 180.0;
73  double dx = distance * std::sin( rads );
74  double dy = distance * std::cos( rads );
75  return QgsPointXY( mX + dx, mY + dy );
76 }
77 
78 double QgsPointXY::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon ) const
79 {
80  double nx, ny; //normal vector
81 
82  nx = y2 - y1;
83  ny = -( x2 - x1 );
84 
85  double t;
86  t = ( mX * ny - mY * nx - x1 * ny + y1 * nx ) / ( ( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
87 
88  if ( t < 0.0 )
89  {
90  minDistPoint.setX( x1 );
91  minDistPoint.setY( y1 );
92  }
93  else if ( t > 1.0 )
94  {
95  minDistPoint.setX( x2 );
96  minDistPoint.setY( y2 );
97  }
98  else
99  {
100  minDistPoint.setX( x1 + t * ( x2 - x1 ) );
101  minDistPoint.setY( y1 + t * ( y2 - y1 ) );
102  }
103 
104  double dist = sqrDist( minDistPoint );
105  //prevent rounding errors if the point is directly on the segment
106  if ( qgsDoubleNear( dist, 0.0, epsilon ) )
107  {
108  minDistPoint.setX( mX );
109  minDistPoint.setY( mY );
110  return 0.0;
111  }
112  return dist;
113 }
int precision
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:265
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
Definition: qgspointxy.cpp:40
double azimuth(const QgsPointXY &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north) ...
Definition: qgspointxy.cpp:63
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspointxy.h:169
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:78
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:225
void setY(double y)
Sets the y value of the point.
Definition: qgspointxy.h:113
double x() const
Gets the x value of the point.
Definition: qgspointxy.h:129
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:70
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspointxy.h:190
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
Gets the y value of the point.
Definition: qgspointxy.h:138
QString asWkt() const
Returns the well known text representation for the point (e.g.
Definition: qgspointxy.cpp:58