Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
vector_2d.hpp
Go to the documentation of this file.
1#ifndef FANG_ROBOTICS_MCB_UTIL_MATH_LINEAR_VECTOR_2D_HPP
2#define FANG_ROBOTICS_MCB_UTIL_MATH_LINEAR_VECTOR_2D_HPP
3#include <cmath>
4namespace fang::math
5{
6 template <typename Unit>
7 struct Vector2D
8 {
9 using unit = Unit;
10 public:
11 Unit x;
12 Unit y;
13
18 {
19 const double rawX{static_cast<double>(x)};
20 const double rawY{static_cast<double>(y)};
21 return Vector2D<double>{rawX, rawY};
22 }
23
24 Unit getMagnitude() const
25 {
26 //Traditional euclidean distance formula
27 //the magnitude of a vector is the square root
28 //of the square sum of its components
29 //Given the use of non-linear math functions
30 //and the desire for general use beyond units.h
31 //the stripped forms are used
32 //since the inputs and outputs are wrapped in units
33 //dimensional consistnency is ensured
34
35 const Vector2D<double> raw{getRawVector()};
36
37 const double xSquared{raw.x * raw.x};
38 const double ySquared{raw.y * raw.y};
39 const double squareSum{xSquared + ySquared};
40 const double distance{std::sqrt(squareSum)};
41 return Unit{distance};
42 }
43
48 {
49 const double rawMagnitude{static_cast<double>(getMagnitude())};
50 const Vector2D<double> raw{getRawVector()};
51
52 //We cannotuse the operators due to template shenanigans :D
53 return Vector2D<double>{raw.x / rawMagnitude, raw.y / rawMagnitude};
54 }
55
57 {
58 return Vector2D{-x, -y};
59 }
60
62 {
63 return Vector2D{x + addend.x, y + addend.y};
64 }
65
67 {
68 return {x - subtrahend.x, y - subtrahend.y};
69 }
70
72 {
73 return Vector2D{x * scalar, y * scalar};
74 }
75 };
76
77
78 template <typename ScaleUnit>
79 Vector2D<ScaleUnit> operator*(const Vector2D<double>& vector, const ScaleUnit& scale)
80 {
81 return Vector2D<ScaleUnit>{vector.x * scale, vector.y * scale};
82 }
83
84 template <typename ScaleUnit>
85 Vector2D<ScaleUnit> operator*(const ScaleUnit& scale, const Vector2D<double>& vector)
86 {
87 return vector * scale;
88 }
89
90 template <typename Unit>
91 Vector2D<Unit> operator*(const double& scale, const Vector2D<Unit>& vector)
92 {
93 return vector * scale;
94 }
95}
96#endif
Definition range.hpp:4
Vector2D< ScaleUnit > operator*(const Vector2D< double > &vector, const ScaleUnit &scale)
Definition vector_2d.hpp:79
Definition vector_2d.hpp:8
Vector2D< double > getUnitVector() const
Definition vector_2d.hpp:47
Unit x
Definition vector_2d.hpp:11
Vector2D< Unit > operator*(double scalar)
Definition vector_2d.hpp:71
Vector2D< Unit > operator-(const Vector2D &subtrahend)
Definition vector_2d.hpp:66
Vector2D< Unit > operator+(const Vector2D &addend)
Definition vector_2d.hpp:61
Vector2D< Unit > operator-()
Definition vector_2d.hpp:56
Vector2D< double > getRawVector() const
Definition vector_2d.hpp:17
Unit unit
Definition vector_2d.hpp:9
Unit getMagnitude() const
Definition vector_2d.hpp:24
Unit y
Definition vector_2d.hpp:12