File tree Expand file tree Collapse file tree 4 files changed +92
-2
lines changed Expand file tree Collapse file tree 4 files changed +92
-2
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,30 @@ class QgsVector
39
39
*/
40
40
double operator*( QgsVector v ) const;
41
41
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
+
42
66
/** Returns the length of the vector.
43
67
*/
44
68
double length() const;
Original file line number Diff line number Diff line change @@ -60,6 +60,30 @@ double QgsVector::operator*( QgsVector v ) const
60
60
return mX * v.mX + mY * v.mY ;
61
61
}
62
62
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
+
63
87
double QgsVector::length () const
64
88
{
65
89
return sqrt ( mX * mX + mY * mY );
Original file line number Diff line number Diff line change @@ -57,11 +57,36 @@ class CORE_EXPORT QgsVector
57
57
*/
58
58
QgsVector operator /( double scalar ) const ;
59
59
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.
62
63
*/
63
64
double operator *( QgsVector v ) const ;
64
65
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
+
65
90
/* * Returns the length of the vector.
66
91
*/
67
92
double length () const ;
Original file line number Diff line number Diff line change @@ -736,6 +736,23 @@ void TestQgsPoint::vector()
736
736
QCOMPARE ( QgsVector ( 0 , 2 ).normalized ().y (), 1.0 );
737
737
QCOMPARE ( QgsVector ( 2 , 0 ).normalized ().x (), 1.0 );
738
738
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 );
739
756
}
740
757
741
758
QTEST_MAIN ( TestQgsPoint )
You can’t perform that action at this time.
0 commit comments