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