summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-06 21:16:27 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-06 21:16:27 -0500
commit7ff951c90b0db3cbd66b9cc2fbb9c58941c953b4 (patch)
treed9816133f455ae66691de9b7c977dd99aa87bf59
parent0e1e97da9e8937d19b0101dcc1f3a16e3db495b6 (diff)
downloadtanetane-7ff951c90b0db3cbd66b9cc2fbb9c58941c953b4.tar.gz
tanetane-7ff951c90b0db3cbd66b9cc2fbb9c58941c953b4.tar.bz2
tanetane-7ff951c90b0db3cbd66b9cc2fbb9c58941c953b4.zip
Added function to get Direction from one point to another
-rw-r--r--src/direction.h26
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
68inline 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 */