QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
7 email : matthias@opengis.ch
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
206 bool operator==( QgsVector other ) const SIP_HOLDGIL
207 {
208 return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
209 }
210
211 bool operator!=( QgsVector other ) const
212 {
213 return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
214 }
215
220 QString toString( int precision = 17 ) const SIP_HOLDGIL
221 {
222 QString str = "Vector (";
224 str += ", ";
226 str += ')';
227 return str;
228 }
229
230#ifdef SIP_RUN
231 SIP_PYOBJECT __repr__();
232 % MethodCode
233 QString str = QStringLiteral( "<QgsVector: %1>" ).arg( sipCpp->toString() );
234 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
235 % End
236#endif
237
238 private:
239 double mX = 0.0;
240 double mY = 0.0;
241
242};
243
244Q_DECLARE_TYPEINFO( QgsVector, Q_MOVABLE_TYPE );
245
246#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:220
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
Definition qgsvector.h:211
#define str(x)
Definition qgis.cpp:39
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:5875
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5958
#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