Port some API from QgsPoint to QgsPointV2

This commit is contained in:
Nyall Dawson 2016-11-05 20:11:15 +10:00
parent dae0a01761
commit 983fe24806
4 changed files with 139 additions and 0 deletions

View File

@ -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;

View File

@ -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 );
}

View File

@ -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" ); }

View File

@ -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()