Add methods to add/subtract QgsVectors

This commit is contained in:
Nyall Dawson 2016-12-08 16:46:03 +10:00
parent e41c2a7b05
commit 77a8e18ec1
4 changed files with 92 additions and 2 deletions

View File

@ -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;

View File

@ -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 );

View File

@ -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;

View File

@ -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 )