QGIS API Documentation  2.99.0-Master (a411669)
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 
67  void set( const QgsPointXY &p1, const QgsPointXY &p2 );
68 
73  void set( double mXmin, double mYmin, double mXmax, double mYmax );
74 
78  void setXMinimum( double x ) { mXmin = x; }
79 
83  void setXMaximum( double x ) { mXmax = x; }
84 
88  void setYMinimum( double y ) { mYmin = y; }
89 
93  void setYMaximum( double y ) { mYmax = y; }
94 
99  void setMinimal();
100 
104  double xMaximum() const { return mXmax; }
105 
109  double xMinimum() const { return mXmin; }
110 
114  double yMaximum() const { return mYmax; }
115 
119  double yMinimum() const { return mYmin; }
120 
124  void normalize();
125 
131  double width() const { return mXmax - mXmin; }
132 
138  double height() const { return mYmax - mYmin; }
139 
147  double area() const { return ( mXmax - mXmin ) * ( mYmax - mYmin ); }
148 
154  double perimeter() const { return 2 * ( mXmax - mXmin ) + 2 * ( mYmax - mYmin ); }
155 
159  QgsPointXY center() const { return QgsPointXY( mXmin + width() / 2, mYmin + height() / 2 ); }
160 
164  void scale( double scaleFactor, const QgsPointXY *c = nullptr );
165 
169  void scale( double scaleFactor, double centerX, double centerY );
170 
175  void grow( double delta );
176 
180  void include( const QgsPointXY &p );
181 
188  QgsRectangle buffered( double width ) const;
189 
193  QgsRectangle intersect( const QgsRectangle *rect ) const;
194 
198  bool intersects( const QgsRectangle &rect ) const;
199 
203  bool contains( const QgsRectangle &rect ) const;
204 
208  bool contains( const QgsPointXY &p ) const;
209 
213  void combineExtentWith( const QgsRectangle &rect );
214 
218  void combineExtentWith( double x, double y );
219 
224  QgsRectangle operator-( const QgsVector v ) const;
225 
230  QgsRectangle operator+( const QgsVector v ) const;
231 
236  QgsRectangle &operator-=( const QgsVector v );
237 
242  QgsRectangle &operator+=( const QgsVector v );
243 
248  bool isEmpty() const;
249 
255  bool isNull() const;
256 
260  QString asWktCoordinates() const;
261 
265  QString asWktPolygon() const;
266 
270  QRectF toRectF() const;
271 
277  QString toString( int precision = 16 ) const;
278 
282  QString asPolygon() const;
283 
288  bool operator==( const QgsRectangle &r1 ) const;
289 
294  bool operator!=( const QgsRectangle &r1 ) const;
295 
300  QgsRectangle &operator=( const QgsRectangle &r1 );
301 
306  bool isFinite() const;
307 
311  void invert();
312 
318  QgsBox3d toBox3d( double zMin, double zMax ) const;
319 
321  operator QVariant() const
322  {
323  return QVariant::fromValue( *this );
324  }
325 
326  private:
327 
328  double mXmin;
329  double mYmin;
330  double mXmax;
331  double mYmax;
332 
333 };
334 
336 
337 #ifndef SIP_RUN
338 
342 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsRectangle &rectangle );
343 
347 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsRectangle &rectangle );
348 
349 inline std::ostream &operator << ( std::ostream &os, const QgsRectangle &r )
350 {
351  return os << r.toString().toLocal8Bit().data();
352 }
353 
354 #endif
355 
356 #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:83
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:154
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:131
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:88
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:147
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:119
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:104
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:93
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:109
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:114
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:159
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:78
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:138