use std::ops::{Add, Neg, Sub};
use num_traits::Signed;
pub const PREC_DISTANCE: DistanceType = 1e-5;
pub type FloatType = f64;
pub type DistanceType = FloatType;
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug, Default)]
pub enum Angle {
#[default]
R0,
R90,
R180,
R270,
}
impl Angle {
pub fn to_int(&self) -> u32 {
match self {
Angle::R0 => 0,
Angle::R90 => 1,
Angle::R180 => 2,
Angle::R270 => 3,
}
}
pub fn from_int(a: u32) -> Self {
match a % 4 {
0 => Angle::R0,
1 => Angle::R90,
2 => Angle::R180,
3 => Angle::R270,
_ => unreachable!(),
}
}
}
impl Add for Angle {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::from_int(self.to_int() + rhs.to_int())
}
}
impl Sub for Angle {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::from_int(self.to_int() + 4 - rhs.to_int())
}
}
impl Neg for Angle {
type Output = Self;
fn neg(self) -> Self::Output {
Self::from_int(4 - self.to_int())
}
}
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub enum Side {
Left,
Center,
Right,
}
impl Side {
pub fn is_left(&self) -> bool {
*self == Side::Left
}
pub fn is_right(&self) -> bool {
*self == Side::Right
}
pub fn is_center(&self) -> bool {
*self == Side::Center
}
pub fn other(&self) -> Self {
match self {
Side::Left => Side::Right,
Side::Center => Side::Center,
Side::Right => Side::Left,
}
}
}
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub enum Orientation {
ClockWise,
CounterClockWise,
Straight,
}
impl Orientation {
pub fn is_clock_wise(self) -> bool {
self == Self::ClockWise
}
pub fn is_counter_clock_wise(self) -> bool {
self == Self::CounterClockWise
}
pub fn is_straight(self) -> bool {
self == Self::Straight
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Contains {
No,
OnBounds,
WithinBounds,
}
impl Contains {
pub fn is_within_bounds(&self) -> bool {
matches!(self, Contains::WithinBounds)
}
pub fn inclusive_bounds(&self) -> bool {
matches!(
self,
Contains::WithinBounds | Contains::OnBounds
)
}
pub fn on_bounds(&self) -> bool {
matches!(self, Contains::OnBounds)
}
pub fn is_no(&self) -> bool {
matches!(self, Contains::No)
}
pub fn max(self, other: Self) -> Self {
match (self, other) {
(Contains::WithinBounds, _) => Contains::WithinBounds,
(_, Contains::WithinBounds) => Contains::WithinBounds,
(Contains::OnBounds, _) => Contains::OnBounds,
(_, Contains::OnBounds) => Contains::OnBounds,
(Contains::No, _) => Contains::No,
}
}
pub fn min(self, other: Self) -> Self {
match (self, other) {
(Contains::No, _) => Contains::No,
(_, Contains::No) => Contains::No,
(Contains::OnBounds, _) => Contains::OnBounds,
(_, Contains::OnBounds) => Contains::OnBounds,
(Contains::WithinBounds, _) => Contains::WithinBounds,
}
}
}
#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
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)]
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)]
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()
}
}