Skip to content

Commit 7941759

Browse files
Zverik3nids
authored andcommitted
Rectangle moving operators
1 parent 6006359 commit 7941759

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

src/core/geometry/qgsrectangle.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,42 @@ void QgsRectangle::combineExtentWith( double x, double y )
222222
}
223223
}
224224

225+
QgsRectangle QgsRectangle::operator-( const QgsVector v ) const
226+
{
227+
double xmin = mXmin - v.x();
228+
double xmax = mXmax - v.x();
229+
double ymin = mYmin - v.y();
230+
double ymax = mYmax - v.y();
231+
return QgsRectangle( xmin, ymin, xmax, ymax );
232+
}
233+
234+
QgsRectangle QgsRectangle::operator+( const QgsVector v ) const
235+
{
236+
double xmin = mXmin + v.x();
237+
double xmax = mXmax + v.x();
238+
double ymin = mYmin + v.y();
239+
double ymax = mYmax + v.y();
240+
return QgsRectangle( xmin, ymin, xmax, ymax );
241+
}
242+
243+
QgsRectangle &QgsRectangle::operator-=( const QgsVector v )
244+
{
245+
mXmin -= v.x();
246+
mXmax -= v.x();
247+
mYmin -= v.y();
248+
mYmax -= v.y();
249+
return *this;
250+
}
251+
252+
QgsRectangle &QgsRectangle::operator+=( const QgsVector v )
253+
{
254+
mXmin += v.x();
255+
mXmax += v.x();
256+
mYmin += v.y();
257+
mYmax += v.y();
258+
return *this;
259+
}
260+
225261
bool QgsRectangle::isEmpty() const
226262
{
227263
return mXmax <= mXmin || mYmax <= mYmin;

src/core/geometry/qgsrectangle.h

+24
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,30 @@ class CORE_EXPORT QgsRectangle
201201
*/
202202
void combineExtentWith( double x, double y );
203203

204+
/**
205+
* Returns a rectangle offset from this one in the direction of the reversed vector.
206+
* \since QGIS 3.0
207+
*/
208+
QgsRectangle operator-( const QgsVector v ) const;
209+
210+
/**
211+
* Returns a rectangle offset from this one in the direction of the vector.
212+
* \since QGIS 3.0
213+
*/
214+
QgsRectangle operator+( const QgsVector v ) const;
215+
216+
/**
217+
* Moves this rectangle in the direction of the reversed vector.
218+
* \since QGIS 3.0
219+
*/
220+
QgsRectangle &operator-=( const QgsVector v );
221+
222+
/**
223+
* Moves this rectangle in the direction of the vector.
224+
* \since QGIS 3.0
225+
*/
226+
QgsRectangle &operator+=( const QgsVector v );
227+
204228
/**
205229
* Returns true if the rectangle is empty.
206230
* An empty rectangle may still be non-null if it contains valid information (e.g. bounding box of a point).

tests/src/core/testqgsrectangle.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TestQgsRectangle: public QObject
2626
private slots:
2727
void manipulate();
2828
void regression6194();
29+
void operators();
2930
};
3031

3132
void TestQgsRectangle::manipulate()
@@ -85,9 +86,30 @@ void TestQgsRectangle::regression6194()
8586
QVERIFY( rect1 == rect2 );
8687
}
8788

88-
QGSTEST_MAIN( TestQgsRectangle )
89-
#include "testqgsrectangle.moc"
89+
void TestQgsRectangle::operators()
90+
{
91+
QgsRectangle rect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 );
92+
QgsVector v = QgsVector( 1.0, 2.0 );
93+
QgsRectangle rect2 = rect1 + v;
94+
QVERIFY( rect1 != rect2 );
95+
QCOMPARE( rect2.height(), rect1.height() );
96+
QCOMPARE( rect2.width(), rect1.width() );
97+
QCOMPARE( rect2.xMinimum(), 11.0 );
98+
QCOMPARE( rect2.yMinimum(), 22.0 );
9099

100+
rect2 -= rect2.center() - rect1.center();
101+
QVERIFY( rect1 == rect2 );
91102

103+
rect2 += v * 2.5;
104+
QCOMPARE( rect2.xMinimum(), 12.5 );
105+
QCOMPARE( rect2.yMinimum(), 25.0 );
92106

107+
rect2 = rect1 - v;
108+
QCOMPARE( rect2.xMinimum(), 9.0 );
109+
QCOMPARE( rect2.yMinimum(), 18.0 );
110+
QCOMPARE( rect2.height(), rect1.height() );
111+
QCOMPARE( rect2.width(), rect1.width() );
112+
}
93113

114+
QGSTEST_MAIN( TestQgsRectangle )
115+
#include "testqgsrectangle.moc"

0 commit comments

Comments
 (0)