QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <QtGlobal>
23
30class CORE_EXPORT QgsVector
31{
32
33 public:
34
38 QgsVector() = default;
39
45 QgsVector( double x, double y )
46 : mX( x )
47 , mY( y )
48 {
49 }
50
53 {
54 return QgsVector( -mX, -mY );
55 }
56
61 QgsVector operator*( double scalar ) const SIP_HOLDGIL
62 {
63 return QgsVector( mX * scalar, mY * scalar );
64 }
65
70 QgsVector operator/( double scalar ) const SIP_HOLDGIL
71 {
72 return *this * ( 1.0 / scalar );
73 }
74
81 {
82 return mX * v.mX + mY * v.mY;
83 }
84
89 {
90 return QgsVector( mX + other.mX, mY + other.mY );
91 }
92
97 {
98 mX += other.mX;
99 mY += other.mY;
100 return *this;
101 }
102
107 {
108 return QgsVector( mX - other.mX, mY - other.mY );
109 }
110
115 {
116 mX -= other.mX;
117 mY -= other.mY;
118 return *this;
119 }
120
125 double length() const SIP_HOLDGIL
126 {
127 return std::sqrt( mX * mX + mY * mY );
128 }
129
136 {
137 return mX * mX + mY * mY;
138 }
139
144 double x() const SIP_HOLDGIL
145 {
146 return mX;
147 }
148
153 double y() const SIP_HOLDGIL
154 {
155 return mY;
156 }
157
162 {
163 return QgsVector( -mY, mX );
164 }
165
169 double angle() const SIP_HOLDGIL
170 {
171 const double angle = std::atan2( mY, mX );
172 return angle < 0.0 ? angle + 2.0 * M_PI : angle;
173 }
174
178 double angle( QgsVector v ) const SIP_HOLDGIL
179 {
180 return v.angle() - angle();
181 }
182
190 {
191 return mX * v.y() - mY * v.x();
192 }
193
198 QgsVector rotateBy( double rot ) const SIP_HOLDGIL;
199
205 QgsVector normalized() const SIP_THROW( QgsException );
206
207 bool operator==( QgsVector other ) const SIP_HOLDGIL
208 {
209 return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
210 }
211
212 bool operator!=( QgsVector other ) const
213 {
214 return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
215 }
216
221 QString toString( int precision = 17 ) const SIP_HOLDGIL
222 {
223 QString str = "Vector (";
224 str += qgsDoubleToString( mX, precision );
225 str += ", ";
226 str += qgsDoubleToString( mY, precision );
227 str += ')';
228 return str;
229 }
230
231#ifdef SIP_RUN
232 SIP_PYOBJECT __repr__();
233 % MethodCode
234 QString str = QStringLiteral( "<QgsVector: %1>" ).arg( sipCpp->toString() );
235 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
236 % End
237#endif
238
239 private:
240 double mX = 0.0;
241 double mY = 0.0;
242
243};
244
245Q_DECLARE_TYPEINFO( QgsVector, Q_MOVABLE_TYPE );
246
247#endif // QGSVECTOR_H
Defines a QGIS exception class.
Represent a 2-dimensional vector.
Definition qgsvector.h:31
QgsVector & operator+=(QgsVector other)
Adds another vector to this vector in place.
Definition qgsvector.h:96
QgsVector operator-() const
Swaps the sign of the x and y components of the vector.
Definition qgsvector.h:52
double y() const
Returns the vector's y-component.
Definition qgsvector.h:153
QgsVector(double x, double y)
Constructor for QgsVector taking x and y component values.
Definition qgsvector.h:45
QgsVector operator-(QgsVector other) const
Subtracts another vector to this vector.
Definition qgsvector.h:106
double lengthSquared() const
Returns the length of the vector.
Definition qgsvector.h:135
QString toString(int precision=17) const
Returns a string representation of the vector.
Definition qgsvector.h:221
double crossProduct(QgsVector v) const
Returns the 2D cross product of this vector and another vector v.
Definition qgsvector.h:189
QgsVector operator/(double scalar) const
Returns a vector where the components have been divided by a scalar value.
Definition qgsvector.h:70
double angle() const
Returns the angle of the vector in radians.
Definition qgsvector.h:169
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:80
double angle(QgsVector v) const
Returns the angle between this vector and another vector in radians.
Definition qgsvector.h:178
QgsVector operator+(QgsVector other) const
Adds another vector to this vector.
Definition qgsvector.h:88
QgsVector & operator-=(QgsVector other)
Subtracts another vector to this vector in place.
Definition qgsvector.h:114
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise).
Definition qgsvector.h:161
double x() const
Returns the vector's x-component.
Definition qgsvector.h:144
QgsVector operator*(double scalar) const
Returns a vector where the components have been multiplied by a scalar value.
Definition qgsvector.h:61
QgsVector()=default
Default constructor for QgsVector.
double length() const
Returns the length of the vector.
Definition qgsvector.h:125
bool operator!=(QgsVector other) const
Definition qgsvector.h:212
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6524
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607
#define SIP_HOLDGIL
Definition qgis_sip.h:179
#define SIP_THROW(name,...)
Definition qgis_sip.h:211
Q_DECLARE_TYPEINFO(QgsVector, Q_MOVABLE_TYPE)