QGIS API Documentation 4.1.0-Master (60fea48833c)
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 public:
39 QgsVector() = default;
40
46 QgsVector( double x, double y )
47 : mX( x )
48 , mY( y )
49 {}
50
51 // clang-format off
54 // clang-format on
55 {
56 return QgsVector( -mX, -mY );
57 }
58
63 QgsVector operator*( double scalar ) const SIP_HOLDGIL
64 {
65 return QgsVector( mX * scalar, mY * scalar );
66 }
67
72 QgsVector operator/( double scalar ) const SIP_HOLDGIL
73 {
74 return *this * ( 1.0 / scalar );
75 }
76
83 {
84 return mX * v.mX + mY * v.mY;
85 }
86
91 {
92 return QgsVector( mX + other.mX, mY + other.mY );
93 }
94
99 {
100 mX += other.mX;
101 mY += other.mY;
102 return *this;
103 }
104
109 {
110 return QgsVector( mX - other.mX, mY - other.mY );
111 }
112
117 {
118 mX -= other.mX;
119 mY -= other.mY;
120 return *this;
121 }
122
127 double length() const SIP_HOLDGIL
128 {
129 return std::sqrt( mX * mX + mY * mY );
130 }
131
138 {
139 return mX * mX + mY * mY;
140 }
141
146 double x() const SIP_HOLDGIL
147 {
148 return mX;
149 }
150
155 double y() const SIP_HOLDGIL
156 {
157 return mY;
158 }
159
164 {
165 return QgsVector( -mY, mX );
166 }
167
171 double angle() const SIP_HOLDGIL
172 {
173 const double angle = std::atan2( mY, mX );
174 return angle < 0.0 ? angle + 2.0 * M_PI : angle;
175 }
176
180 double angle( QgsVector v ) const SIP_HOLDGIL
181 {
182 return v.angle() - angle();
183 }
184
192 {
193 return mX * v.y() - mY * v.x();
194 }
195
200 QgsVector rotateBy( double rot ) const SIP_HOLDGIL;
201
207 QgsVector normalized() const SIP_THROW( QgsException );
208
209 bool operator==( QgsVector other ) const SIP_HOLDGIL
210 {
211 return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
212 }
213
214 bool operator!=( QgsVector other ) const
215 {
216 return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
217 }
218
223 QString toString( int precision = 17 ) const SIP_HOLDGIL
224 {
225 QString str = "Vector (";
226 str += qgsDoubleToString( mX, precision );
227 str += ", ";
228 str += qgsDoubleToString( mY, precision );
229 str += ')';
230 return str;
231 }
232
233#ifdef SIP_RUN
234// clang-format off
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// clang-format on
241#endif
242
243 private:
244 double mX = 0.0;
245 double mY = 0.0;
246
247};
248
249Q_DECLARE_TYPEINFO( QgsVector, Q_MOVABLE_TYPE );
250
251#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:98
QgsVector operator-() const
Swaps the sign of the x and y components of the vector.
Definition qgsvector.h:53
double y() const
Returns the vector's y-component.
Definition qgsvector.h:155
QgsVector(double x, double y)
Constructor for QgsVector taking x and y component values.
Definition qgsvector.h:46
QgsVector operator-(QgsVector other) const
Subtracts another vector to this vector.
Definition qgsvector.h:108
double lengthSquared() const
Returns the length of the vector.
Definition qgsvector.h:137
QString toString(int precision=17) const
Returns a string representation of the vector.
Definition qgsvector.h:223
double crossProduct(QgsVector v) const
Returns the 2D cross product of this vector and another vector v.
Definition qgsvector.h:191
QgsVector operator/(double scalar) const
Returns a vector where the components have been divided by a scalar value.
Definition qgsvector.h:72
double angle() const
Returns the angle of the vector in radians.
Definition qgsvector.h:171
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:82
double angle(QgsVector v) const
Returns the angle between this vector and another vector in radians.
Definition qgsvector.h:180
QgsVector operator+(QgsVector other) const
Adds another vector to this vector.
Definition qgsvector.h:90
QgsVector & operator-=(QgsVector other)
Subtracts another vector to this vector in place.
Definition qgsvector.h:116
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise).
Definition qgsvector.h:163
double x() const
Returns the vector's x-component.
Definition qgsvector.h:146
QgsVector operator*(double scalar) const
Returns a vector where the components have been multiplied by a scalar value.
Definition qgsvector.h:63
QgsVector()=default
Default constructor for QgsVector.
double length() const
Returns the length of the vector.
Definition qgsvector.h:127
bool operator!=(QgsVector other) const
Definition qgsvector.h:214
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6893
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
#define SIP_HOLDGIL
Definition qgis_sip.h:178
#define SIP_THROW(name,...)
Definition qgis_sip.h:210
Q_DECLARE_TYPEINFO(QgsVector, Q_MOVABLE_TYPE)