Skip to content
Permalink
Browse files

Port some API from QgsPoint to QgsPointV2

  • Loading branch information
nyalldawson committed Nov 7, 2016
1 parent dae0a01 commit 983fe24806b47d4c8a9aa6d226685be3fb8de90f
@@ -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;
@@ -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 );
}
@@ -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" ); }
@@ -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()

0 comments on commit 983fe24

Please sign in to comment.
You can’t perform that action at this time.