QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsvector.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvector.h - QgsVector
3 
4  ---------------------
5  begin : 24.2.2017
6  copyright : (C) 2017 by Matthias Kuhn
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSVECTOR_H
17 #define QGSVECTOR_H
18 
19 #include "qgis.h"
20 #include "qgis_core.h"
21 #include <QtGlobal>
22 
29 class CORE_EXPORT QgsVector
30 {
31 
32  public:
33 
37  QgsVector() = default;
38 
44  QgsVector( double x, double y )
45  : mX( x )
46  , mY( y )
47  {
48  }
49 
52  {
53  return QgsVector( -mX, -mY );
54  }
55 
60  QgsVector operator*( double scalar ) const SIP_HOLDGIL
61  {
62  return QgsVector( mX * scalar, mY * scalar );
63  }
64 
69  QgsVector operator/( double scalar ) const SIP_HOLDGIL
70  {
71  return *this * ( 1.0 / scalar );
72  }
73 
79  double operator*( QgsVector v ) const SIP_HOLDGIL
80  {
81  return mX * v.mX + mY * v.mY;
82  }
83 
89  {
90  return QgsVector( mX + other.mX, mY + other.mY );
91  }
92 
97  QgsVector &operator+=( QgsVector other ) SIP_HOLDGIL
98  {
99  mX += other.mX;
100  mY += other.mY;
101  return *this;
102  }
103 
109  {
110  return QgsVector( mX - other.mX, mY - other.mY );
111  }
112 
117  QgsVector &operator-=( QgsVector other ) SIP_HOLDGIL
118  {
119  mX -= other.mX;
120  mY -= other.mY;
121  return *this;
122  }
123 
128  double length() const SIP_HOLDGIL
129  {
130  return std::sqrt( mX * mX + mY * mY );
131  }
132 
138  double lengthSquared() const SIP_HOLDGIL
139  {
140  return mX * mX + mY * mY;
141  }
142 
147  double x() const SIP_HOLDGIL
148  {
149  return mX;
150  }
151 
156  double y() const SIP_HOLDGIL
157  {
158  return mY;
159  }
160 
164  QgsVector perpVector() const SIP_HOLDGIL
165  {
166  return QgsVector( -mY, mX );
167  }
168 
172  double angle() const SIP_HOLDGIL
173  {
174  const double angle = std::atan2( mY, mX );
175  return angle < 0.0 ? angle + 2.0 * M_PI : angle;
176  }
177 
181  double angle( QgsVector v ) const SIP_HOLDGIL
182  {
183  return v.angle() - angle();
184  }
185 
192  double crossProduct( QgsVector v ) const SIP_HOLDGIL
193  {
194  return mX * v.y() - mY * v.x();
195  }
196 
201  QgsVector rotateBy( double rot ) const SIP_HOLDGIL;
202 
208  QgsVector normalized() const SIP_THROW( QgsException );
209 
211  bool operator==( QgsVector other ) const SIP_HOLDGIL
212  {
213  return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
214  }
215 
217  bool operator!=( QgsVector other ) const
218  {
219  return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
220  }
221 
226  QString toString( int precision = 17 ) const SIP_HOLDGIL
227  {
228  QString str = "Vector (";
229  str += qgsDoubleToString( mX, precision );
230  str += ", ";
232  str += ')';
233  return str;
234  }
235 
236 #ifdef SIP_RUN
237  SIP_PYOBJECT __repr__();
238  % MethodCode
239  QString str = QStringLiteral( "<QgsVector: %1>" ).arg( sipCpp->toString() );
240  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
241  % End
242 #endif
243 
244  private:
245  double mX = 0.0;
246  double mY = 0.0;
247 
248 };
249 
250 Q_DECLARE_TYPEINFO( QgsVector, Q_MOVABLE_TYPE );
251 
252 #endif // QGSVECTOR_H
QgsException
Defines a QGIS exception class.
Definition: qgsexception.h:34
Q_DECLARE_TYPEINFO
Q_DECLARE_TYPEINFO(QgsVector, Q_MOVABLE_TYPE)
QgsVector::angle
double angle() const SIP_HOLDGIL
Returns the angle of the vector in radians.
Definition: qgsvector.h:186
qgis.h
operator/
QgsMargins operator/(const QgsMargins &margins, double divisor)
Returns a QgsMargins object that is formed by dividing the components of the given margins by the giv...
Definition: qgsmargins.h:262
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:430
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:2204
precision
int precision
Definition: qgswfsgetfeature.cpp:103
SIP_HOLDGIL
#define SIP_HOLDGIL
Definition: qgis_sip.h:166
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsVector::x
double x() const SIP_HOLDGIL
Returns the vector's x-component.
Definition: qgsvector.h:161
QgsVector::y
double y() const SIP_HOLDGIL
Returns the vector's y-component.
Definition: qgsvector.h:170
operator*
QgsMargins operator*(const QgsMargins &margins, double factor)
Returns a QgsMargins object that is formed by multiplying each component of the given margins by fact...
Definition: qgsmargins.h:242
str
#define str(x)
Definition: qgis.cpp:37
operator-
QgsInterval operator-(const QDateTime &dt1, const QDateTime &dt2)
Returns the interval between two datetimes.
Definition: qgsinterval.cpp:299
QgsVector
A class to represent a vector. Currently no Z axis / 2.5D support is implemented.
Definition: qgsvector.h:29
operator+
QDateTime operator+(const QDateTime &start, const QgsInterval &interval)
Adds an interval to a datetime.
Definition: qgsinterval.cpp:305
MathUtils::angle
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786