QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
51QString 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 const QString x = std::isfinite( mX ) ? QString::number( mX, 'f', precision ) : QObject::tr( "infinite" );
64 const QString y = std::isfinite( mY ) ? QString::number( mY, 'f', precision ) : QObject::tr( "infinite" );
65 return QStringLiteral( "%1,%2" ).arg( x, y );
66 }
67}
68
69QString QgsPointXY::asWkt() const
70{
71 QString wkt = QStringLiteral( "POINT" );
72 if ( isEmpty() )
73 wkt += QLatin1String( " EMPTY" );
74 else
75 wkt += QStringLiteral( "(%1 %2)" ).arg( qgsDoubleToString( mX ), qgsDoubleToString( mY ) );
76
77 return wkt;
78}
79
80double QgsPointXY::azimuth( const QgsPointXY &other ) const
81{
82 const double dx = other.x() - mX;
83 const double dy = other.y() - mY;
84 return ( std::atan2( dx, dy ) * 180.0 / M_PI );
85}
86
87QgsPointXY QgsPointXY::project( double distance, double bearing ) const
88{
89 const double rads = bearing * M_PI / 180.0;
90 const double dx = distance * std::sin( rads );
91 const double dy = distance * std::cos( rads );
92 return QgsPointXY( mX + dx, mY + dy );
93}
94
95double 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 const 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}
A class to represent a 2D point.
Definition: qgspointxy.h:59
bool isEmpty() const SIP_HOLDGIL
Returns true if the geometry is empty.
Definition: qgspointxy.h:249
QgsPointXY project(double distance, double bearing) const SIP_HOLDGIL
Returns a new point which corresponds to this point projected by a specified distance in a specified ...
Definition: qgspointxy.cpp:87
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
Definition: qgspointxy.cpp:51
double sqrDist(double x, double y) const SIP_HOLDGIL
Returns the squared distance between this point a specified x, y coordinate.
Definition: qgspointxy.h:190
QString asWkt() const
Returns the well known text representation for the point (e.g.
Definition: qgspointxy.cpp:69
void setX(double x) SIP_HOLDGIL
Sets the x value of the point.
Definition: qgspointxy.h:122
double azimuth(const QgsPointXY &other) const SIP_HOLDGIL
Calculates azimuth between this point and other one (clockwise in degree, starting from north)
Definition: qgspointxy.cpp:80
QgsPointXY()=default
Default constructor.
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
double distance(double x, double y) const SIP_HOLDGIL
Returns the distance between this point and a specified x, y coordinate.
Definition: qgspointxy.h:211
void setY(double y) SIP_HOLDGIL
Sets the y value of the point.
Definition: qgspointxy.h:132
double sqrDistToSegment(double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon=DEFAULT_SEGMENT_EPSILON) const SIP_HOLDGIL
Returns the minimum distance between this point and a segment.
Definition: qgspointxy.cpp:95
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
Definition: qgspoint.cpp:767
Q_GADGET double x
Definition: qgspoint.h:52
double y
Definition: qgspoint.h:53
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:2466
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2527
int precision