Skip to content

Commit

Permalink
Add some more QgsVector operators, allow use of QgsVector with QgsPoi…
Browse files Browse the repository at this point in the history
…ntV2
  • Loading branch information
nyalldawson committed Dec 8, 2016
1 parent 77a8e18 commit 379e7a4
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
31 changes: 31 additions & 0 deletions python/core/geometry/qgspointv2.sip
Expand Up @@ -179,6 +179,37 @@ class QgsPointV2: public QgsAbstractGeometry
*/
double azimuth( const QgsPointV2& other ) const;

/**
* Calculates the vector obtained by subtracting a point from this point.
* @note added in QGIS 3.0
*/
QgsVector operator-( const QgsPointV2& p ) const;

/**
* Adds a vector to this point in place.
* @note added in QGIS 3.0
*/
QgsPointV2 &operator+=( QgsVector v );

/**
* Subtracts a vector from this point in place.
* @note added in QGIS 3.0
*/
QgsPointV2 &operator-=( QgsVector v );

/**
* Adds a vector to this point.
* @note added in QGIS 3.0
*/
QgsPointV2 operator+( QgsVector v ) const;

/**
* Subtracts a vector from this point.
* @note added in QGIS 3.0
*/
QgsPointV2 operator-( QgsVector v ) const;


//implementation of inherited methods
virtual QgsRectangle boundingBox() const;
virtual QString geometryType() const;
Expand Down
6 changes: 6 additions & 0 deletions python/core/qgspoint.sip
Expand Up @@ -98,6 +98,12 @@ class QgsVector
* if called on a vector with length of 0.
*/
QgsVector normalized() const;

//! Equality operator
bool operator==( QgsVector other ) const;

//! Inequality operator
bool operator!=( QgsVector other ) const;
};


Expand Down
30 changes: 30 additions & 0 deletions src/core/geometry/qgspointv2.h
Expand Up @@ -192,6 +192,36 @@ class CORE_EXPORT QgsPointV2: public QgsAbstractGeometry
*/
double azimuth( const QgsPointV2& other ) const;

/**
* Calculates the vector obtained by subtracting a point from this point.
* @note added in QGIS 3.0
*/
QgsVector operator-( const QgsPointV2& p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }

/**
* Adds a vector to this point in place.
* @note added in QGIS 3.0
*/
QgsPointV2 &operator+=( QgsVector v ) { mX += v.x(); mY += v.y(); return *this; }

/**
* Subtracts a vector from this point in place.
* @note added in QGIS 3.0
*/
QgsPointV2 &operator-=( QgsVector v ) { mX -= v.x(); mY -= v.y(); return *this; }

/**
* Adds a vector to this point.
* @note added in QGIS 3.0
*/
QgsPointV2 operator+( QgsVector v ) const { QgsPointV2 r = *this; r.rx() += v.x(); r.ry() += v.y(); return r; }

/**
* Subtracts a vector from this point.
* @note added in QGIS 3.0
*/
QgsPointV2 operator-( QgsVector v ) const { QgsPointV2 r = *this; r.rx() -= v.x(); r.ry() -= v.y(); return r; }

//implementation of inherited methods
virtual QgsRectangle boundingBox() const override { return QgsRectangle( mX, mY, mX, mY ); }
virtual QString geometryType() const override { return QStringLiteral( "Point" ); }
Expand Down
10 changes: 10 additions & 0 deletions src/core/qgspoint.cpp
Expand Up @@ -134,6 +134,16 @@ QgsVector QgsVector::normalized() const
return *this / len;
}

bool QgsVector::operator==( QgsVector other ) const
{
return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
}

bool QgsVector::operator!=( QgsVector other ) const
{
return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
}


//
// QgsPoint
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgspoint.h
Expand Up @@ -123,6 +123,12 @@ class CORE_EXPORT QgsVector
*/
QgsVector normalized() const;

//! Equality operator
bool operator==( QgsVector other ) const;

//! Inequality operator
bool operator!=( QgsVector other ) const;

private:

double mX, mY;
Expand Down
15 changes: 15 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -820,6 +820,21 @@ void TestQgsGeometry::point()
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 );

// operators
QgsPointV2 p31( 1, 2 );
QgsPointV2 p32( 3, 5 );
QCOMPARE( p32 - p31, QgsVector( 2, 3 ) );
QCOMPARE( p31 - p32, QgsVector( -2, -3 ) );

p31 = QgsPointV2( 1, 2 );
QCOMPARE( p31 + QgsVector( 3, 5 ), QgsPointV2( 4, 7 ) );
p31 += QgsVector( 3, 5 );
QCOMPARE( p31, QgsPointV2( 4, 7 ) );

QCOMPARE( p31 - QgsVector( 3, 5 ), QgsPointV2( 1 , 2 ) );
p31 -= QgsVector( 3, 5 );
QCOMPARE( p31, QgsPointV2( 1, 2 ) );
}

void TestQgsGeometry::lineString()
Expand Down
5 changes: 5 additions & 0 deletions tests/src/core/testqgspoint.cpp
Expand Up @@ -681,6 +681,11 @@ void TestQgsPoint::project()

void TestQgsPoint::vector()
{
//equality
QVERIFY( QgsVector( 1, 2 ) == QgsVector( 1, 2 ) );
QVERIFY( QgsVector( 1, 2 ) != QgsVector( 3, 2 ) );
QVERIFY( QgsVector( 1, 2 ) != QgsVector( 1, 3 ) );

//test constructors, x(), y() accessors
QgsVector v1;
QCOMPARE( v1.x(), 0.0 );
Expand Down

0 comments on commit 379e7a4

Please sign in to comment.