-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.cpp
67 lines (49 loc) · 1.55 KB
/
position.cpp
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
#include "Header.h"
void position::sum(position pos) {
x += pos.x;
y += pos.y;
}
position position::mins(position pos) {
position n_pos;
n_pos.x = x - pos.x;
n_pos.y = y - pos.y;
return n_pos;
}
position position::mult(float num) {
return position(x * num, y * num);
}
position position::div(float num) {
return position(x / num, y / num);
}
float position::dist(const position& other) {
return std::sqrt(pow((other.x - x), 2) + pow((other.y - y), 2));
}
float position::len() {
return std::sqrt(pow(x, 2) + pow(y, 2));
}
position position::normalise() {
float len = this->len();
if (len == 0) return *this;
return this->div(len);
}
void position::follow(const position& to_pos, float speed, float min_dist = 0) {
if (!(position(x, y).dist(to_pos) <= min_dist)) {
position between_vec = position(to_pos.x - x, to_pos.y - y);
between_vec = between_vec.normalise();
between_vec = between_vec.mult(speed);
this->sum(between_vec);
//return position(x, y).sum(between_vec);
}
}
void position::move_to(position to_pos, float min_dist = 0) {
position between_vec = to_pos.mins(*this);
float len = between_vec.len();
if (len > min_dist) {
this->sum(between_vec.mult((len - min_dist) / len));
//return position(x, y).sum(between_vec.mult((len - min_dist) / len));
}
//return *this;
}
position position::coords_to_vec_space(position coord_pos, int cols, int rows) {
return position(coord_pos.x + cols / 2, ((rows / 2) - coord_pos.y) * 2);
}