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 *
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 }
bool operator!=(QgsVector other) const
Inequality operator.
Definition: qgsvector.cpp:131
QgsVector rotateBy(double rot) const
Rotates the vector by a specified angle.
Definition: qgsvector.cpp:107
QgsVector & operator+=(QgsVector other)
Adds another vector to this vector in place.
Definition: qgsvector.cpp:52
bool operator==(QgsVector other) const
Equality operator.
Definition: qgsvector.cpp:126
QgsVector operator+(QgsVector other) const
Adds another vector to this vector.
Definition: qgsvector.cpp:47
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:278
double y() const
Returns the vector&#39;s y-component.
Definition: qgsvector.cpp:81
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-() const
Swaps the sign of the x and y components of the vector.
Definition: qgsvector.cpp:27
QgsVector normalized() const
Returns the vector&#39;s normalized (or "unit") vector (ie same angle but length of 1.0).
Definition: qgsvector.cpp:114
A class to represent a vector.
Definition: qgsvector.h:28
double length() const
Returns the length of the vector.
Definition: qgsvector.cpp:71
QgsVector operator*(double scalar) const
Returns a vector where the components have been multiplied by a scalar value.
Definition: qgsvector.cpp:32
QgsVector & operator-=(QgsVector other)
Subtracts another vector to this vector in place.
Definition: qgsvector.cpp:64
double crossProduct(QgsVector v) const
Returns the 2D cross product of this vector and another vector v.
Definition: qgsvector.cpp:102
double x() const
Returns the vector&#39;s x-component.
Definition: qgsvector.cpp:76
QgsVector operator/(double scalar) const
Returns a vector where the components have been divided by a scalar value.
Definition: qgsvector.cpp:37
Defines a QGIS exception class.
Definition: qgsexception.h:34
QgsVector()=default
Default constructor for QgsVector.