From 7ff951c90b0db3cbd66b9cc2fbb9c58941c953b4 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 6 Feb 2021 21:16:27 -0500 Subject: Added function to get Direction from one point to another --- src/direction.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 @@ #ifndef DIRECTION_H_AB66A90E #define DIRECTION_H_AB66A90E +#include #include #include #include "vector.h" @@ -64,4 +65,29 @@ inline Direction oppositeDirection(Direction value) { } } +inline Direction directionFacingPoint(vec2i point) { + double theta = atan2(point.y(), point.x()); + theta /= M_PI; + + if (theta < -7.0/8.0) { + return Direction::left; + } else if (theta < -5.0/8.0) { + return Direction::down_left; + } else if (theta < -3.0/8.0) { + return Direction::down; + } else if (theta < -1.0/8.0) { + return Direction::down_right; + } else if (theta < 1.0/8.0) { + return Direction::right; + } else if (theta < 3.0/8.0) { + return Direction::up_right; + } else if (theta < 5.0/8.0) { + return Direction::up; + } else if (theta < 7.0/8.0) { + return Direction::up_left; + } else { + return Direction::left; + } +} + #endif /* end of include guard: DIRECTION_H_AB66A90E */ -- cgit 1.4.1