QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
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
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
29class 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
80 {
81 return mX * v.mX + mY * v.mY;
82 }
83
88 {
89 return QgsVector( mX + other.mX, mY + other.mY );
90 }
91
96 {
97 mX += other.mX;
98 mY += other.mY;
99 return *this;
100 }
101
106 {
107 return QgsVector( mX - other.mX, mY - other.mY );
108 }
109
114 {
115 mX -= other.mX;
116 mY -= other.mY;
117 return *this;
118 }
119
124 double length() const SIP_HOLDGIL
125 {
126 return std::sqrt( mX * mX + mY * mY );
127 }
128
135 {
136 return mX * mX + mY * mY;
137 }
138
143 double x() const SIP_HOLDGIL
144 {
145 return mX;
146 }
147
152 double y() const SIP_HOLDGIL
153 {
154 return mY;
155 }
156
161 {
162 return QgsVector( -mY, mX );
163 }
164
168 double angle() const SIP_HOLDGIL
169 {
170 const double angle = std::atan2( mY, mX );
171 return angle < 0.0 ? angle + 2.0 * M_PI : angle;
172 }
173
177 double angle( QgsVector v ) const SIP_HOLDGIL
178 {
179 return v.angle() - angle();
180 }
181
189 {
190 return mX * v.y() - mY * v.x();
191 }
192
197 QgsVector rotateBy( double rot ) const SIP_HOLDGIL;
198
204 QgsVector normalized() const SIP_THROW( QgsException );
205
207 bool operator==( QgsVector other ) const SIP_HOLDGIL
208 {
209 return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
210 }
211
213 bool operator!=( QgsVector other ) const
214 {
215 return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
216 }
217
222 QString toString( int precision = 17 ) const SIP_HOLDGIL
223 {
224 QString str = "Vector (";
226 str += ", ";
228 str += ')';
229 return str;
230 }
231
232#ifdef SIP_RUN
233 SIP_PYOBJECT __repr__();
234 % MethodCode
235 QString str = QStringLiteral( "<QgsVector: %1>" ).arg( sipCpp->toString() );
236 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
237 % End
238#endif
239
240 private:
241 double mX = 0.0;
242 double mY = 0.0;
243
244};
245
246Q_DECLARE_TYPEINFO( QgsVector, Q_MOVABLE_TYPE );
247
248#endif // QGSVECTOR_H
Defines a QGIS exception class.
A class to represent a vector.
Definition qgsvector.h:30
QgsVector & operator+=(QgsVector other)
Adds another vector to this vector in place.
Definition qgsvector.h:95
QgsVector operator-() const
Swaps the sign of the x and y components of the vector.
Definition qgsvector.h:51
double y() const
Returns the vector's y-component.
Definition qgsvector.h:152
QgsVector(double x, double y)
Constructor for QgsVector taking x and y component values.
Definition qgsvector.h:44
QgsVector operator-(QgsVector other) const
Subtracts another vector to this vector.
Definition qgsvector.h:105
double lengthSquared() const
Returns the length of the vector.
Definition qgsvector.h:134
QString toString(int precision=17) const
Returns a string representation of the vector.
Definition qgsvector.h:222
double crossProduct(QgsVector v) const
Returns the 2D cross product of this vector and another vector v.
Definition qgsvector.h:188
QgsVector operator/(double scalar) const
Returns a vector where the components have been divided by a scalar value.
Definition qgsvector.h:69
double angle() const
Returns the angle of the vector in radians.
Definition qgsvector.h:168
double operator*(QgsVector v) const
Returns the dot product of two vectors, which is the sum of the x component of this vector multiplied...
Definition qgsvector.h:79
double angle(QgsVector v) const
Returns the angle between this vector and another vector in radians.
Definition qgsvector.h:177
QgsVector operator+(QgsVector other) const
Adds another vector to this vector.
Definition qgsvector.h:87
QgsVector & operator-=(QgsVector other)
Subtracts another vector to this vector in place.
Definition qgsvector.h:113
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
Definition qgsvector.h:160
double x() const
Returns the vector's x-component.
Definition qgsvector.h:143
QgsVector operator*(double scalar) const
Returns a vector where the components have been multiplied by a scalar value.
Definition qgsvector.h:60
QgsVector()=default
Default constructor for QgsVector.
double length() const
Returns the length of the vector.
Definition qgsvector.h:124
bool operator!=(QgsVector other) const
Inequality operator.
Definition qgsvector.h:213
#define str(x)
Definition qgis.cpp:38
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5362
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5445
#define SIP_HOLDGIL
Definition qgis_sip.h:171
#define SIP_THROW(name,...)
Definition qgis_sip.h:203
Q_DECLARE_TYPEINFO(QgsVector, Q_MOVABLE_TYPE)
int precision