Skip to content

Commit 45aa88b

Browse files
committed
add direction()
1 parent 1cc60b8 commit 45aa88b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/gf2/core/Direction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace gf {
2121
GF_CORE_API Vec2F unit(Direction direction);
2222
GF_CORE_API Vec2I displacement(Direction direction);
2323
GF_CORE_API float angle(Direction direction);
24+
GF_CORE_API Direction direction(float angle);
2425
GF_CORE_API Direction opposite(Direction direction);
2526

2627
} // namespace gf

library/core/Direction.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ namespace gf {
5858
return static_cast<float>(direction) * Pi / 2;
5959
}
6060

61+
Direction direction(float angle)
62+
{
63+
float normalized = std::fmod(angle, 2 * Pi);
64+
65+
if (angle < 0) {
66+
assert(normalized < 0);
67+
normalized += 2 * Pi;
68+
}
69+
70+
assert(0.0f <= normalized && normalized < 2 * Pi);
71+
72+
static constexpr float Pi4 = Pi / 4.0f;
73+
74+
if (normalized < 1 * Pi4) {
75+
return Direction::Right;
76+
}
77+
if (normalized < 3 * Pi4) {
78+
return Direction::Down;
79+
}
80+
if (normalized < 5 * Pi4) {
81+
return Direction::Right;
82+
}
83+
if (normalized < 7 * Pi4) {
84+
return Direction::Up;
85+
}
86+
return Direction::Right;
87+
}
88+
6189
Direction opposite(Direction direction)
6290
{
6391
if (direction == Direction::Center) {

0 commit comments

Comments
 (0)