Skip to main content

blockworx/document_ng/
linear_distance.rs

1const UNIT_SCALE: f64 = 16_777_216.0;
2
3#[derive(Clone, Copy, PartialEq, Debug, PartialOrd, Ord, Eq)]
4pub struct LinearDistance(i64);
5
6impl From<f32> for LinearDistance {
7    fn from(value: f32) -> Self {
8        LinearDistance((value * UNIT_SCALE as f32) as i64)
9    }
10}
11
12impl From<f64> for LinearDistance {
13    fn from(value: f64) -> Self {
14        LinearDistance((value * UNIT_SCALE) as i64)
15    }
16}
17
18impl From<LinearDistance> for f32 {
19    fn from(value: LinearDistance) -> Self {
20        value.0 as f32 / UNIT_SCALE as f32
21    }
22}
23
24impl std::ops::Add<LinearDistance> for LinearDistance {
25    type Output = LinearDistance;
26
27    fn add(self, rhs: LinearDistance) -> Self::Output {
28        LinearDistance(self.0 + rhs.0)
29    }
30}
31
32impl std::ops::AddAssign<f32> for LinearDistance {
33    fn add_assign(&mut self, rhs: f32) {
34        *self = LinearDistance::from(f32::from(*self) + rhs);
35    }
36}
37
38impl std::ops::Mul<f32> for LinearDistance {
39    type Output = LinearDistance;
40
41    fn mul(self, rhs: f32) -> Self::Output {
42        LinearDistance::from(f32::from(self) * rhs)
43    }
44}
45
46impl std::ops::Mul<f64> for LinearDistance {
47    type Output = LinearDistance;
48
49    fn mul(self, rhs: f64) -> Self::Output {
50        LinearDistance::from(f32::from(self) * rhs as f32)
51    }
52}
53
54impl std::ops::Sub<LinearDistance> for LinearDistance {
55    type Output = LinearDistance;
56
57    fn sub(self, rhs: LinearDistance) -> Self::Output {
58        LinearDistance(self.0 - rhs.0)
59    }
60}