egui_charts/drawings/domain/
coordinates.rs1#[derive(Clone, Copy)]
22pub struct ChartPoint {
23 pub bar_idx: f32,
25 pub price: f64,
27 pub(crate) time_anchor: Option<(chrono::DateTime<chrono::Utc>, f32)>,
29}
30
31impl ChartPoint {
32 #[inline]
34 pub fn new(bar_idx: f32, price: f64) -> Self {
35 Self {
36 bar_idx,
37 price,
38 time_anchor: None,
39 }
40 }
41
42 #[inline]
44 pub fn zero() -> Self {
45 Self {
46 bar_idx: 0.0,
47 price: 0.0,
48 time_anchor: None,
49 }
50 }
51
52 #[inline]
54 pub fn shift_bar_idx(&mut self, delta: f32) {
55 self.bar_idx += delta;
56 }
57
58 #[inline]
60 pub fn with_shifted_bar_idx(self, delta: f32) -> Self {
61 Self {
62 bar_idx: self.bar_idx + delta,
63 price: self.price,
64 time_anchor: None,
65 }
66 }
67
68 pub fn lerp(a: ChartPoint, b: ChartPoint, t: f32) -> ChartPoint {
70 ChartPoint {
71 bar_idx: a.bar_idx + (b.bar_idx - a.bar_idx) * t,
72 price: a.price + (b.price - a.price) * t as f64,
73 time_anchor: None,
74 }
75 }
76}
77
78impl std::fmt::Debug for ChartPoint {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 f.debug_struct("ChartPoint")
81 .field("bar_idx", &self.bar_idx)
82 .field("price", &self.price)
83 .finish()
84 }
85}
86
87impl PartialEq for ChartPoint {
88 fn eq(&self, other: &Self) -> bool {
89 self.bar_idx == other.bar_idx && self.price == other.price
90 }
91}
92
93impl Default for ChartPoint {
94 fn default() -> Self {
95 Self::zero()
96 }
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn test_lerp() {
105 let a = ChartPoint::new(0.0, 100.0);
106 let b = ChartPoint::new(10.0, 200.0);
107 let mid = ChartPoint::lerp(a, b, 0.5);
108 assert_eq!(mid.bar_idx, 5.0);
109 assert_eq!(mid.price, 150.0);
110 }
111}