Navigation Menu

Skip to content

Commit

Permalink
Rectangle moving operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Zverik authored and 3nids committed Jun 26, 2017
1 parent 6006359 commit 7941759
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/core/geometry/qgsrectangle.cpp
Expand Up @@ -222,6 +222,42 @@ void QgsRectangle::combineExtentWith( double x, double y )
}
}

QgsRectangle QgsRectangle::operator-( const QgsVector v ) const
{
double xmin = mXmin - v.x();
double xmax = mXmax - v.x();
double ymin = mYmin - v.y();
double ymax = mYmax - v.y();
return QgsRectangle( xmin, ymin, xmax, ymax );
}

QgsRectangle QgsRectangle::operator+( const QgsVector v ) const
{
double xmin = mXmin + v.x();
double xmax = mXmax + v.x();
double ymin = mYmin + v.y();
double ymax = mYmax + v.y();
return QgsRectangle( xmin, ymin, xmax, ymax );
}

QgsRectangle &QgsRectangle::operator-=( const QgsVector v )
{
mXmin -= v.x();
mXmax -= v.x();
mYmin -= v.y();
mYmax -= v.y();
return *this;
}

QgsRectangle &QgsRectangle::operator+=( const QgsVector v )
{
mXmin += v.x();
mXmax += v.x();
mYmin += v.y();
mYmax += v.y();
return *this;
}

bool QgsRectangle::isEmpty() const
{
return mXmax <= mXmin || mYmax <= mYmin;
Expand Down
24 changes: 24 additions & 0 deletions src/core/geometry/qgsrectangle.h
Expand Up @@ -201,6 +201,30 @@ class CORE_EXPORT QgsRectangle
*/
void combineExtentWith( double x, double y );

/**
* Returns a rectangle offset from this one in the direction of the reversed vector.
* \since QGIS 3.0
*/
QgsRectangle operator-( const QgsVector v ) const;

/**
* Returns a rectangle offset from this one in the direction of the vector.
* \since QGIS 3.0
*/
QgsRectangle operator+( const QgsVector v ) const;

/**
* Moves this rectangle in the direction of the reversed vector.
* \since QGIS 3.0
*/
QgsRectangle &operator-=( const QgsVector v );

/**
* Moves this rectangle in the direction of the vector.
* \since QGIS 3.0
*/
QgsRectangle &operator+=( const QgsVector v );

/**
* Returns true if the rectangle is empty.
* An empty rectangle may still be non-null if it contains valid information (e.g. bounding box of a point).
Expand Down
26 changes: 24 additions & 2 deletions tests/src/core/testqgsrectangle.cpp
Expand Up @@ -26,6 +26,7 @@ class TestQgsRectangle: public QObject
private slots:
void manipulate();
void regression6194();
void operators();
};

void TestQgsRectangle::manipulate()
Expand Down Expand Up @@ -85,9 +86,30 @@ void TestQgsRectangle::regression6194()
QVERIFY( rect1 == rect2 );
}

QGSTEST_MAIN( TestQgsRectangle )
#include "testqgsrectangle.moc"
void TestQgsRectangle::operators()
{
QgsRectangle rect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 );
QgsVector v = QgsVector( 1.0, 2.0 );
QgsRectangle rect2 = rect1 + v;
QVERIFY( rect1 != rect2 );
QCOMPARE( rect2.height(), rect1.height() );
QCOMPARE( rect2.width(), rect1.width() );
QCOMPARE( rect2.xMinimum(), 11.0 );
QCOMPARE( rect2.yMinimum(), 22.0 );

rect2 -= rect2.center() - rect1.center();
QVERIFY( rect1 == rect2 );

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

rect2 = rect1 - v;
QCOMPARE( rect2.xMinimum(), 9.0 );
QCOMPARE( rect2.yMinimum(), 18.0 );
QCOMPARE( rect2.height(), rect1.height() );
QCOMPARE( rect2.width(), rect1.width() );
}

QGSTEST_MAIN( TestQgsRectangle )
#include "testqgsrectangle.moc"

0 comments on commit 7941759

Please sign in to comment.