Skip to content

Commit

Permalink
Optional End X
Browse files Browse the repository at this point in the history
  • Loading branch information
elftausend committed Feb 24, 2024
1 parent 2780915 commit 1f136bd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ pub struct XEnd(pub f64);
pub struct YEnd(f64, f64);

/// sets the absolute max value for x
pub fn x(end_x: f64) -> XEnd {
XEnd(end_x.abs())
pub fn x(end_x: f64) -> Option<XEnd> {
Some(XEnd(end_x.abs()))
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Desc {
pub end: XEnd,
pub end_x: Option<XEnd>,
pub spacing_x: f32, // pixels
pub spacing_y: f32, // pixels
pub min_steps_x: f32,
Expand All @@ -59,7 +59,7 @@ pub struct Desc {
impl Default for Desc {
fn default() -> Self {
Self {
end: x(1.),
end_x: None,
spacing_x: 40.,
spacing_y: 40.,
min_steps_x: 4.,
Expand Down
8 changes: 4 additions & 4 deletions src/plots/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,13 @@ impl<F: Fn(f64) -> f64> PlotArg for F {
}
}

impl<F: Fn(f64) -> f64> PlotArg for (F, XEnd) {
impl<F: Fn(f64) -> f64> PlotArg for (F, Option<XEnd>) {
fn as_plot(&self) -> Plot {
let mut xs = vec![0.; 200];

let mut add = -100f64;
for x in &mut xs {
*x = (add / 100.) * self.1 .0;
*x = (add / 100.) * self.1.unwrap().0;
add += 1.;
}

Expand All @@ -372,15 +372,15 @@ impl<F: Fn(f64) -> f64> PlotArg for (F, XEnd) {
}
}

impl<F: Copy + Fn(f64) -> f64> PlotArg for (F, XEnd, &str) {
impl<F: Copy + Fn(f64) -> f64> PlotArg for (F, Option<XEnd>, &str) {
fn as_plot(&self) -> Plot {
let mut plot = (self.0, self.1).as_plot();
plot.line_desc = vec![self.2.into()];
plot
}
}

impl<F: Copy + Fn(f64) -> f64> PlotArg for (F, &str, XEnd) {
impl<F: Copy + Fn(f64) -> f64> PlotArg for (F, &str, Option<XEnd>) {
fn as_plot(&self) -> Plot {
let mut plot = (self.0, self.2).as_plot();
plot.line_desc = vec![self.1.into()];
Expand Down
6 changes: 3 additions & 3 deletions src/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ impl PlotArg for Polynomial {
}
}

impl PlotArg for (Polynomial, XEnd) {
impl PlotArg for (Polynomial, Option<XEnd>) {
fn as_plot(&self) -> crate::Plot {
let mut xs = [0.; 200];

let mut add = -100f64;
for x in &mut xs {
*x = (add / 100.) * self.1 .0;
*x = (add / 100.) * self.1.unwrap().0;
add += 1.;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ impl PlotArg for (Polynomial, &str) {
}
}

impl PlotArg for (Polynomial, XEnd, &str) {
impl PlotArg for (Polynomial, Option<XEnd>, &str) {
fn as_plot(&self) -> Plot {
let mut plot = (self.0.clone(), self.1).as_plot();
plot.line_desc = vec![self.2.into()];
Expand Down
9 changes: 5 additions & 4 deletions src/render/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ pub async fn run(plot: Plot, min_y: f64, other_scaling: bool) {
let spacing_x = plot.desc.spacing_x;
let spacing_y = plot.desc.spacing_y;

let mut max_x = max_matrix(&plot.xs);
//let mut max_x = max(&maxed);

max_x = max_display(max_x, other_scaling);
let max_x = match plot.desc.end_x {
Some(x) => x.0,
None => max_display(max_matrix(&plot.xs), other_scaling),
};

//let mut max_x = max(&maxed);
let x_font_size = get_font_size_x(max_x);

let steps = get_steps(max_x, plot.desc.min_steps_x.into());
Expand Down
4 changes: 2 additions & 2 deletions tests/desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use graplot::{x, Desc, Plot, XEnd};
#[test]
fn test_desc() {
let desc = Desc {
end: x(2.),
end_x: x(2.),
..Default::default()
};
assert_eq!(
Desc {
end: XEnd(2.),
end_x: Some(XEnd(2.)),
spacing_x: 40.,
spacing_y: 40.,
min_steps_x: 4.,
Expand Down

0 comments on commit 1f136bd

Please sign in to comment.