From 983fe24806b47d4c8a9aa6d226685be3fb8de90f Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sat, 5 Nov 2016 20:11:15 +1000 Subject: [PATCH] Port some API from QgsPoint to QgsPointV2 --- python/core/geometry/qgspointv2.sip | 41 +++++++++++++++++++++++++++++ src/core/geometry/qgspointv2.cpp | 27 +++++++++++++++++++ src/core/geometry/qgspointv2.h | 41 +++++++++++++++++++++++++++++ tests/src/core/testqgsgeometry.cpp | 30 +++++++++++++++++++++ 4 files changed, 139 insertions(+) diff --git a/python/core/geometry/qgspointv2.sip b/python/core/geometry/qgspointv2.sip index 2df14c504c5..5c774d47214 100644 --- a/python/core/geometry/qgspointv2.sip +++ b/python/core/geometry/qgspointv2.sip @@ -138,6 +138,47 @@ class QgsPointV2: public QgsAbstractGeometry */ QPointF toQPointF() const; + /** + * Returns the distance between this point and a specified x, y coordinate. In certain + * cases it may be more appropriate to call the faster distanceSquared() method, eg + * when comparing distances. + * @note added in QGIS 3.0 + * @see distanceSquared() + */ + double distance( double x, double y ) const; + + /** + * Returns the 2D distance between this point and another point. In certain + * cases it may be more appropriate to call the faster distanceSquared() method, eg + * when comparing distances. + * @note added in QGIS 3.0 + */ + double distance( const QgsPointV2& other ) const; + + /** + * Returns the squared distance between this point a specified x, y coordinate. Calling + * this is faster than calling distance(), and may be useful in use cases such as comparing + * distances where the extra expense of calling distance() is not required. + * @see distance() + * @note added in QGIS 3.0 + */ + double distanceSquared( double x, double y ) const; + + /** + * Returns the squared distance between this point another point. Calling + * this is faster than calling distance(), and may be useful in use cases such as comparing + * distances where the extra expense of calling distance() is not required. + * @see distance() + * @note added in QGIS 3.0 + */ + double distanceSquared( const QgsPointV2& other ) const; + + /** + * Calculates azimuth between this point and other one (clockwise in degree, starting from north) + * @note added in QGIS 3.0 + */ + double azimuth( const QgsPointV2& other ) const; + //implementation of inherited methods virtual QgsRectangle boundingBox() const; virtual QString geometryType() const; diff --git a/src/core/geometry/qgspointv2.cpp b/src/core/geometry/qgspointv2.cpp index 246ee968380..9a2e2c768d3 100644 --- a/src/core/geometry/qgspointv2.cpp +++ b/src/core/geometry/qgspointv2.cpp @@ -439,3 +439,30 @@ QPointF QgsPointV2::toQPointF() const { return QPointF( mX, mY ); } + +double QgsPointV2::distance( double x, double y ) const +{ + return sqrt(( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) ); +} + +double QgsPointV2::distance( const QgsPointV2& other ) const +{ + return sqrt(( mX - other.x() ) * ( mX - other.x() ) + ( mY - other.y() ) * ( mY - other.y() ) ); +} + +double QgsPointV2::distanceSquared( double x, double y ) const +{ + return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ); +} + +double QgsPointV2::distanceSquared( const QgsPointV2& other ) const +{ + return ( mX - other.x() ) * ( mX - other.x() ) + ( mY - other.y() ) * ( mY - other.y() ) ; +} + +double QgsPointV2::azimuth( const QgsPointV2& other ) const +{ + double dx = other.x() - mX; + double dy = other.y() - mY; + return ( atan2( dx, dy ) * 180.0 / M_PI ); +} diff --git a/src/core/geometry/qgspointv2.h b/src/core/geometry/qgspointv2.h index b8d00622f4c..cd034a1fd5a 100644 --- a/src/core/geometry/qgspointv2.h +++ b/src/core/geometry/qgspointv2.h @@ -151,6 +151,47 @@ class CORE_EXPORT QgsPointV2: public QgsAbstractGeometry */ QPointF toQPointF() const; + /** + * Returns the distance between this point and a specified x, y coordinate. In certain + * cases it may be more appropriate to call the faster distanceSquared() method, eg + * when comparing distances. + * @note added in QGIS 3.0 + * @see distanceSquared() + */ + double distance( double x, double y ) const; + + /** + * Returns the 2D distance between this point and another point. In certain + * cases it may be more appropriate to call the faster distanceSquared() method, eg + * when comparing distances. + * @note added in QGIS 3.0 + */ + double distance( const QgsPointV2& other ) const; + + /** + * Returns the squared distance between this point a specified x, y coordinate. Calling + * this is faster than calling distance(), and may be useful in use cases such as comparing + * distances where the extra expense of calling distance() is not required. + * @see distance() + * @note added in QGIS 3.0 + */ + double distanceSquared( double x, double y ) const; + + /** + * Returns the squared distance between this point another point. Calling + * this is faster than calling distance(), and may be useful in use cases such as comparing + * distances where the extra expense of calling distance() is not required. + * @see distance() + * @note added in QGIS 3.0 + */ + double distanceSquared( const QgsPointV2& other ) const; + + /** + * Calculates azimuth between this point and other one (clockwise in degree, starting from north) + * @note added in QGIS 3.0 + */ + double azimuth( const QgsPointV2& other ) const; + //implementation of inherited methods virtual QgsRectangle boundingBox() const override { return QgsRectangle( mX, mY, mX, mY ); } virtual QString geometryType() const override { return QStringLiteral( "Point" ); } diff --git a/tests/src/core/testqgsgeometry.cpp b/tests/src/core/testqgsgeometry.cpp index ac0f1247f72..255a4905a76 100644 --- a/tests/src/core/testqgsgeometry.cpp +++ b/tests/src/core/testqgsgeometry.cpp @@ -792,6 +792,36 @@ void TestQgsGeometry::point() //boundary QgsPointV2 p30( 1.0, 2.0 ); QVERIFY( !p30.boundary() ); + + // distance + QCOMPARE( QgsPointV2( 1, 2 ).distance( QgsPointV2( 2, 2 ) ), 1.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distance( 2, 2 ), 1.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distance( QgsPointV2( 3, 2 ) ), 2.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distance( 3, 2 ), 2.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distance( QgsPointV2( 1, 3 ) ), 1.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distance( 1, 3 ), 1.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distance( QgsPointV2( 1, 4 ) ), 2.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distance( 1, 4 ), 2.0 ); + QCOMPARE( QgsPointV2( 1, -2 ).distance( QgsPointV2( 1, -4 ) ), 2.0 ); + QCOMPARE( QgsPointV2( 1, -2 ).distance( 1, -4 ), 2.0 ); + + QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( QgsPointV2( 2, 2 ) ), 1.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( 2, 2 ), 1.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( QgsPointV2( 3, 2 ) ), 4.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( 3, 2 ), 4.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( QgsPointV2( 1, 3 ) ), 1.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( 1, 3 ), 1.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( QgsPointV2( 1, 4 ) ), 4.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( 1, 4 ), 4.0 ); + QCOMPARE( QgsPointV2( 1, -2 ).distanceSquared( QgsPointV2( 1, -4 ) ), 4.0 ); + QCOMPARE( QgsPointV2( 1, -2 ).distanceSquared( 1, -4 ), 4.0 ); + + // azimuth + QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 1, 2 ) ), 0.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 1, 3 ) ), 0.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 2, 2 ) ), 90.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 1, 0 ) ), 180.0 ); + QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 0, 2 ) ), -90.0 ); } void TestQgsGeometry::lineString()