1#ifndef RIVET_MATH_VECTOR2
2#define RIVET_MATH_VECTOR2
4#include "Rivet/Math/MathConstants.hh"
5#include "Rivet/Math/MathUtils.hh"
6#include "Rivet/Math/VectorN.hh"
28 class Vector2 :
public Vector<2> {
31 friend Vector2 multiply(
const double,
const Vector2&);
32 friend Vector2 multiply(
const Vector2&,
const double);
33 friend Vector2 add(
const Vector2&,
const Vector2&);
34 friend Vector2 subtract(
const Vector2&,
const Vector2&);
37 Vector2() : Vector<2>() { }
39 template<
typename V2TYPE>
40 Vector2(
const V2TYPE& other) {
41 this->setX(other.x());
42 this->setY(other.y());
45 Vector2(
const Vector<2>& other) {
46 this->setX(other.get(0));
47 this->setY(other.get(1));
50 Vector2(
double x,
double y) {
60 static Vector2 mkX() {
return Vector2(1,0); }
61 static Vector2 mkY() {
return Vector2(0,1); }
66 double x()
const {
return get(0); }
67 double y()
const {
return get(1); }
68 Vector2& setX(
double x) {
set(0, x);
return *
this; }
69 Vector2& setY(
double y) {
set(1, y);
return *
this; }
73 double dot(
const Vector2& v)
const {
74 return _vec.dot(v._vec);
78 double angle(
const Vector2& v)
const {
80 if (localDotOther > 1.0)
return 0.0;
81 if (localDotOther < -1.0)
return M_PI;
82 return acos(localDotOther);
90 else return *
this * 1.0/md;
100 Vector2& operator*=(
const double a) {
101 _vec = multiply(a, *
this)._vec;
105 Vector2& operator/=(
const double a) {
106 _vec = multiply(1.0/a, *
this)._vec;
110 Vector2& operator+=(
const Vector2& v) {
111 _vec = add(*
this, v)._vec;
115 Vector2& operator-=(
const Vector2& v) {
116 _vec = subtract(*
this, v)._vec;
136 result._vec = a * v._vec;
141 return multiply(a, v);
145 return multiply(a, v);
149 return multiply(a, v);
153 return multiply(1.0/a, v);
158 result._vec = a._vec + b._vec;
164 result._vec = a._vec - b._vec;
Two-dimensional specialisation of Vector.
Definition Vector2.hh:28
double dot(const Vector2 &v) const
Dot-product with another vector.
Definition Vector2.hh:73
Vector2 unit() const
Synonym for unitVec.
Definition Vector2.hh:94
Vector2 unitVec() const
Unit-normalized version of this vector.
Definition Vector2.hh:87
double angle(const Vector2 &v) const
Angle in radians to another vector.
Definition Vector2.hh:78
double mod() const
Definition VectorN.hh:95
Vector< N > & set(const size_t index, const double value)
Definition VectorN.hh:60
Vector< N > operator-() const
Definition VectorN.hh:102
Definition MC_CENT_PPB_Projections.hh:10
double subtract(double a, double b, double tolerance=1e-5)
Subtract two numbers with FP fuzziness.
Definition MathUtils.hh:223
double add(double a, double b, double tolerance=1e-5)
Add two numbers with FP fuzziness.
Definition MathUtils.hh:229
double angle(const Vector2 &a, const Vector2 &b)
Angle (in radians) between two 2-vectors.
Definition Vector2.hh:177
std::enable_if_t< std::is_arithmetic_v< N1 > &&std::is_arithmetic_v< N2 >, bool > fuzzyLessEquals(N1 a, N2 b, double tolerance=1e-5)
Compare two floating point numbers for <= with a degree of fuzziness.
Definition MathUtils.hh:94