Skip to content
Permalink
Browse files

Add some more QgsVector operators, allow use of QgsVector with QgsPoi…

…ntV2
  • Loading branch information
nyalldawson committed Dec 8, 2016
1 parent 77a8e18 commit 379e7a42e674311f393c00226d947c23b64a8307
@@ -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;
@@ -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;
};


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

0 comments on commit 379e7a4

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