use num_traits::Signed;
#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Direction1D {
Low,
High,
}
impl Direction1D {
pub fn reversed(self) -> Self {
match self {
Direction1D::Low => Direction1D::High,
Direction1D::High => Direction1D::Low,
}
}
pub fn sign<N>(&self) -> N
where
N: Signed,
{
match self {
Direction1D::Low => N::one().neg(),
Direction1D::High => N::one(),
}
}
}
impl From<Direction2D> for Direction1D {
fn from(d2: Direction2D) -> Self {
match d2 {
Direction2D::Right | Direction2D::Up => Direction1D::High,
Direction2D::Left | Direction2D::Down => Direction1D::Low,
}
}
}
#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Orientation2D {
Horizontal,
Vertical,
}
impl Orientation2D {
pub fn other(self) -> Self {
match self {
Orientation2D::Horizontal => Self::Vertical,
Orientation2D::Vertical => Self::Horizontal,
}
}
pub fn direction(self, d1: Direction1D) -> Direction2D {
use Direction1D::*;
use Direction2D::*;
use Orientation2D::*;
match (self, d1) {
(Horizontal, Low) => Right,
(Horizontal, High) => Left,
(Vertical, Low) => Down,
(Vertical, High) => Up,
}
}
pub fn is_vertical(&self) -> bool {
self == &Self::Vertical
}
pub fn is_horizontal(&self) -> bool {
self == &Self::Horizontal
}
}
#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Direction2D {
Left,
Up,
Right,
Down,
}
impl Direction2D {
pub fn reversed(self) -> Self {
match self {
Direction2D::Left => Self::Right,
Direction2D::Up => Self::Down,
Direction2D::Right => Self::Left,
Direction2D::Down => Self::Up,
}
}
pub fn orientation(&self) -> Orientation2D {
match self {
Direction2D::Left | Direction2D::Right => Orientation2D::Horizontal,
Direction2D::Up | Direction2D::Down => Orientation2D::Vertical,
}
}
pub fn sign<N>(self) -> N
where
N: Signed,
{
let d1: Direction1D = self.into();
d1.sign()
}
}