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