Skip to content
Permalink
Browse files

Add method to determine distance from point to rectangle bounds

  • Loading branch information
nyalldawson committed Mar 2, 2020
1 parent d8111e1 commit 997ac4eba427f92574c12b0bbfbb7425990ff829
@@ -252,6 +252,13 @@ Expands the rectangle so that it covers both the original rectangle and the give
Expands the rectangle so that it covers both the original rectangle and the given point.

.. versionadded:: 3.2
%End

double distance( const QgsPointXY &point ) const;
%Docstring
Returns the distance from ``point`` to the nearest point on the boundary of the rectangle.

.. versionadded:: 3.14
%End

QgsRectangle operator-( QgsVector v ) const;
@@ -395,6 +395,17 @@ class CORE_EXPORT QgsRectangle
combineExtentWith( point.x(), point.y() );
}

/**
* Returns the distance from \a point to the nearest point on the boundary of the rectangle.
* \since QGIS 3.14
*/
double distance( const QgsPointXY &point ) const
{
const double dx = std::max( std::max( mXmin - point.x(), 0.0 ), point.x() - mXmax );
const double dy = std::max( std::max( mYmin - point.y(), 0.0 ), point.y() - mYmax );
return std::sqrt( dx * dx + dy * dy );
}

/**
* Returns a rectangle offset from this one in the direction of the reversed vector.
* \since QGIS 3.0
@@ -42,6 +42,7 @@ class TestQgsRectangle: public QObject
void dataStream();
void scale();
void snappedToGrid();
void distanceToPoint();
};

void TestQgsRectangle::isEmpty()
@@ -382,5 +383,26 @@ void TestQgsRectangle::snappedToGrid()
QCOMPARE( QgsRectangle().snappedToGrid( 0.1 ), QgsRectangle() );
}

void TestQgsRectangle::distanceToPoint()
{
QgsRectangle rect( 10, 100, 20, 110 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 10, 100 ) ), 0.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 20, 100 ) ), 0.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 15, 100 ) ), 0.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 10, 110 ) ), 0.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 20, 110 ) ), 0.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 15, 110 ) ), 0.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 10, 105 ) ), 0.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 20, 100 ) ), 0.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 0, 100 ) ), 10.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 35, 100 ) ), 15.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 15, 95 ) ), 5.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 15, 120 ) ), 10.0, 0.000000001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 5, 95 ) ), 7.071068, 0.00001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 25, 95 ) ), 7.071068, 0.00001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 5, 115 ) ), 7.071068, 0.00001 );
QGSCOMPARENEAR( rect.distance( QgsPointXY( 25, 115 ) ), 7.071068, 0.00001 );
}

QGSTEST_MAIN( TestQgsRectangle )
#include "testqgsrectangle.moc"

0 comments on commit 997ac4e

Please sign in to comment.
You can’t perform that action at this time.