1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
//!	A library that provides a way to easily extract 1d ranges from a 2d container based off of the x or y axis statically through
//! type parameters. This can help with performnace in algorithms where you need to get values for a particular axis often.

#![no_std]

mod range;
mod rect;
mod vec2;
mod ray;

pub use self::ray::Ray;
pub use self::ray::CastResult;
pub use self::ray::ray;

pub use self::range::Range;
pub use self::rect::Rect;
pub use self::rect::rect;
pub use self::vec2::arr2_as;
pub use self::vec2::vec2;
pub use self::vec2::vec2same;
pub use self::vec2::Vec2;

///The x axis implementation of the Axis
#[derive(Copy, Clone)]
pub struct XAXIS;
impl Axis for XAXIS {
    type Next = YAXIS;
    #[inline(always)]
    fn is_xaxis(&self) -> bool {
        true
    }
    #[inline(always)]
    fn next(&self) -> Self::Next {
        YAXIS
    }
}

///The y axis implementation of the Axis
#[derive(Copy, Clone)]
pub struct YAXIS;
impl Axis for YAXIS {
    type Next = XAXIS;

    #[inline(always)]
    fn is_xaxis(&self) -> bool {
        false
    }
    #[inline(always)]
    fn next(&self) -> Self::Next {
        XAXIS
    }
}

///Axis trait can be used to extract the x or y portions of a container.
///when you know the axis as compile time.
///The X implementation of this trait's Next associated trait is the Y implementation.
///The Y implementation of this trait's Next associated trait is the X implementation.
pub trait Axis: Sync + Send + Copy + Clone {
    type Next: Axis;
    fn is_xaxis(&self) -> bool;
    fn next(&self) -> Self::Next;

    #[inline(always)]
    fn is_equal_to<B: Axis>(&self, other: B) -> bool {
        if self.is_xaxis() && other.is_xaxis() {
            return true;
        }
        if !self.is_xaxis() && !other.is_xaxis() {
            return true;
        }
        false
    }
}


///Represents a rectangle with the specified aspect ratio
///and the specified width. The height of the rectangle
///can be inferred by the aspect ratio.
#[derive(Copy,Clone,Debug)]
pub struct Vec2AspectRatio{
    pub ratio:AspectRatio,
    pub width:f64
}
impl Vec2AspectRatio{
    pub fn vec(&self)->Vec2<f64>{
        let height = self.ratio.height_over_width()*self.width;
        vec2(self.width,height)
    }
}

///An aspect ratio represented as a fraction
///so that there is no precision loss.
#[derive(Copy,Clone,Debug)]
pub struct AspectRatio(pub Vec2<f64>);

impl AspectRatio{
    pub fn width_over_height(&self)->f64{
        let v=self.0;
        v.x as f64/v.y as f64
    }

    pub fn height_over_width(&self)->f64{
        let v=self.0;
        v.y as f64/v.x as f64
    }
       
}