Skip to content
Permalink
Browse files

Add methods to add/subtract QgsVectors

  • Loading branch information
nyalldawson committed Dec 8, 2016
1 parent e41c2a7 commit 77a8e18ec1eaeec6cfb019cc72e8dafebf11e8a4
Showing with 92 additions and 2 deletions.
  1. +24 −0 python/core/qgspoint.sip
  2. +24 −0 src/core/qgspoint.cpp
  3. +27 −2 src/core/qgspoint.h
  4. +17 −0 tests/src/core/testqgspoint.cpp
@@ -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;
@@ -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 );
@@ -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;
@@ -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 )

0 comments on commit 77a8e18

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