si_vectors/cartesian/
mod.rs

1
2use crate::{SiUnit,Vector};
3use core::ops::Add;
4#[derive(Debug)]
5///Cartesian coordinates struct
6pub struct Cartesian{
7    ///x coordinates
8    pub x:f64,
9    ///y coordinates
10    pub y:f64,
11    ///Numerator Vector of SiUnits
12    pub num: Vec<SiUnit>,
13    ///Denominator Vector of SiUnits
14    pub den: Vec<SiUnit>
15}
16///Transform Vector to Cartesian
17/// ```rust
18/// let v = Vector{
19///     value:Value{
20///         magnitude:1_f64,
21///         si_units_num: Vec::from([SiUnit::Kilogram,SiUnit::Metres])
22///         si_units_den: Vec::from([SiUnit::Seconds,SiUnit::Seconds])
23///     }
24///     theta:crate::PI
25/// };
26/// let cart:Cartesian = v.into()
27///println!("{}",cart);
28/// ```
29impl From<Vector> for Cartesian{
30    fn from(other: Vector) -> Self {
31        Self{x:other.value.magnitude*other.theta.cos(),
32            y:other.value.magnitude*other.theta.sin(),
33            num:other.value.si_units_num,
34            den:other.value.si_units_den}
35    }
36}
37
38
39/// ```rust
40/// let c1 = Cartesian{x: 1,y: 0,num: Vec::<SiUnit>::from([]),den: Vec::<SiUnit>::from([])}
41/// let c2 = Cartesian{x: 1,y: 0,num: Vec::<SiUnit>::from([]),den: Vec::<SiUnit>::from([])}
42/// println!("{}",c1+c2);
43impl Add<Cartesian> for Cartesian {
44    type Output = Cartesian;
45    fn add(self, rhs: Cartesian) -> Self::Output {
46        if self.num.len() == rhs.num.len(){
47            for i in 0..self.num.len(){
48                if !(self.num[i] == rhs.num[i]){
49                    println!("Paniccc");
50                    panic!("Units not identical");
51                }
52            }
53        }else {
54            panic!("Units not identical");
55        }
56        if self.den.len() == rhs.den.len() {
57            for i in 0..self.den.len(){
58                if !(self.den[i] == rhs.den[i]){
59                    panic!("Units not identical");
60                }
61            }
62        }
63        Self{x: self.x + rhs.x,y: self.y + rhs.y,..self}
64    }
65}
66impl Clone for Cartesian{
67    fn clone(&self) -> Self {
68        Self{x: self.x,y:self.y,num:self.num.clone(),den:self.den.clone()}
69    }
70}