Skip to content

Commit 8cf9f8f

Browse files
committedNov 19, 2017
Fix combining extent of valid rectangle with invalid rectangle results
in invalid rectangle
1 parent 8f1021c commit 8cf9f8f

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed
 

‎src/core/geometry/qgsrectangle.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,13 @@ void QgsRectangle::combineExtentWith( const QgsRectangle &rect )
221221
{
222222
if ( isNull() )
223223
*this = rect;
224-
else
224+
else if ( !rect.isNull() )
225225
{
226-
mXmin = ( ( mXmin < rect.xMinimum() ) ? mXmin : rect.xMinimum() );
227-
mXmax = ( ( mXmax > rect.xMaximum() ) ? mXmax : rect.xMaximum() );
228-
229-
mYmin = ( ( mYmin < rect.yMinimum() ) ? mYmin : rect.yMinimum() );
230-
mYmax = ( ( mYmax > rect.yMaximum() ) ? mYmax : rect.yMaximum() );
226+
mXmin = std::min( mXmin, rect.xMinimum() );
227+
mXmax = std::max( mXmax, rect.xMaximum() );
228+
mYmin = std::min( mYmin, rect.yMinimum() );
229+
mYmax = std::max( mYmax, rect.yMaximum() );;
231230
}
232-
233231
}
234232

235233
void QgsRectangle::combineExtentWith( double x, double y )

‎tests/src/core/testqgsrectangle.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class TestQgsRectangle: public QObject
3838
void include();
3939
void buffered();
4040
void isFinite();
41+
void combine();
4142
void dataStream();
4243
};
4344

@@ -313,6 +314,25 @@ void TestQgsRectangle::isFinite()
313314
QVERIFY( !QgsRectangle( 1, 2, 3, std::numeric_limits<double>::quiet_NaN() ).isFinite() );
314315
}
315316

317+
void TestQgsRectangle::combine()
318+
{
319+
QgsRectangle rect;
320+
// combine extent of null rectangle with valid rectangle
321+
rect.combineExtentWith( QgsRectangle( 1, 2, 3, 4 ) );
322+
QCOMPARE( rect.xMinimum(), 1.0 );
323+
QCOMPARE( rect.yMinimum(), 2.0 );
324+
QCOMPARE( rect.xMaximum(), 3.0 );
325+
QCOMPARE( rect.yMaximum(), 4.0 );
326+
327+
// combine extent of valid rectangle with null rectangle
328+
rect = QgsRectangle( 1, 2, 3, 4 );
329+
rect.combineExtentWith( QgsRectangle() );
330+
QCOMPARE( rect.xMinimum(), 1.0 );
331+
QCOMPARE( rect.yMinimum(), 2.0 );
332+
QCOMPARE( rect.xMaximum(), 3.0 );
333+
QCOMPARE( rect.yMaximum(), 4.0 );
334+
}
335+
316336
void TestQgsRectangle::dataStream()
317337
{
318338
QgsRectangle original( 10.1, 20.2, 110.3, 220.4 );

0 commit comments

Comments
 (0)
Please sign in to comment.