QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsvector.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvector.cpp - QgsVector
3 
4  ---------------------
5  begin : 24.2.2017
6  copyright : (C) 2017 by Matthias Kuhn
7  email : [email protected]
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 
17 #include "qgsvector.h"
18 #include "qgis.h"
19 #include "qgsexception.h"
20 
21 QgsVector::QgsVector( double x, double y )
22  : mX( x )
23  , mY( y )
24 {
25 }
26 
28 {
29  return QgsVector( -mX, -mY );
30 }
31 
32 QgsVector QgsVector::operator*( double scalar ) const
33 {
34  return QgsVector( mX * scalar, mY * scalar );
35 }
36 
37 QgsVector QgsVector::operator/( double scalar ) const
38 {
39  return *this * ( 1.0 / scalar );
40 }
41 
43 {
44  return mX * v.mX + mY * v.mY;
45 }
46 
48 {
49  return QgsVector( mX + other.mX, mY + other.mY );
50 }
51 
53 {
54  mX += other.mX;
55  mY += other.mY;
56  return *this;
57 }
58 
60 {
61  return QgsVector( mX - other.mX, mY - other.mY );
62 }
63 
65 {
66  mX -= other.mX;
67  mY -= other.mY;
68  return *this;
69 }
70 
71 double QgsVector::length() const
72 {
73  return std::sqrt( mX * mX + mY * mY );
74 }
75 
76 double QgsVector::x() const
77 {
78  return mX;
79 }
80 
81 double QgsVector::y() const
82 {
83  return mY;
84 }
85 
87 {
88  return QgsVector( -mY, mX );
89 }
90 
91 double QgsVector::angle() const
92 {
93  double angle = std::atan2( mY, mX );
94  return angle < 0.0 ? angle + 2.0 * M_PI : angle;
95 }
96 
97 double QgsVector::angle( QgsVector v ) const
98 {
99  return v.angle() - angle();
100 }
101 
103 {
104  return mX * v.y() - mY * v.x();
105 }
106 
107 QgsVector QgsVector::rotateBy( double rot ) const
108 {
109  double angle = std::atan2( mY, mX ) + rot;
110  double len = length();
111  return QgsVector( len * std::cos( angle ), len * std::sin( angle ) );
112 }
113 
115 {
116  double len = length();
117 
118  if ( len == 0.0 )
119  {
120  throw QgsException( QStringLiteral( "normalized vector of null vector undefined" ) );
121  }
122 
123  return *this / len;
124 }
125 
126 bool QgsVector::operator==( QgsVector other ) const
127 {
128  return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
129 }
130 
131 bool QgsVector::operator!=( QgsVector other ) const
132 {
133  return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
134 }
QgsVector::crossProduct
double crossProduct(QgsVector v) const
Returns the 2D cross product of this vector and another vector v.
Definition: qgsvector.cpp:102
QgsException
Definition: qgsexception.h:34
QgsVector::normalized
QgsVector normalized() const
Returns the vector's normalized (or "unit") vector (ie same angle but length of 1....
Definition: qgsvector.cpp:114
QgsVector::operator-=
QgsVector & operator-=(QgsVector other)
Subtracts another vector to this vector in place.
Definition: qgsvector.cpp:64
qgis.h
QgsVector::QgsVector
QgsVector()=default
Default constructor for QgsVector.
qgsvector.h
QgsVector::operator*
QgsVector operator*(double scalar) const
Returns a vector where the components have been multiplied by a scalar value.
Definition: qgsvector.cpp:32
QgsVector::x
double x() const
Returns the vector's x-component.
Definition: qgsvector.cpp:76
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsVector::operator!=
bool operator!=(QgsVector other) const
Inequality operator.
Definition: qgsvector.cpp:131
QgsVector::operator+
QgsVector operator+(QgsVector other) const
Adds another vector to this vector.
Definition: qgsvector.cpp:47
QgsVector::operator-
QgsVector operator-() const
Swaps the sign of the x and y components of the vector.
Definition: qgsvector.cpp:27
QgsVector::operator==
bool operator==(QgsVector other) const
Equality operator.
Definition: qgsvector.cpp:126
QgsVector::y
double y() const
Returns the vector's y-component.
Definition: qgsvector.cpp:81
QgsVector
Definition: qgsvector.h:29
QgsVector::angle
double angle() const
Returns the angle of the vector in radians.
Definition: qgsvector.cpp:91
qgsexception.h
QgsVector::length
double length() const
Returns the length of the vector.
Definition: qgsvector.cpp:71
QgsVector::operator+=
QgsVector & operator+=(QgsVector other)
Adds another vector to this vector in place.
Definition: qgsvector.cpp:52
QgsVector::perpVector
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
Definition: qgsvector.cpp:86
QgsVector::rotateBy
QgsVector rotateBy(double rot) const
Rotates the vector by a specified angle.
Definition: qgsvector.cpp:107
QgsVector::operator/
QgsVector operator/(double scalar) const
Returns a vector where the components have been divided by a scalar value.
Definition: qgsvector.cpp:37