use std::ops::{
Add,
Sub,
Mul,
Div
};
use crate::angle::Angle;
pub type Distance = f64;
#[derive(Clone, Copy)]
pub struct Pos<T: Sized + Add + Mul + Div + Sub>(pub T, pub T);
macro_rules! pos_impl {
($t: ty) => {
impl From<($t, $t)> for Pos<$t> {
fn from(tuple: ($t, $t)) -> Self {
Self(
tuple.0 as $t,
tuple.1 as $t
)
}
}
impl Default for Pos<$t> {
fn default() -> Self {
Self(0 as $t, 0 as $t)
}
}
impl Into<($t, $t)> for Pos<$t> {
fn into(self) -> ($t, $t) {
(
self.0 as $t,
self.1 as $t
)
}
}
impl Pos<$t> {
pub fn next_pos_turn(&self, t: Angle, a: Angle, d: Distance) -> Pos<$t> {
let (x0, y0) = (self.0, self.1);
let direction = (a.radian() + t.radian());
let x1 = x0 + (d * direction.cos()) as $t;
let y1 = y0 + (d * direction.sin()) as $t;
Self(x1, y1)
}
}
};
}
pos_impl!(i16);
pos_impl!(i32);
pos_impl!(i64);
pos_impl!(i128);
pos_impl!(f64);