QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
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
21#include <cmath>
22
23#include "qgsexception.h"
24#include "qgspoint.h"
25
26#include <QObject>
27#include <QString>
28#include <QTextStream>
29
30#include "moc_qgspointxy.cpp"
31
32using namespace Qt::StringLiterals;
33
35 : mX( p.x() )
36 , mY( p.y() )
37 , mIsEmpty( p.isEmpty() )
38{}
39
41{
42 if ( point.isEmpty() )
43 {
44 mX = 0.0;
45 mY = 0.0;
46 mIsEmpty = true;
47 }
48 else
49 {
50 mX = point.x();
51 mY = point.y();
52 mIsEmpty = false;
53 }
54}
55
56QString QgsPointXY::toString( int precision ) const
57{
58 if ( precision < 0 )
59 {
60 QString rep;
61 QTextStream ot( &rep );
62 ot.setRealNumberPrecision( 12 );
63 ot << mX << ", " << mY;
64 return rep;
65 }
66 else
67 {
68 const QString x = std::isfinite( mX ) ? QString::number( mX, 'f', precision ) : QObject::tr( "infinite" );
69 const QString y = std::isfinite( mY ) ? QString::number( mY, 'f', precision ) : QObject::tr( "infinite" );
70 return u"%1,%2"_s.arg( x, y );
71 }
72}
73
74QString QgsPointXY::asWkt() const
75{
76 QString wkt = u"POINT"_s;
77 if ( isEmpty() )
78 wkt += " EMPTY"_L1;
79 else
80 wkt += u"(%1 %2)"_s.arg( qgsDoubleToString( mX ), qgsDoubleToString( mY ) );
81
82 return wkt;
83}
84
85double QgsPointXY::azimuth( const QgsPointXY &other ) const
86{
87 const double dx = other.x() - mX;
88 const double dy = other.y() - mY;
89 return ( std::atan2( dx, dy ) * 180.0 / M_PI );
90}
91
92QgsPointXY QgsPointXY::project( double distance, double bearing ) const
93{
94 const double rads = bearing * M_PI / 180.0;
95 const double dx = distance * std::sin( rads );
96 const double dy = distance * std::cos( rads );
97 return QgsPointXY( mX + dx, mY + dy );
98}
99
100double QgsPointXY::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon ) const
101{
102 double nx, ny; //normal vector
103
104 nx = y2 - y1;
105 ny = -( x2 - x1 );
106
107 double t;
108 t = ( mX * ny - mY * nx - x1 * ny + y1 * nx ) / ( ( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
109
110 if ( t < 0.0 )
111 {
112 minDistPoint.setX( x1 );
113 minDistPoint.setY( y1 );
114 }
115 else if ( t > 1.0 )
116 {
117 minDistPoint.setX( x2 );
118 minDistPoint.setY( y2 );
119 }
120 else
121 {
122 minDistPoint.setX( x1 + t * ( x2 - x1 ) );
123 minDistPoint.setY( y1 + t * ( y2 - y1 ) );
124 }
125
126 const double dist = sqrDist( minDistPoint );
127 //prevent rounding errors if the point is directly on the segment
128 if ( qgsDoubleNear( dist, 0.0, epsilon ) )
129 {
130 minDistPoint.setX( mX );
131 minDistPoint.setY( mY );
132 return 0.0;
133 }
134 return dist;
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 ...
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition qgspointxy.h:189
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition qgspointxy.h:209
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:132
QString asWkt() const
Returns the well known text representation for the point (e.g.
double azimuth(const QgsPointXY &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north).
QgsPointXY()=default
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
double sqrDistToSegment(double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon=Qgis::DEFAULT_SEGMENT_EPSILON) const
Returns the minimum distance between this point and a segment.
void setX(double x)
Sets the x value of the point.
Definition qgspointxy.h:122
bool isEmpty() const
Returns true if the geometry is empty.
Definition qgspointxy.h:245
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
double x
Definition qgspoint.h:56
bool isEmpty() const override
Returns true if the geometry is empty.
Definition qgspoint.cpp:766
double y
Definition qgspoint.h:57
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6893
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975