QGIS API Documentation 3.99.0-Master (d270888f95f)
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
22#include <QString>
23#include <QtGlobal>
24
25using namespace Qt::StringLiterals;
26
33class CORE_EXPORT QgsVector
34{
35
36 public:
37
41 QgsVector() = default;
42
48 QgsVector( double x, double y )
49 : mX( x )
50 , mY( y )
51 {
52 }
53
56 {
57 return QgsVector( -mX, -mY );
58 }
59
64 QgsVector operator*( double scalar ) const SIP_HOLDGIL
65 {
66 return QgsVector( mX * scalar, mY * scalar );
67 }
68
73 QgsVector operator/( double scalar ) const SIP_HOLDGIL
74 {
75 return *this * ( 1.0 / scalar );
76 }
77
84 {
85 return mX * v.mX + mY * v.mY;
86 }
87
92 {
93 return QgsVector( mX + other.mX, mY + other.mY );
94 }
95
100 {
101 mX += other.mX;
102 mY += other.mY;
103 return *this;
104 }
105
110 {
111 return QgsVector( mX - other.mX, mY - other.mY );
112 }
113
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
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 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
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
210 bool operator==( QgsVector other ) const SIP_HOLDGIL
211 {
212 return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
213 }
214
215 bool operator!=( QgsVector other ) const
216 {
217 return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
218 }
219
224 QString toString( int precision = 17 ) const SIP_HOLDGIL
225 {
226 QString str = "Vector (";
227 str += qgsDoubleToString( mX, precision );
228 str += ", ";
229 str += qgsDoubleToString( mY, precision );
230 str += ')';
231 return str;
232 }
233
234#ifdef SIP_RUN
235 SIP_PYOBJECT __repr__();
236 % MethodCode
237 QString str = u"<QgsVector: %1>"_s.arg( sipCpp->toString() );
238 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
239 % End
240#endif
241
242 private:
243 double mX = 0.0;
244 double mY = 0.0;
245
246};
247
248Q_DECLARE_TYPEINFO( QgsVector, Q_MOVABLE_TYPE );
249
250#endif // QGSVECTOR_H
Defines a QGIS exception class.
Represent a 2-dimensional vector.
Definition qgsvector.h:34
QgsVector & operator+=(QgsVector other)
Adds another vector to this vector in place.
Definition qgsvector.h:99
QgsVector operator-() const
Swaps the sign of the x and y components of the vector.
Definition qgsvector.h:55
double y() const
Returns the vector's y-component.
Definition qgsvector.h:156
QgsVector(double x, double y)
Constructor for QgsVector taking x and y component values.
Definition qgsvector.h:48
QgsVector operator-(QgsVector other) const
Subtracts another vector to this vector.
Definition qgsvector.h:109
double lengthSquared() const
Returns the length of the vector.
Definition qgsvector.h:138
QString toString(int precision=17) const
Returns a string representation of the vector.
Definition qgsvector.h:224
double crossProduct(QgsVector v) const
Returns the 2D cross product of this vector and another vector v.
Definition qgsvector.h:192
QgsVector operator/(double scalar) const
Returns a vector where the components have been divided by a scalar value.
Definition qgsvector.h:73
double angle() const
Returns the angle of the vector in radians.
Definition qgsvector.h:172
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:83
double angle(QgsVector v) const
Returns the angle between this vector and another vector in radians.
Definition qgsvector.h:181
QgsVector operator+(QgsVector other) const
Adds another vector to this vector.
Definition qgsvector.h:91
QgsVector & operator-=(QgsVector other)
Subtracts another vector to this vector in place.
Definition qgsvector.h:117
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise).
Definition qgsvector.h:164
double x() const
Returns the vector's x-component.
Definition qgsvector.h:147
QgsVector operator*(double scalar) const
Returns a vector where the components have been multiplied by a scalar value.
Definition qgsvector.h:64
QgsVector()=default
Default constructor for QgsVector.
double length() const
Returns the length of the vector.
Definition qgsvector.h:128
bool operator!=(QgsVector other) const
Definition qgsvector.h:215
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6817
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900
#define SIP_HOLDGIL
Definition qgis_sip.h:179
#define SIP_THROW(name,...)
Definition qgis_sip.h:211
Q_DECLARE_TYPEINFO(QgsVector, Q_MOVABLE_TYPE)