graplot/
lib.rs

1//!
2//! 'graplot' is an experimental plotting library written in Rust that is based on [macroquad].
3//! It creates a window displaying the graphs.
4//!
5//! # Example
6//! ```
7//! use graplot::Plot;
8//!
9//!
10//! let plot = Plot::new(|x: f64| x.powf(2.));
11//! plot.show();
12//!
13//! ```
14//!
15
16mod line_desc;
17mod plots;
18mod polynomial;
19mod render;
20mod eval;
21pub mod graph;
22
23pub use graph::Graph;
24pub use plots::*;
25pub use line_desc::*;
26pub use polynomial::*;
27pub use render::*;
28pub use eval::*;
29pub use litequad::color::colors::*;
30pub use litequad::color::Color;
31
32pub type Matrix = Vec<Vec<f64>>;
33
34#[derive(Default, Clone)]
35pub struct AxisDesc {
36    pub title: String,
37    pub x_label: String,
38    pub y_label: String,
39}
40
41#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
42pub struct XEnd(pub f64);
43pub struct YEnd(f64, f64);
44
45/// sets the absolute max value for x
46pub fn x(end_x: f64) -> XEnd {
47    XEnd(end_x.abs())
48}
49
50#[derive(Debug, PartialEq, Clone, Copy)]
51pub struct Desc {
52    pub end: XEnd,
53    pub spacing_x: f32, // pixels
54    pub spacing_y: f32, // pixels
55    pub min_steps_x: f32,
56    pub min_steps_y: f32,
57}
58
59impl Default for Desc {
60    fn default() -> Self {
61        Self {
62            end: x(1.),
63            spacing_x: 40.,
64            spacing_y: 40.,
65            min_steps_x: 4.,
66            min_steps_y: 4.,
67        }
68    }
69}
70
71pub(crate) trait ToF64 {
72    type Output;
73    fn to_f64(&self) -> Self::Output;
74}
75
76impl<T: ToF64<Output = f64>> ToF64 for [T] {
77    type Output = Vec<f64>;
78
79    fn to_f64(&self) -> Self::Output {
80        self.iter().map(|val| val.to_f64()).collect()
81    }
82}
83
84macro_rules! impl_tof64 {
85    ($($t:ty),*) => {
86        $(
87            impl ToF64 for $t {
88                type Output = f64;
89                #[inline]
90                fn to_f64(&self) -> f64 {
91                    *self as f64
92                }
93            }
94        )*
95    };
96}
97
98impl_tof64!(f32, f64, i8, i16, i32, i64, i128,
99    isize, u8, u16, u32, u64, u128, usize
100);