Skip to content

Commit 379e7a4

Browse files
committedDec 8, 2016
Add some more QgsVector operators, allow use of QgsVector with QgsPointV2
1 parent 77a8e18 commit 379e7a4

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed
 

‎python/core/geometry/qgspointv2.sip

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,37 @@ class QgsPointV2: public QgsAbstractGeometry
179179
*/
180180
double azimuth( const QgsPointV2& other ) const;
181181

182+
/**
183+
* Calculates the vector obtained by subtracting a point from this point.
184+
* @note added in QGIS 3.0
185+
*/
186+
QgsVector operator-( const QgsPointV2& p ) const;
187+
188+
/**
189+
* Adds a vector to this point in place.
190+
* @note added in QGIS 3.0
191+
*/
192+
QgsPointV2 &operator+=( QgsVector v );
193+
194+
/**
195+
* Subtracts a vector from this point in place.
196+
* @note added in QGIS 3.0
197+
*/
198+
QgsPointV2 &operator-=( QgsVector v );
199+
200+
/**
201+
* Adds a vector to this point.
202+
* @note added in QGIS 3.0
203+
*/
204+
QgsPointV2 operator+( QgsVector v ) const;
205+
206+
/**
207+
* Subtracts a vector from this point.
208+
* @note added in QGIS 3.0
209+
*/
210+
QgsPointV2 operator-( QgsVector v ) const;
211+
212+
182213
//implementation of inherited methods
183214
virtual QgsRectangle boundingBox() const;
184215
virtual QString geometryType() const;

‎python/core/qgspoint.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ class QgsVector
9898
* if called on a vector with length of 0.
9999
*/
100100
QgsVector normalized() const;
101+
102+
//! Equality operator
103+
bool operator==( QgsVector other ) const;
104+
105+
//! Inequality operator
106+
bool operator!=( QgsVector other ) const;
101107
};
102108

103109

‎src/core/geometry/qgspointv2.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,36 @@ class CORE_EXPORT QgsPointV2: public QgsAbstractGeometry
192192
*/
193193
double azimuth( const QgsPointV2& other ) const;
194194

195+
/**
196+
* Calculates the vector obtained by subtracting a point from this point.
197+
* @note added in QGIS 3.0
198+
*/
199+
QgsVector operator-( const QgsPointV2& p ) const { return QgsVector( mX - p.mX, mY - p.mY ); }
200+
201+
/**
202+
* Adds a vector to this point in place.
203+
* @note added in QGIS 3.0
204+
*/
205+
QgsPointV2 &operator+=( QgsVector v ) { mX += v.x(); mY += v.y(); return *this; }
206+
207+
/**
208+
* Subtracts a vector from this point in place.
209+
* @note added in QGIS 3.0
210+
*/
211+
QgsPointV2 &operator-=( QgsVector v ) { mX -= v.x(); mY -= v.y(); return *this; }
212+
213+
/**
214+
* Adds a vector to this point.
215+
* @note added in QGIS 3.0
216+
*/
217+
QgsPointV2 operator+( QgsVector v ) const { QgsPointV2 r = *this; r.rx() += v.x(); r.ry() += v.y(); return r; }
218+
219+
/**
220+
* Subtracts a vector from this point.
221+
* @note added in QGIS 3.0
222+
*/
223+
QgsPointV2 operator-( QgsVector v ) const { QgsPointV2 r = *this; r.rx() -= v.x(); r.ry() -= v.y(); return r; }
224+
195225
//implementation of inherited methods
196226
virtual QgsRectangle boundingBox() const override { return QgsRectangle( mX, mY, mX, mY ); }
197227
virtual QString geometryType() const override { return QStringLiteral( "Point" ); }

‎src/core/qgspoint.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ QgsVector QgsVector::normalized() const
134134
return *this / len;
135135
}
136136

137+
bool QgsVector::operator==( QgsVector other ) const
138+
{
139+
return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
140+
}
141+
142+
bool QgsVector::operator!=( QgsVector other ) const
143+
{
144+
return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
145+
}
146+
137147

138148
//
139149
// QgsPoint

‎src/core/qgspoint.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ class CORE_EXPORT QgsVector
123123
*/
124124
QgsVector normalized() const;
125125

126+
//! Equality operator
127+
bool operator==( QgsVector other ) const;
128+
129+
//! Inequality operator
130+
bool operator!=( QgsVector other ) const;
131+
126132
private:
127133

128134
double mX, mY;

‎tests/src/core/testqgsgeometry.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,21 @@ void TestQgsGeometry::point()
820820
QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 2, 2 ) ), 90.0 );
821821
QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 1, 0 ) ), 180.0 );
822822
QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 0, 2 ) ), -90.0 );
823+
824+
// operators
825+
QgsPointV2 p31( 1, 2 );
826+
QgsPointV2 p32( 3, 5 );
827+
QCOMPARE( p32 - p31, QgsVector( 2, 3 ) );
828+
QCOMPARE( p31 - p32, QgsVector( -2, -3 ) );
829+
830+
p31 = QgsPointV2( 1, 2 );
831+
QCOMPARE( p31 + QgsVector( 3, 5 ), QgsPointV2( 4, 7 ) );
832+
p31 += QgsVector( 3, 5 );
833+
QCOMPARE( p31, QgsPointV2( 4, 7 ) );
834+
835+
QCOMPARE( p31 - QgsVector( 3, 5 ), QgsPointV2( 1 , 2 ) );
836+
p31 -= QgsVector( 3, 5 );
837+
QCOMPARE( p31, QgsPointV2( 1, 2 ) );
823838
}
824839

825840
void TestQgsGeometry::lineString()

‎tests/src/core/testqgspoint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ void TestQgsPoint::project()
681681

682682
void TestQgsPoint::vector()
683683
{
684+
//equality
685+
QVERIFY( QgsVector( 1, 2 ) == QgsVector( 1, 2 ) );
686+
QVERIFY( QgsVector( 1, 2 ) != QgsVector( 3, 2 ) );
687+
QVERIFY( QgsVector( 1, 2 ) != QgsVector( 1, 3 ) );
688+
684689
//test constructors, x(), y() accessors
685690
QgsVector v1;
686691
QCOMPARE( v1.x(), 0.0 );

0 commit comments

Comments
 (0)
Please sign in to comment.