ed_journals/modules/galaxy/models/
galactic_distance.rs

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
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;

/// A distance in light-years between two objects in the galaxy, usually between two systems.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct GalacticDistance(f32);

impl GalacticDistance {
    pub fn from_ly(ly: f32) -> Self {
        GalacticDistance(ly)
    }

    pub fn as_ly(&self) -> f32 {
        self.0
    }

    pub fn between(a: [f32; 3], b: [f32; 3]) -> Self {
        let x = a[0] - b[0];
        let y = a[1] - b[1];
        let z = a[2] - b[2];

        GalacticDistance((x * x + y * y + z * z).sqrt())
    }
}

impl PartialOrd for GalacticDistance {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.0.partial_cmp(&other.0)
    }
}