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
use std::fmt::{Display, Formatter};

use rta_for_fps_lib::window::WindowEnd;
use rta_for_fps_lib::{
    curve::curve_types::CurveType, curve::Curve, window::Demand, window::Window,
};

pub struct TotalDemandCurve {
    steps: Vec<Window<Demand>>,
}

impl Display for TotalDemandCurve {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "x,y")?;
        let mut summed_demand = 0;

        for window in &self.steps {
            let window_start = window.start.as_unit();
            writeln!(f, "{x},{y}", x = window_start, y = summed_demand)?;

            match window.length() {
                WindowEnd::Finite(length) => {
                    let length = length.as_unit();
                    let window_end = window_start + length;
                    summed_demand += length;
                    writeln!(f, "{x},{y}", x = window_end, y = summed_demand)?;
                }
                WindowEnd::Infinite => {}
            }
        }
        Ok(())
    }
}

impl TotalDemandCurve {
    pub fn new<C: CurveType<WindowKind = Demand>>(curve: Curve<C>) -> Self {
        TotalDemandCurve {
            steps: curve.into_windows(),
        }
    }
}

pub struct CurveWindows<W> {
    windows: Vec<Window<W>>,
}

impl<W> Display for CurveWindows<W> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for window in self.windows.iter() {
            let length = match window.length() {
                WindowEnd::Finite(length) => length,
                WindowEnd::Infinite => continue,
            };
            writeln!(
                f,
                "\\fill ({start}.0, 0.0) rectangle ++({length}.0, 1.0);",
                start = window.start.as_unit(),
                length = length.as_unit()
            )?;
        }
        Ok(())
    }
}

impl<W> CurveWindows<W> {
    pub fn new<C: CurveType<WindowKind = W>>(curve: Curve<C>) -> Self {
        CurveWindows {
            windows: curve.into_windows(),
        }
    }
}