Skip to content
Permalink
Browse files

Add method to create QgsRectangle from center and size

  • Loading branch information
nyalldawson committed Oct 21, 2017
1 parent 7924aa1 commit 53c877963ac5374c720c6182e0ebc494ea43ecde
@@ -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
@@ -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();
@@ -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.
@@ -27,6 +27,7 @@ class TestQgsRectangle: public QObject
private slots:
void isEmpty();
void fromWkt();
void fromCenter();
void manipulate();
void regression6194();
void operators();
@@ -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

0 comments on commit 53c8779

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