Skip to content
Permalink
Browse files

Rectangle moving operators

  • Loading branch information
Zverik authored and 3nids committed Jun 26, 2017
1 parent 6006359 commit 7941759d23422b1db637ca9fe69cd490e562cb2d
Showing with 84 additions and 2 deletions.
  1. +36 −0 src/core/geometry/qgsrectangle.cpp
  2. +24 −0 src/core/geometry/qgsrectangle.h
  3. +24 −2 tests/src/core/testqgsrectangle.cpp
@@ -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;
@@ -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).
@@ -26,6 +26,7 @@ class TestQgsRectangle: public QObject
private slots:
void manipulate();
void regression6194();
void operators();
};

void TestQgsRectangle::manipulate()
@@ -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.
You can’t perform that action at this time.