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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
#[allow(dead_code)] use std::ops; /// # Coord #[derive(Clone, Copy, Debug)] pub struct Coord{ pub x: f64, pub y: f64, } /// ## creating a new Coord /// #### Example /// ```rust /// use coord2d::*; /// fn main () { /// let a: coords::Coord = new_coord!(1, 2.0); /// println!("{:?}", a); /// // returns Coord {x: 1.0, y: 2.0} /// let a: coords::Coord = new_coord!(2.0); /// println!("{:?}", a); /// // returns Coord {x: 0.0, y: 2.0} /// } /// ``` /// * the var **a** contains to var x and y as position in a graph (O, I, J) /// * it convert i32 ,i64, u32 ,u64 , usize ,isize and f32 to f64 #[macro_export] macro_rules! new_coord { () => { $crate::coords::Coord {x: 0.0, y: 0.0} }; ($x:expr, $y:expr) => { $crate::coords::Coord { x: $x as f64, y: $y as f64} }; ($y:expr) => { $crate::coords::Coord { x: 0.0 , y: $y as f64} }; } /// ## implementations impl Coord { /// ### converting to a Vec /// #### Example /// ```rust /// use coord2d::*; /// fn main () { /// let a: coords::Coord = new_coord!(1, 2.0); /// let ve: Vec<f64> = a.to_vec(); /// println!("{:?}", ve); /// } /// ``` pub fn to_vec(self: Coord ) -> Vec<f64> { return vec![self.x, self.y]; } /// ### converting to a tuple /// #### Example /// ```rust /// use coord2d::*; /// fn main () { /// let a: coords::Coord = new_coord!(1, 2.0); /// let tu: (f64, f64) = a.to_tuple(); /// println!("{:?}", tu); /// } /// ``` pub fn to_tuple(self :Coord) -> (f64, f64) { return (self.x, self.y); } /// ### spliting Coord /// #### Example /// ```rust /// use coord2d::*; /// fn main () { /// let a: coords::Coord = new_coord!(1, 2.0); /// let (x, y) = a.split(); /// println!("{}, {}", x , y); /// } /// ``` pub fn split(self: Coord) -> (f64, f64) { return self.to_tuple(); } } /// ## math /// ### addition /// ##### Example /// ```rust /// use coord2d::*; /// fn main () { /// let a: coords::Coord = new_coord!(1, 2.0); /// let b: coords::Coord = new_coord!(0.4, 3.3); /// let sum:coords::Coord = a + b; /// println!("{:?}", sum); /// } /// ``` impl ops::Add<Coord> for Coord{ type Output = Coord; fn add(self: Coord, rhs: Coord) -> Coord { let x = self.x + rhs.x; let y = self.y + rhs.y; return Coord{x, y}; } } /// ### subtraction /// ##### Example /// ```rust /// use coord2d::*; /// fn main () { /// let a: coords::Coord = new_coord!(1, 2.0); /// let b: coords::Coord = new_coord!(0.4, 3.3); /// let sub:coords::Coord = a - b; /// println!("{:?}", sub); /// } /// ``` impl ops::Sub<Coord> for Coord{ type Output = Coord; fn sub(self: Coord, rhs: Coord) -> Coord { let x = self.x - rhs.x; let y = self.y - rhs.y; return Coord{x, y}; } } /// ### multiplication /// ##### Example /// ```rust /// use coord2d::*; /// fn main () { /// let a: coords::Coord = new_coord!(1, 2.0); /// let b: coords::Coord = new_coord!(0.4, 3.3); /// let mul:coords::Coord = a * b; /// println!("{:?}", mul); /// } /// ``` impl ops::Mul<Coord> for Coord{ type Output = Coord; fn mul(self: Coord, rhs: Coord) -> Coord { let x = self.x * rhs.x; let y = self.y * rhs.y; return Coord{x, y}; } } /// ### division /// ##### Example /// ```rust /// use coord2d::*; /// fn main () { /// let a: coords::Coord = new_coord!(1, 2.0); /// let b: coords::Coord = new_coord!(0.4, 3.3); /// let div:coords::Coord = a / b; /// println!("{:?}", div); /// } /// ``` impl ops::Div<Coord> for Coord{ type Output = Coord; fn div(self: Coord, rhs: Coord) -> Coord { let x = self.x / rhs.x; let y = self.y / rhs.y; return Coord{x, y}; } }