Skip to content

Commit 77a8e18

Browse files
committedDec 8, 2016
Add methods to add/subtract QgsVectors
1 parent e41c2a7 commit 77a8e18

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed
 

‎python/core/qgspoint.sip

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ class QgsVector
3939
*/
4040
double operator*( QgsVector v ) const;
4141

42+
/**
43+
* Adds another vector to this vector.
44+
* @node added in QGIS 3.0
45+
*/
46+
QgsVector operator+( QgsVector other ) const;
47+
48+
/**
49+
* Adds another vector to this vector in place.
50+
* @node added in QGIS 3.0
51+
*/
52+
QgsVector& operator+=( QgsVector other );
53+
54+
/**
55+
* Subtracts another vector to this vector.
56+
* @node added in QGIS 3.0
57+
*/
58+
QgsVector operator-( QgsVector other ) const;
59+
60+
/**
61+
* Subtracts another vector to this vector in place.
62+
* @node added in QGIS 3.0
63+
*/
64+
QgsVector& operator-=( QgsVector other );
65+
4266
/** Returns the length of the vector.
4367
*/
4468
double length() const;

‎src/core/qgspoint.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ double QgsVector::operator*( QgsVector v ) const
6060
return mX * v.mX + mY * v.mY;
6161
}
6262

63+
QgsVector QgsVector::operator+( QgsVector other ) const
64+
{
65+
return QgsVector( mX + other.mX, mY + other.mY );
66+
}
67+
68+
QgsVector& QgsVector::operator+=( QgsVector other )
69+
{
70+
mX += other.mX;
71+
mY += other.mY;
72+
return *this;
73+
}
74+
75+
QgsVector QgsVector::operator-( QgsVector other ) const
76+
{
77+
return QgsVector( mX - other.mX, mY - other.mY );
78+
}
79+
80+
QgsVector& QgsVector::operator-=( QgsVector other )
81+
{
82+
mX -= other.mX;
83+
mY -= other.mY;
84+
return *this;
85+
}
86+
6387
double QgsVector::length() const
6488
{
6589
return sqrt( mX * mX + mY * mY );

‎src/core/qgspoint.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,36 @@ class CORE_EXPORT QgsVector
5757
*/
5858
QgsVector operator/( double scalar ) const;
5959

60-
/** Returns the sum of the x component of this vector multiplied by the x component of another
61-
* vector plus the y component of this vector multipled by the y component of another vector.
60+
/** Returns the dot product of two vectors, which is the sum of the x component
61+
* of this vector multiplied by the x component of another
62+
* vector plus the y component of this vector multipled by the y component of another vector.
6263
*/
6364
double operator*( QgsVector v ) const;
6465

66+
/**
67+
* Adds another vector to this vector.
68+
* @note added in QGIS 3.0
69+
*/
70+
QgsVector operator+( QgsVector other ) const;
71+
72+
/**
73+
* Adds another vector to this vector in place.
74+
* @note added in QGIS 3.0
75+
*/
76+
QgsVector& operator+=( QgsVector other );
77+
78+
/**
79+
* Subtracts another vector to this vector.
80+
* @note added in QGIS 3.0
81+
*/
82+
QgsVector operator-( QgsVector other ) const;
83+
84+
/**
85+
* Subtracts another vector to this vector in place.
86+
* @note added in QGIS 3.0
87+
*/
88+
QgsVector& operator-=( QgsVector other );
89+
6590
/** Returns the length of the vector.
6691
*/
6792
double length() const;

‎tests/src/core/testqgspoint.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,23 @@ void TestQgsPoint::vector()
736736
QCOMPARE( QgsVector( 0, 2 ).normalized().y(), 1.0 );
737737
QCOMPARE( QgsVector( 2, 0 ).normalized().x(), 1.0 );
738738
QCOMPARE( QgsVector( 2, 0 ).normalized().y(), 0.0 );
739+
740+
// operator +, -
741+
v1 = QgsVector( 1, 3 );
742+
v2 = QgsVector( 2, 5 );
743+
QgsVector v3 = v1 + v2;
744+
QCOMPARE( v3.x(), 3.0 );
745+
QCOMPARE( v3.y(), 8.0 );
746+
v3 = v1 - v2;
747+
QCOMPARE( v3.x(), -1.0 );
748+
QCOMPARE( v3.y(), -2.0 );
749+
// operator +=, -=
750+
v1 += v2;
751+
QCOMPARE( v1.x(), 3.0 );
752+
QCOMPARE( v1.y(), 8.0 );
753+
v1 -= v2;
754+
QCOMPARE( v1.x(), 1.0 );
755+
QCOMPARE( v1.y(), 3.0 );
739756
}
740757

741758
QTEST_MAIN( TestQgsPoint )

0 commit comments

Comments
 (0)
Please sign in to comment.