18 #ifndef QGSRECTANGLE_H 19 #define QGSRECTANGLE_H 21 #include "qgis_core.h" 23 #include <QDomDocument> 48 explicit QgsRectangle(
double xMin,
double yMin = 0,
double xMax = 0,
double yMax = 0 )
66 mXmin = qRectF.topLeft().x();
67 mYmin = qRectF.topLeft().y();
68 mXmax = qRectF.bottomRight().x();
69 mYmax = qRectF.bottomRight().y();
117 void set(
double xMin,
double yMin,
double xMax,
double yMax )
152 mXmin = std::numeric_limits<double>::max();
153 mYmin = std::numeric_limits<double>::max();
154 mXmax = -std::numeric_limits<double>::max();
155 mYmax = -std::numeric_limits<double>::max();
188 std::swap( mXmin, mXmax );
192 std::swap( mYmin, mYmax );
201 double width()
const {
return mXmax - mXmin; }
208 double height()
const {
return mYmax - mYmin; }
217 double area()
const {
return ( mXmax - mXmin ) * ( mYmax - mYmin ); }
224 double perimeter()
const {
return 2 * ( mXmax - mXmin ) + 2 * ( mYmax - mYmin ); }
237 double centerX, centerY;
245 centerX = mXmin + width() / 2;
246 centerY = mYmin + height() / 2;
248 scale( scaleFactor, centerX, centerY );
254 void scale(
double scaleFactor,
double centerX,
double centerY )
256 double newWidth = width() * scaleFactor;
257 double newHeight = height() * scaleFactor;
258 mXmin = centerX - newWidth / 2.0;
259 mXmax = centerX + newWidth / 2.0;
260 mYmin = centerY - newHeight / 2.0;
261 mYmax = centerY + newHeight / 2.0;
287 if ( p.
x() < xMinimum() )
288 setXMinimum( p.
x() );
289 else if ( p.
x() > xMaximum() )
290 setXMaximum( p.
x() );
291 if ( p.
y() < yMinimum() )
292 setYMinimum( p.
y() );
293 if ( p.
y() > yMaximum() )
294 setYMaximum( p.
y() );
305 return QgsRectangle( mXmin - width, mYmin - width, mXmax + width, mYmax + width );
314 if ( intersects( rect ) )
329 double x1 = ( mXmin > rect.mXmin ? mXmin : rect.mXmin );
330 double x2 = ( mXmax < rect.mXmax ? mXmax : rect.mXmax );
333 double y1 = ( mYmin > rect.mYmin ? mYmin : rect.mYmin );
334 double y2 = ( mYmax < rect.mYmax ? mYmax : rect.mYmax );
343 return ( rect.mXmin >= mXmin && rect.mXmax <= mXmax && rect.mYmin >= mYmin && rect.mYmax <= mYmax );
351 return mXmin <= p.
x() && p.
x() <= mXmax &&
352 mYmin <= p.
y() && p.
y() <= mYmax;
362 else if ( !rect.
isNull() )
364 mXmin = std::min( mXmin, rect.
xMinimum() );
365 mXmax = std::max( mXmax, rect.
xMaximum() );
366 mYmin = std::min( mYmin, rect.
yMinimum() );
367 mYmax = std::max( mYmax, rect.
yMaximum() );;
380 mXmin = ( ( mXmin < x ) ? mXmin : x );
381 mXmax = ( ( mXmax > x ) ? mXmax : x );
383 mYmin = ( ( mYmin < y ) ? mYmin : y );
384 mYmax = ( ( mYmax > y ) ? mYmax : y );
394 combineExtentWith( point.
x(), point.
y() );
446 QString asWktCoordinates()
const;
451 QString asWktPolygon()
const;
458 return QRectF( static_cast< qreal >( mXmin ), static_cast< qreal >( mYmin ), static_cast< qreal >( mXmax - mXmin ), static_cast< qreal >( mYmax - mYmin ) );
466 QString toString(
int precision = 16 )
const;
471 QString asPolygon()
const;
491 return ( !
operator==( r1 ) );
517 if ( std::isinf( mXmin ) || std::isinf( mYmin ) || std::isinf( mXmax ) || std::isinf( mYmax ) )
521 if ( std::isnan( mXmin ) || std::isnan( mYmin ) || std::isnan( mXmax ) || std::isnan( mYmax ) )
533 std::swap( mXmin, mYmin );
534 std::swap( mXmax, mYmax );
542 QgsBox3d toBox3d(
double zMin,
double zMax )
const;
545 operator QVariant()
const 547 return QVariant::fromValue( *
this );
559 SIP_PYOBJECT __repr__();
561 QString str = QStringLiteral(
"<QgsRectangle: %1>" ).arg( sipCpp->asWktCoordinates() );
562 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
591 return os << r.
toString().toLocal8Bit().data();
596 #endif // QGSRECTANGLE_H QgsRectangle(const QgsPointXY &p1, const QgsPointXY &p2)
Construct a rectangle from two points. The rectangle is normalized after construction.
bool intersects(const QgsRectangle &rect) const
Returns true when rectangle intersects with other rectangle.
A rectangle specified with double values.
bool isEmpty() const
Returns true if the rectangle is empty.
QRectF toRectF() const
Returns a QRectF with same coordinates as the rectangle.
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
double perimeter() const
Returns the perimeter of the rectangle.
QgsRectangle(const QRectF &qRectF)
Construct a rectangle from a QRectF. The rectangle is normalized after construction.
void setXMaximum(double x)
Set the maximum x value.
void combineExtentWith(const QgsPointXY &point)
Expands the rectangle so that it covers both the original rectangle and the given point...
bool isFinite() const
Returns true if the rectangle has finite boundaries.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
A class to represent a 2D point.
void scale(double scaleFactor, const QgsPointXY *c=nullptr)
Scale the rectangle around its center point.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
void include(const QgsPointXY &p)
Updates the rectangle to include the specified point.
A 3-dimensional box composed of x, y, z coordinates.
Q_DECLARE_METATYPE(QModelIndex)
double area() const
Returns the area of the rectangle.
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsRectangle &rectangle)
Writes the list rectangle to stream out.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QgsRectangle & operator=(const QgsRectangle &r1)
Assignment operator.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
QgsRectangle buffered(double width) const
Gets rectangle enlarged by buffer.
void grow(double delta)
Grows the rectangle in place by the specified amount.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
void setYMinimum(double y)
Set the minimum y value.
QgsPointXY center() const
Returns the center point of the rectangle.
QDateTime operator+(const QDateTime &start, QgsInterval interval)
bool operator!=(const QgsRectangle &r1) const
Comparison operator.
A class to represent a vector.
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
QgsRectangle(double xMin, double yMin=0, double xMax=0, double yMax=0)
Constructor.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle...
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsRectangle &rectangle)
Reads a rectangle from stream in into rectangle.
void setYMaximum(double y)
Set the maximum y value.
void combineExtentWith(double x, double y)
Expands the rectangle so that it covers both the original rectangle and the given point...
void normalize()
Normalize the rectangle so it has non-negative width/height.
QgsRectangle(const QgsRectangle &other)
Copy constructor.
bool contains(const QgsPointXY &p) const
Returns true when rectangle contains a point.
void scale(double scaleFactor, double centerX, double centerY)
Scale the rectangle around its center point.
double width() const
Returns the width of the rectangle.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
void setXMinimum(double x)
Set the minimum x value.
double height() const
Returns the height of the rectangle.
void invert()
Swap x/y coordinates in the rectangle.
bool operator==(const QgsRectangle &r1) const
Comparison operator.