Skip to content

Commit

Permalink
Fix combining extent of valid rectangle with invalid rectangle results
Browse files Browse the repository at this point in the history
in invalid rectangle
  • Loading branch information
nyalldawson committed Nov 19, 2017
1 parent 8f1021c commit 8cf9f8f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/core/geometry/qgsrectangle.cpp
Expand Up @@ -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 )
Expand Down
20 changes: 20 additions & 0 deletions tests/src/core/testqgsrectangle.cpp
Expand Up @@ -38,6 +38,7 @@ class TestQgsRectangle: public QObject
void include();
void buffered();
void isFinite();
void combine();
void dataStream();
};

Expand Down Expand Up @@ -313,6 +314,25 @@ void TestQgsRectangle::isFinite()
QVERIFY( !QgsRectangle( 1, 2, 3, std::numeric_limits<double>::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 );
Expand Down

0 comments on commit 8cf9f8f

Please sign in to comment.