QGIS API Documentation  3.0.2-Girona (307d082)
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 
102 QgsVector QgsVector::rotateBy( double rot ) const
103 {
104  double angle = std::atan2( mY, mX ) + rot;
105  double len = length();
106  return QgsVector( len * std::cos( angle ), len * std::sin( angle ) );
107 }
108 
110 {
111  double len = length();
112 
113  if ( len == 0.0 )
114  {
115  throw QgsException( QStringLiteral( "normalized vector of null vector undefined" ) );
116  }
117 
118  return *this / len;
119 }
120 
121 bool QgsVector::operator==( QgsVector other ) const
122 {
123  return qgsDoubleNear( mX, other.mX ) && qgsDoubleNear( mY, other.mY );
124 }
125 
126 bool QgsVector::operator!=( QgsVector other ) const
127 {
128  return !qgsDoubleNear( mX, other.mX ) || !qgsDoubleNear( mY, other.mY );
129 }
QgsVector operator+(QgsVector other) const
Adds another vector to this vector.
Definition: qgsvector.cpp:47
bool operator!=(QgsVector other) const
Inequality operator.
Definition: qgsvector.cpp:126
QgsVector & operator+=(const QgsVector other)
Adds another vector to this vector in place.
Definition: qgsvector.cpp:52
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:251
QgsVector operator/(double scalar) const
Returns a vector where the components have been divided by a scalar value.
Definition: qgsvector.cpp:37
double length() const
Returns the length of the vector.
Definition: qgsvector.cpp:71
A class to represent a vector.
Definition: qgsvector.h:27
double angle() const
Returns the angle of the vector in radians.
Definition: qgsvector.cpp:91
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise) ...
Definition: qgsvector.cpp:86
QgsVector operator*(double scalar) const
Returns a vector where the components have been multiplied by a scalar value.
Definition: qgsvector.cpp:32
QgsVector rotateBy(double rot) const
Rotates the vector by a specified angle.
Definition: qgsvector.cpp:102
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:76
QgsVector operator-() const
Swaps the sign of the x and y components of the vector.
Definition: qgsvector.cpp:27
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:81
Defines a QGIS exception class.
Definition: qgsexception.h:34
bool operator==(QgsVector other) const
Equality operator.
Definition: qgsvector.cpp:121
QgsVector()=default
Default constructor for QgsVector.
QgsVector normalized() const
Returns the vector&#39;s normalized (or "unit") vector (ie same angle but length of 1.0).
Definition: qgsvector.cpp:109
QgsVector & operator-=(const QgsVector other)
Subtracts another vector to this vector in place.
Definition: qgsvector.cpp:64