diff --git a/src/core/geometry/qgsrectangle.cpp b/src/core/geometry/qgsrectangle.cpp index 60a7b66ba1d..e83ebb6588c 100644 --- a/src/core/geometry/qgsrectangle.cpp +++ b/src/core/geometry/qgsrectangle.cpp @@ -221,15 +221,13 @@ void QgsRectangle::combineExtentWith( const QgsRectangle &rect ) { if ( isNull() ) *this = rect; - else + else if ( !rect.isNull() ) { - mXmin = ( ( mXmin < rect.xMinimum() ) ? mXmin : rect.xMinimum() ); - mXmax = ( ( mXmax > rect.xMaximum() ) ? mXmax : rect.xMaximum() ); - - mYmin = ( ( mYmin < rect.yMinimum() ) ? mYmin : rect.yMinimum() ); - mYmax = ( ( mYmax > rect.yMaximum() ) ? mYmax : rect.yMaximum() ); + mXmin = std::min( mXmin, rect.xMinimum() ); + mXmax = std::max( mXmax, rect.xMaximum() ); + mYmin = std::min( mYmin, rect.yMinimum() ); + mYmax = std::max( mYmax, rect.yMaximum() );; } - } void QgsRectangle::combineExtentWith( double x, double y ) diff --git a/tests/src/core/testqgsrectangle.cpp b/tests/src/core/testqgsrectangle.cpp index 9fa014e0b4e..aa3939d805c 100644 --- a/tests/src/core/testqgsrectangle.cpp +++ b/tests/src/core/testqgsrectangle.cpp @@ -38,6 +38,7 @@ class TestQgsRectangle: public QObject void include(); void buffered(); void isFinite(); + void combine(); void dataStream(); }; @@ -313,6 +314,25 @@ void TestQgsRectangle::isFinite() QVERIFY( !QgsRectangle( 1, 2, 3, std::numeric_limits::quiet_NaN() ).isFinite() ); } +void TestQgsRectangle::combine() +{ + QgsRectangle rect; + // combine extent of null rectangle with valid rectangle + rect.combineExtentWith( QgsRectangle( 1, 2, 3, 4 ) ); + QCOMPARE( rect.xMinimum(), 1.0 ); + QCOMPARE( rect.yMinimum(), 2.0 ); + QCOMPARE( rect.xMaximum(), 3.0 ); + QCOMPARE( rect.yMaximum(), 4.0 ); + + // combine extent of valid rectangle with null rectangle + rect = QgsRectangle( 1, 2, 3, 4 ); + rect.combineExtentWith( QgsRectangle() ); + QCOMPARE( rect.xMinimum(), 1.0 ); + QCOMPARE( rect.yMinimum(), 2.0 ); + QCOMPARE( rect.xMaximum(), 3.0 ); + QCOMPARE( rect.yMaximum(), 4.0 ); +} + void TestQgsRectangle::dataStream() { QgsRectangle original( 10.1, 20.2, 110.3, 220.4 );