diff options
-rw-r--r-- | src/direction.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/direction.h b/src/direction.h index e11b1f3..3dd95f9 100644 --- a/src/direction.h +++ b/src/direction.h | |||
@@ -1,6 +1,7 @@ | |||
1 | #ifndef DIRECTION_H_AB66A90E | 1 | #ifndef DIRECTION_H_AB66A90E |
2 | #define DIRECTION_H_AB66A90E | 2 | #define DIRECTION_H_AB66A90E |
3 | 3 | ||
4 | #include <cmath> | ||
4 | #include <string> | 5 | #include <string> |
5 | #include <stdexcept> | 6 | #include <stdexcept> |
6 | #include "vector.h" | 7 | #include "vector.h" |
@@ -64,4 +65,29 @@ inline Direction oppositeDirection(Direction value) { | |||
64 | } | 65 | } |
65 | } | 66 | } |
66 | 67 | ||
68 | inline Direction directionFacingPoint(vec2i point) { | ||
69 | double theta = atan2(point.y(), point.x()); | ||
70 | theta /= M_PI; | ||
71 | |||
72 | if (theta < -7.0/8.0) { | ||
73 | return Direction::left; | ||
74 | } else if (theta < -5.0/8.0) { | ||
75 | return Direction::down_left; | ||
76 | } else if (theta < -3.0/8.0) { | ||
77 | return Direction::down; | ||
78 | } else if (theta < -1.0/8.0) { | ||
79 | return Direction::down_right; | ||
80 | } else if (theta < 1.0/8.0) { | ||
81 | return Direction::right; | ||
82 | } else if (theta < 3.0/8.0) { | ||
83 | return Direction::up_right; | ||
84 | } else if (theta < 5.0/8.0) { | ||
85 | return Direction::up; | ||
86 | } else if (theta < 7.0/8.0) { | ||
87 | return Direction::up_left; | ||
88 | } else { | ||
89 | return Direction::left; | ||
90 | } | ||
91 | } | ||
92 | |||
67 | #endif /* end of include guard: DIRECTION_H_AB66A90E */ | 93 | #endif /* end of include guard: DIRECTION_H_AB66A90E */ |