aboutsummaryrefslogtreecommitdiff
path: root/src/coord/translate.rs
blob: 32888be035d8ec12ef588f936f835aa943790b79 (plain)
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
use plotters_backend::BackendCoord;
use std::ops::Deref;

/// The trait that translates some customized object to the backend coordinate
pub trait CoordTranslate {
    type From;

    /// Translate the guest coordinate to the guest coordinate
    fn translate(&self, from: &Self::From) -> BackendCoord;
}

impl<C, T> CoordTranslate for T
where
    C: CoordTranslate,
    T: Deref<Target = C>,
{
    type From = C::From;
    fn translate(&self, from: &Self::From) -> BackendCoord {
        self.deref().translate(from)
    }
}

/// The trait indicates that the coordinate system supports reverse transform
/// This is useful when we need an interactive plot, thus we need to map the event
/// from the backend coordinate to the logical coordinate
pub trait ReverseCoordTranslate: CoordTranslate {
    /// Reverse translate the coordinate from the drawing coordinate to the
    /// logic coordinate.
    /// Note: the return value is an option, because it's possible that the drawing
    /// coordinate isn't able to be represented in te guest coordinate system
    fn reverse_translate(&self, input: BackendCoord) -> Option<Self::From>;
}