ART v1.0-alpha
A Robot Template that raises the floor for VRC teams
Loading...
Searching...
No Matches
Vec2.cpp
Go to the documentation of this file.
1
15#include "ART/Vec2.h"
16
17namespace art
18{
19 Vec2::Vec2() : x(0.0), y(0.0) {}
20
21 double Vec2::magnitude() const
22 {
23 return std::sqrt(pow(x, 2) + pow(y, 2));
24 }
25
26 double Vec2::direction() const
27 {
28 double angle = 0;
29 angle = atan2(x, y);
30 return angle;
31 }
32
34 {
35 double scale = 1.f / magnitude();
36 Vec2 out = *this * scale;
37 return out;
38 }
39
40 double Vec2::distTo(Vec2 target) const
41 {
42 Vec2 dist = target - *this;
43 return dist.magnitude();
44 }
45
46 double Vec2::angleTo(Vec2 target) const
47 {
48 Vec2 dist = target - *this;
49 return dist.direction();
50 }
51
52 Vec2 Vec2::operator+(Vec2 const& obj) const
53 {
54 Vec2 output = XandY(this->x + obj.x, this->y + obj.y);
55 return output;
56 }
57
58 Vec2 Vec2::operator-(Vec2 const& obj) const
59 {
60 Vec2 output = XandY(this->x - obj.x, this->y - obj.y);
61 return output;
62 }
63
64 Vec2 Vec2::operator*(double const& scale) const
65 {
66 Vec2 output = XandY(this->x * scale, this->y * scale);
67 return output;
68 }
69
70 double Vec2::operator*(Vec2 const& other) const
71 {
72 return x*other.x + y*other.y ;
73 }
74
76 {
77 this->x += obj.x;
78 this->y += obj.y;
79 return *this;
80 }
81
83 {
84 this->x -= obj.x;
85 this->y -= obj.y;
86 return *this;
87 }
88
89 Vec2& Vec2::operator*=(double const& scale)
90 {
91 this->x *= scale;
92 this->y *= scale;
93 return *this;
94 }
95
96 Vec2 Vec2::XandY(double x, double y)
97 {
98 Vec2 output;
99 output.x = x;
100 output.y = y;
101 return output;
102 }
103
104 Vec2 Vec2::dirAndMag(double dir, double mag)
105 {
106 Vec2 output;
107 output.x = mag * sin(dir);
108 output.y = mag * cos(dir);
109 return output;
110 }
111} // namespace art
Header containing the 2d Vector class.
Definition PID.h:20
A Utility 2D Vector class.
Definition Vec2.h:39
Vec2 & operator+=(Vec2 const &obj)
Adds another vector to this one.
Definition Vec2.cpp:75
Vec2()
Construct a new Vec 2 object.
Definition Vec2.cpp:19
double y
Stores the Y component of the Vector.
Definition Vec2.h:54
Vec2 operator*(double const &scale) const
Returns a vector after scaling it.
Definition Vec2.cpp:64
double x
Stores the X component of the Vector.
Definition Vec2.h:46
Vec2 operator-(Vec2 const &obj) const
Returns the difference of 2 vectors.
Definition Vec2.cpp:58
double angleTo(Vec2 target) const
Gets the angle from this vector to another.
Definition Vec2.cpp:46
double direction() const
Gets the direction of the Vector.
Definition Vec2.cpp:26
Vec2 & operator*=(double const &scale)
Changes the vector by a scale.
Definition Vec2.cpp:89
Vec2 operator+(Vec2 const &obj) const
Returns the sum of 2 vectors.
Definition Vec2.cpp:52
double magnitude() const
Gets the magnitude of the Vector.
Definition Vec2.cpp:21
Vec2 & operator-=(Vec2 const &obj)
Substracts another vector from this one.
Definition Vec2.cpp:82
double distTo(Vec2 target) const
Gets the distance from this vector to another.
Definition Vec2.cpp:40
static Vec2 dirAndMag(double dir, double mag)
Constructs a vector with the specified direction and magnitude.
Definition Vec2.cpp:104
Vec2 normalize() const
Returns a unit Vector with the same direction.
Definition Vec2.cpp:33
static Vec2 XandY(double x, double y)
Constructs a vector with the specified x and y components.
Definition Vec2.cpp:96