QGIS API Documentation  2.99.0-Master (19b062c)
qgsrectangle.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrectangle.h - description
3  -------------------
4  begin : Sat Jun 22 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
7 ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSRECTANGLE_H
19 #define QGSRECTANGLE_H
20 
21 #include "qgis_core.h"
22 #include <iosfwd>
23 #include <QDomDocument>
24 
25 class QString;
26 class QRectF;
27 class QgsBox3d;
28 #include "qgspointxy.h"
29 
30 
39 class CORE_EXPORT QgsRectangle
40 {
41  public:
43  QgsRectangle( double xMin = 0, double yMin = 0, double xMax = 0, double yMax = 0 );
45  QgsRectangle( const QgsPointXY &p1, const QgsPointXY &p2 );
47  QgsRectangle( const QRectF &qRectF );
49  QgsRectangle( const QgsRectangle &other );
50 
51  // IMPORTANT - while QgsRectangle is inherited by QgsReferencedRectangle, we do NOT want a virtual destructor here
52  // because this class MUST be lightweight and we don't want the cost of the vtable here.
53  // see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
54  ~QgsRectangle() = default;
55 
61  static QgsRectangle fromWkt( const QString &wkt );
62 
68  static QgsRectangle fromCenterAndSize( QgsPointXY center, double width, double height );
69 
74  void set( const QgsPointXY &p1, const QgsPointXY &p2 );
75 
80  void set( double mXmin, double mYmin, double mXmax, double mYmax );
81 
85  void setXMinimum( double x ) { mXmin = x; }
86 
90  void setXMaximum( double x ) { mXmax = x; }
91 
95  void setYMinimum( double y ) { mYmin = y; }
96 
100  void setYMaximum( double y ) { mYmax = y; }
101 
106  void setMinimal();
107 
111  double xMaximum() const { return mXmax; }
112 
116  double xMinimum() const { return mXmin; }
117 
121  double yMaximum() const { return mYmax; }
122 
126  double yMinimum() const { return mYmin; }
127 
131  void normalize();
132 
138  double width() const { return mXmax - mXmin; }
139 
145  double height() const { return mYmax - mYmin; }
146 
154  double area() const { return ( mXmax - mXmin ) * ( mYmax - mYmin ); }
155 
161  double perimeter() const { return 2 * ( mXmax - mXmin ) + 2 * ( mYmax - mYmin ); }
162 
166  QgsPointXY center() const { return QgsPointXY( mXmin + width() / 2, mYmin + height() / 2 ); }
167 
171  void scale( double scaleFactor, const QgsPointXY *c = nullptr );
172 
176  void scale( double scaleFactor, double centerX, double centerY );
177 
182  void grow( double delta );
183 
187  void include( const QgsPointXY &p );
188 
195  QgsRectangle buffered( double width ) const;
196 
200  QgsRectangle intersect( const QgsRectangle *rect ) const;
201 
205  bool intersects( const QgsRectangle &rect ) const;
206 
210  bool contains( const QgsRectangle &rect ) const;
211 
215  bool contains( const QgsPointXY &p ) const;
216 
220  void combineExtentWith( const QgsRectangle &rect );
221 
225  void combineExtentWith( double x, double y );
226 
231  QgsRectangle operator-( const QgsVector v ) const;
232 
237  QgsRectangle operator+( const QgsVector v ) const;
238 
243  QgsRectangle &operator-=( const QgsVector v );
244 
249  QgsRectangle &operator+=( const QgsVector v );
250 
255  bool isEmpty() const;
256 
262  bool isNull() const;
263 
267  QString asWktCoordinates() const;
268 
272  QString asWktPolygon() const;
273 
277  QRectF toRectF() const;
278 
284  QString toString( int precision = 16 ) const;
285 
289  QString asPolygon() const;
290 
295  bool operator==( const QgsRectangle &r1 ) const;
296 
301  bool operator!=( const QgsRectangle &r1 ) const;
302 
307  QgsRectangle &operator=( const QgsRectangle &r1 );
308 
313  bool isFinite() const;
314 
318  void invert();
319 
325  QgsBox3d toBox3d( double zMin, double zMax ) const;
326 
328  operator QVariant() const
329  {
330  return QVariant::fromValue( *this );
331  }
332 
333  private:
334 
335  double mXmin;
336  double mYmin;
337  double mXmax;
338  double mYmax;
339 
340 };
341 
343 
344 #ifndef SIP_RUN
345 
349 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle );
350 
354 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsRectangle &rectangle );
355 
356 inline std::ostream &operator << ( std::ostream &os, const QgsRectangle &r )
357 {
358  return os << r.toString().toLocal8Bit().data();
359 }
360 
361 #endif
362 
363 #endif // QGSRECTANGLE_H
A rectangle specified with double values.
Definition: qgsrectangle.h:39
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:90
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
A class to represent a 2D point.
Definition: qgspointxy.h:43
double perimeter() const
Returns the perimeter of the rectangle.
Definition: qgsrectangle.h:161
A 3-dimensional box composed of x, y, z coordinates.
Definition: qgsbox3d.h:35
Q_DECLARE_METATYPE(QModelIndex)
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsRectangle &rectangle)
Writes the list rectangle to stream out.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:138
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:95
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
QDateTime operator+(const QDateTime &start, QgsInterval interval)
A class to represent a vector.
Definition: qgsvector.h:27
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
double area() const
Returns the area of the rectangle.
Definition: qgsrectangle.h:154
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:126
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:111
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.
Definition: qgsrectangle.h:100
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:116
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:121
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:166
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:85
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:145