QGIS API Documentation 3.99.0-Master (d270888f95f)
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}
40
42{
43 if ( point.isEmpty() )
44 {
45 mX = 0.0;
46 mY = 0.0;
47 mIsEmpty = true;
48 }
49 else
50 {
51 mX = point.x();
52 mY = point.y();
53 mIsEmpty = false;
54 }
55}
56
57QString QgsPointXY::toString( int precision ) const
58{
59 if ( precision < 0 )
60 {
61 QString rep;
62 QTextStream ot( &rep );
63 ot.setRealNumberPrecision( 12 );
64 ot << mX << ", " << mY;
65 return rep;
66 }
67 else
68 {
69 const QString x = std::isfinite( mX ) ? QString::number( mX, 'f', precision ) : QObject::tr( "infinite" );
70 const QString y = std::isfinite( mY ) ? QString::number( mY, 'f', precision ) : QObject::tr( "infinite" );
71 return u"%1,%2"_s.arg( x, y );
72 }
73}
74
75QString QgsPointXY::asWkt() const
76{
77 QString wkt = u"POINT"_s;
78 if ( isEmpty() )
79 wkt += " EMPTY"_L1;
80 else
81 wkt += u"(%1 %2)"_s.arg( qgsDoubleToString( mX ), qgsDoubleToString( mY ) );
82
83 return wkt;
84}
85
86double QgsPointXY::azimuth( const QgsPointXY &other ) const
87{
88 const double dx = other.x() - mX;
89 const double dy = other.y() - mY;
90 return ( std::atan2( dx, dy ) * 180.0 / M_PI );
91}
92
93QgsPointXY QgsPointXY::project( double distance, double bearing ) const
94{
95 const double rads = bearing * M_PI / 180.0;
96 const double dx = distance * std::sin( rads );
97 const double dy = distance * std::cos( rads );
98 return QgsPointXY( mX + dx, mY + dy );
99}
100
101double QgsPointXY::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon ) const
102{
103 double nx, ny; //normal vector
104
105 nx = y2 - y1;
106 ny = -( x2 - x1 );
107
108 double t;
109 t = ( mX * ny - mY * nx - x1 * ny + y1 * nx ) / ( ( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
110
111 if ( t < 0.0 )
112 {
113 minDistPoint.setX( x1 );
114 minDistPoint.setY( y1 );
115 }
116 else if ( t > 1.0 )
117 {
118 minDistPoint.setX( x2 );
119 minDistPoint.setY( y2 );
120 }
121 else
122 {
123 minDistPoint.setX( x1 + t * ( x2 - x1 ) );
124 minDistPoint.setY( y1 + t * ( y2 - y1 ) );
125 }
126
127 const double dist = sqrDist( minDistPoint );
128 //prevent rounding errors if the point is directly on the segment
129 if ( qgsDoubleNear( dist, 0.0, epsilon ) )
130 {
131 minDistPoint.setX( mX );
132 minDistPoint.setY( mY );
133 return 0.0;
134 }
135 return dist;
136}
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:188
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
Definition qgspointxy.h:208
void setY(double y)
Sets the y value of the point.
Definition qgspointxy.h:131
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:121
bool isEmpty() const
Returns true if the geometry is empty.
Definition qgspointxy.h:244
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:742
double y
Definition qgspoint.h:57
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6817
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900