Skip to content

Commit

Permalink
Add methods to add/subtract QgsVectors
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 8, 2016
1 parent e41c2a7 commit 77a8e18
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
24 changes: 24 additions & 0 deletions python/core/qgspoint.sip
Expand Up @@ -39,6 +39,30 @@ class QgsVector
*/
double operator*( QgsVector v ) const;

/**
* Adds another vector to this vector.
* @node added in QGIS 3.0
*/
QgsVector operator+( QgsVector other ) const;

/**
* Adds another vector to this vector in place.
* @node added in QGIS 3.0
*/
QgsVector& operator+=( QgsVector other );

/**
* Subtracts another vector to this vector.
* @node added in QGIS 3.0
*/
QgsVector operator-( QgsVector other ) const;

/**
* Subtracts another vector to this vector in place.
* @node added in QGIS 3.0
*/
QgsVector& operator-=( QgsVector other );

/** Returns the length of the vector.
*/
double length() const;
Expand Down
24 changes: 24 additions & 0 deletions src/core/qgspoint.cpp
Expand Up @@ -60,6 +60,30 @@ double QgsVector::operator*( QgsVector v ) const
return mX * v.mX + mY * v.mY;
}

QgsVector QgsVector::operator+( QgsVector other ) const
{
return QgsVector( mX + other.mX, mY + other.mY );
}

QgsVector& QgsVector::operator+=( QgsVector other )
{
mX += other.mX;
mY += other.mY;
return *this;
}

QgsVector QgsVector::operator-( QgsVector other ) const
{
return QgsVector( mX - other.mX, mY - other.mY );
}

QgsVector& QgsVector::operator-=( QgsVector other )
{
mX -= other.mX;
mY -= other.mY;
return *this;
}

double QgsVector::length() const
{
return sqrt( mX * mX + mY * mY );
Expand Down
29 changes: 27 additions & 2 deletions src/core/qgspoint.h
Expand Up @@ -57,11 +57,36 @@ class CORE_EXPORT QgsVector
*/
QgsVector operator/( double scalar ) const;

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

/**
* Adds another vector to this vector.
* @note added in QGIS 3.0
*/
QgsVector operator+( QgsVector other ) const;

/**
* Adds another vector to this vector in place.
* @note added in QGIS 3.0
*/
QgsVector& operator+=( QgsVector other );

/**
* Subtracts another vector to this vector.
* @note added in QGIS 3.0
*/
QgsVector operator-( QgsVector other ) const;

/**
* Subtracts another vector to this vector in place.
* @note added in QGIS 3.0
*/
QgsVector& operator-=( QgsVector other );

/** Returns the length of the vector.
*/
double length() const;
Expand Down
17 changes: 17 additions & 0 deletions tests/src/core/testqgspoint.cpp
Expand Up @@ -736,6 +736,23 @@ void TestQgsPoint::vector()
QCOMPARE( QgsVector( 0, 2 ).normalized().y(), 1.0 );
QCOMPARE( QgsVector( 2, 0 ).normalized().x(), 1.0 );
QCOMPARE( QgsVector( 2, 0 ).normalized().y(), 0.0 );

// operator +, -
v1 = QgsVector( 1, 3 );
v2 = QgsVector( 2, 5 );
QgsVector v3 = v1 + v2;
QCOMPARE( v3.x(), 3.0 );
QCOMPARE( v3.y(), 8.0 );
v3 = v1 - v2;
QCOMPARE( v3.x(), -1.0 );
QCOMPARE( v3.y(), -2.0 );
// operator +=, -=
v1 += v2;
QCOMPARE( v1.x(), 3.0 );
QCOMPARE( v1.y(), 8.0 );
v1 -= v2;
QCOMPARE( v1.x(), 1.0 );
QCOMPARE( v1.y(), 3.0 );
}

QTEST_MAIN( TestQgsPoint )
Expand Down

0 comments on commit 77a8e18

Please sign in to comment.