QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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
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
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
165  {
166  return QgsVector( -mY, mX );
167  }
168
172  double angle() const SIP_HOLDGIL
173  {
174  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
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 += ", ";
231  str += qgsDoubleToString( mY, precision );
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:35
QgsVector::operator*
QgsVector operator*(double scalar) const SIP_HOLDGIL
Returns a vector where the components have been multiplied by a scalar value.
Definition: qgsvector.h:60
QgsVector::QgsVector
QgsVector(double x, double y)
Constructor for QgsVector taking x and y component values.
Definition: qgsvector.h:44
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:172
qgis.h
QgsVector::QgsVector
QgsVector()=default
Default constructor for QgsVector.
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:275
QgsVector::perpVector
QgsVector perpVector() const SIP_HOLDGIL
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
Definition: qgsvector.h:164
QgsVector::length
double length() const SIP_HOLDGIL
Returns the length of the vector.
Definition: qgsvector.h:128
precision
int precision
Definition: qgswfsgetfeature.cpp:49
SIP_HOLDGIL
#define SIP_HOLDGIL
Definition: qgis_sip.h:157
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:189
QgsVector::lengthSquared
double lengthSquared() const SIP_HOLDGIL
Returns the length of the vector.
Definition: qgsvector.h:138
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:315
QgsVector::x
double x() const SIP_HOLDGIL
Returns the vector's x-component.
Definition: qgsvector.h:147
QgsVector::operator!=
bool operator!=(QgsVector other) const
Inequality operator.
Definition: qgsvector.h:217
QgsVector::operator+
QgsVector operator+(QgsVector other) const SIP_HOLDGIL
Adds another vector to this vector.
Definition: qgsvector.h:88
QgsVector::y
double y() const SIP_HOLDGIL
Returns the vector's y-component.
Definition: qgsvector.h:156
QgsVector::operator-
QgsVector operator-() const SIP_HOLDGIL
Swaps the sign of the x and y components of the vector.
Definition: qgsvector.h:51
QgsVector::crossProduct
double crossProduct(QgsVector v) const SIP_HOLDGIL
Returns the 2D cross product of this vector and another vector v.
Definition: qgsvector.h:192
QgsVector::operator-=
QgsVector & operator-=(QgsVector other) SIP_HOLDGIL
Subtracts another vector to this vector in place.
Definition: qgsvector.h:117
QgsVector
A class to represent a vector.
Definition: qgsvector.h:30
QgsVector::operator+=
QgsVector & operator+=(QgsVector other) SIP_HOLDGIL
Adds another vector to this vector in place.
Definition: qgsvector.h:97
QgsVector::operator/
QgsVector operator/(double scalar) const SIP_HOLDGIL
Returns a vector where the components have been divided by a scalar value.
Definition: qgsvector.h:69
QgsVector::operator*
double operator*(QgsVector v) const SIP_HOLDGIL
Returns the dot product of two vectors, which is the sum of the x component of this vector multiplied...
Definition: qgsvector.h:79
QgsVector::toString
QString toString(int precision=17) const SIP_HOLDGIL
Returns a string representation of the vector.
Definition: qgsvector.h:226
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
QgsVector::angle
double angle(QgsVector v) const SIP_HOLDGIL
Returns the angle between this vector and another vector in radians.
Definition: qgsvector.h:181
QgsVector::operator-
QgsVector operator-(QgsVector other) const SIP_HOLDGIL
Subtracts another vector to this vector.
Definition: qgsvector.h:108