si_vectors/vector/
mod.rs

1
2use core::fmt;
3use std::{ops::{Mul, Div, Add, Rem}, f64::consts::PI};
4use crate::Value;
5use crate::Cartesian;
6
7
8#[derive(Debug)]
9/// A struct that holds a Value struct and direction
10pub struct Vector {
11    ///Value Struct
12    pub value:Value,
13    ///Angle of Vector in radians
14    pub theta: f64
15}
16
17impl  Mul<Vector> for Vector{
18    type Output = Self;
19    fn mul(self, rhs: Vector) -> Self::Output {
20        Self{value: self.value*rhs.value,theta: (self.theta+rhs.theta).rem(2_f64*PI)}
21    }
22}
23
24impl Mul<Value> for Vector {
25    type Output = Vector;
26    fn mul(self, rhs: Value) -> Self::Output {
27        Self::Output{value: self.value * rhs, ..self}
28    }
29}
30
31impl Mul<Vector> for Value {
32    type Output = Vector;
33    fn mul(self, rhs: Vector) -> Self::Output {
34        Self::Output{value: self * rhs.value, ..rhs}
35    }
36}
37
38impl Div<Vector> for Vector{
39    type Output = Self;
40    fn div(self, rhs: Vector) -> Self::Output {
41        Self{value: self.value*rhs.value,theta: (self.theta-rhs.theta).rem(2_f64*PI)}
42    }
43}
44
45impl Div<Value> for Vector{
46    type Output = Self;
47    fn div(self, rhs: Value) -> Self::Output {
48        Self{value: self.value/rhs,..self}
49    }
50}
51
52impl Div<Vector> for Value{
53    type Output = Vector;
54    fn div(self, rhs: Vector) -> Self::Output {
55        Self::Output{value: self/rhs.value,..rhs}
56    }
57}
58
59
60impl Add<Vector> for Vector{
61    type Output = Vector;
62    fn add(self, rhs: Vector) -> Self::Output {
63        let cs:Cartesian = self.into();
64        let cr:Cartesian = rhs.into();
65        let f:Self::Output = (cs+cr).into();
66        f
67    }
68}
69impl From<Cartesian> for Vector{
70    fn from(value: Cartesian) -> Self {
71        Self{value:Value{
72            magnitude: (value.x.powi(2) + value.y.powi(2)).sqrt(),
73            si_units_num: value.num,si_units_den: value.den },
74            theta: Vector::get_angle(value.x, value.y) 
75        }
76    }
77}
78
79impl Clone for Vector{
80    fn clone(&self) -> Self {
81        Self{value:self.value.clone(),theta: self.theta}
82    }
83}
84
85impl fmt::Display for Vector{
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        write!(f,"{},  {} radians",self.value,self.theta)
88    }
89}
90
91impl Vector {
92    ///Returns angle from x,y coordinates
93    pub fn get_angle(x:f64,y:f64) -> f64{
94        if x.is_sign_positive() && y.is_sign_positive() {
95            (y/x).atan()
96        }else if x.is_sign_positive() && y.is_sign_negative() {
97            (y/x).atan() + 2_f64*PI
98        } else if x.is_sign_negative() && y.is_sign_positive() {
99            (y/x).atan() + PI
100        }else{
101            (y/x).atan() + PI
102        }
103    }
104}