-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.hpp
More file actions
114 lines (93 loc) · 1.7 KB
/
vector.hpp
File metadata and controls
114 lines (93 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
struct vector{
double x = 0;
double y = 0;
vector operator+(vector v){
return {x + v.x, y + v.y};
}
vector operator-(vector v){
return {x - v.x, y - v.y};
}
vector operator-(){
return flip();
}
void operator+=(vector v){
this -> x += v.x;
this -> y += v.y;
}
void operator-=(vector v){
this -> x -= v.x;
this -> y -= v.y;
}
double angle(){
double r = 0;
if (x != 0){
r = atan(y/x);
}
else{
if (y > 0){
r = PI/2;
}
else{
r = -PI/2;
}
}
if (x < 0){
r += PI;
}
return r;
}
float magnitude(){
return sqrt(x * x + y * y);
}
void setMandA(float mag, float ang){
x = cos(ang) * mag; // Brush up on yer trig young man
y = sin(ang) * mag;
}
void setMagnitude(float mag){
setMandA(mag, angle());
}
void setAngle(float ang){
setMandA(magnitude(), ang);
}
vector rotate(float amount){
vector r;
r.setMandA(magnitude(), angle() + amount);
return r;
}
vector flip(){
vector r;
r.x = -x;
r.y = -y;
return r;
}
bool isZero(){
return (x == 0) && (y == 0);
}
void dead(float band){
if (magnitude() < band){
zero();
}
}
void zero(){
x = 0;
y = 0;
}
void cap (float top){
if (magnitude() > top){
setMagnitude(top);
}
}
void speedLimit(double cap){
setMagnitude(magnitude() * cap);
if (magnitude() > cap){
setMagnitude(cap);
}
}
void SetPercent(float p){ /* For PIDController compatibility */
setMagnitude(p);
}
std::string string(){
return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
}
};