Skip to content

Commit

Permalink
Add method to create QgsRectangle from center and size
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 7, 2017
1 parent 7924aa1 commit 53c8779
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
10 changes: 9 additions & 1 deletion python/core/geometry/qgsrectangle.sip
Expand Up @@ -46,12 +46,20 @@ Copy constructor

static QgsRectangle fromWkt( const QString &wkt );
%Docstring
Creates a new rectangle from a WKT string.
Creates a new rectangle from a ``wkt`` string.
The WKT must contain only 5 vertices, representing a rectangle aligned with X and Y axes.
.. versionadded:: 3.0
:rtype: QgsRectangle
%End

static QgsRectangle fromCenterAndSize( QgsPointXY center, double width, double height );
%Docstring
Creates a new rectangle, given the specified ``center`` point
and ``width`` and ``height``.
.. versionadded:: 3.0
:rtype: QgsRectangle
%End

void set( const QgsPointXY &p1, const QgsPointXY &p2 );
%Docstring
Sets the rectangle from two QgsPoints. The rectangle is
Expand Down
9 changes: 9 additions & 0 deletions src/core/geometry/qgsrectangle.cpp
Expand Up @@ -80,6 +80,15 @@ QgsRectangle QgsRectangle::fromWkt( const QString &wkt )
return QgsRectangle();
}

QgsRectangle QgsRectangle::fromCenterAndSize( QgsPointXY center, double width, double height )
{
double xMin = center.x() - width / 2.0;
double xMax = xMin + width;
double yMin = center.y() - height / 2.0;
double yMax = yMin + height;
return QgsRectangle( xMin, yMin, xMax, yMax );
}

void QgsRectangle::set( const QgsPointXY &p1, const QgsPointXY &p2 )
{
mXmin = p1.x();
Expand Down
9 changes: 8 additions & 1 deletion src/core/geometry/qgsrectangle.h
Expand Up @@ -54,12 +54,19 @@ class CORE_EXPORT QgsRectangle
~QgsRectangle() = default;

/**
* Creates a new rectangle from a WKT string.
* Creates a new rectangle from a \a wkt string.
* The WKT must contain only 5 vertices, representing a rectangle aligned with X and Y axes.
* \since QGIS 3.0
*/
static QgsRectangle fromWkt( const QString &wkt );

/**
* Creates a new rectangle, given the specified \a center point
* and \a width and \a height.
* \since QGIS 3.0
*/
static QgsRectangle fromCenterAndSize( QgsPointXY center, double width, double height );

/**
* Sets the rectangle from two QgsPoints. The rectangle is
* normalised after construction.
Expand Down
23 changes: 23 additions & 0 deletions tests/src/core/testqgsrectangle.cpp
Expand Up @@ -27,6 +27,7 @@ class TestQgsRectangle: public QObject
private slots:
void isEmpty();
void fromWkt();
void fromCenter();
void manipulate();
void regression6194();
void operators();
Expand Down Expand Up @@ -76,6 +77,28 @@ void TestQgsRectangle::fromWkt()
QVERIFY( QgsRectangle::fromWkt( QStringLiteral( "POLYGON((0 0,1 0,1 1,0 1))" ) ).isEmpty() );
}

void TestQgsRectangle::fromCenter()
{
QgsRectangle rect = QgsRectangle::fromCenterAndSize( QgsPointXY( 12, 21 ), 20, 40 );
QVERIFY( ! rect.isEmpty() );
QCOMPARE( rect.xMinimum(), 2.0 );
QCOMPARE( rect.yMinimum(), 1.0 );
QCOMPARE( rect.xMaximum(), 22.0 );
QCOMPARE( rect.yMaximum(), 41.0 );

rect = QgsRectangle::fromCenterAndSize( QgsPointXY( 12, 21 ), 0, 40 );
QCOMPARE( rect.xMinimum(), 12.0 );
QCOMPARE( rect.yMinimum(), 1.0 );
QCOMPARE( rect.xMaximum(), 12.0 );
QCOMPARE( rect.yMaximum(), 41.0 );

rect = QgsRectangle::fromCenterAndSize( QgsPointXY( 12, 21 ), 20, 0 );
QCOMPARE( rect.xMinimum(), 2.0 );
QCOMPARE( rect.yMinimum(), 21.0 );
QCOMPARE( rect.xMaximum(), 22.0 );
QCOMPARE( rect.yMaximum(), 21.0 );
}

void TestQgsRectangle::manipulate()
{
// Set up two intersecting rectangles and normalize
Expand Down

0 comments on commit 53c8779

Please sign in to comment.