Refactor curve to builder pattern

This commit is contained in:
Michał Chodzikiewicz 2021-02-26 01:03:14 +01:00
parent a555d74a25
commit 5139122a95
5 changed files with 67 additions and 58 deletions

View file

@ -1,10 +1,16 @@
use core::ops::{Range};
use crate::range_conv::Scalable;
use crate::drawable_curve::DrawableCurve;
use embedded_graphics::prelude::*;
use itertools::{Itertools, MinMaxResult::MinMax, MinMaxResult};
use embedded_graphics::drawable::{Drawable};
use embedded_graphics::DrawTarget;
use embedded_graphics::geometry::Point;
use embedded_graphics::pixelcolor::{PixelColor};
use embedded_graphics::primitives::{Line, Primitive};
use embedded_graphics::style::PrimitiveStyle;
pub struct PlotPoint {
pub x: i32,
pub y: i32,
@ -43,8 +49,6 @@ impl<'a> Curve<'a> {
pub fn into_drawable_curve<C>(&self,
top_left: &'a Point,
bottom_right: &'a Point,
color: C,
thickness: usize,
) -> DrawableCurve<C, impl Iterator<Item=Point> + '_>
where C: PixelColor
{
@ -64,10 +68,61 @@ impl<'a> Curve<'a> {
&Range { start: bottom_right.y, end: top_left.y },
),
});
DrawableCurve::new(it, color,thickness)
DrawableCurve {
scaled_data: it,
color: None,
thickness: None,
}
}
}
pub struct DrawableCurve<C, I>
{
scaled_data: I,
color: Option<C>,
thickness: Option<usize>,
}
impl<C, I> DrawableCurve<C, I>
where
C: PixelColor,
I: Iterator<Item=Point>,
{
pub fn set_color(mut self, color: C) -> DrawableCurve<C, I> {
self.color = Some(color);
self
}
pub fn set_thickness(mut self, thickness: usize) -> DrawableCurve<C,I> {
self.thickness = Some(thickness);
self
}
}
impl<C, I> Drawable<C> for DrawableCurve<C, I>
where C: PixelColor + Default,
I: Iterator<Item=Point>,
{
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
let color = match self.color {
None => C::default(),
Some(c) => c,
};
let thickness = match self.thickness {
None => 2,
Some(t) => t,
};
let style = PrimitiveStyle::with_stroke(color, thickness as u32);
let mut iter = self.scaled_data.into_iter();
let mut prev = iter.next().unwrap();
for point in iter {
Line::new(prev, point)
.into_styled(style)
.draw(display)?;
prev = point;
}
Ok(())
}
}
#[cfg(test)]
mod tests {

View file

@ -1,45 +0,0 @@
use embedded_graphics::drawable::{Drawable};
use embedded_graphics::DrawTarget;
use embedded_graphics::geometry::Point;
use embedded_graphics::pixelcolor::{PixelColor};
use embedded_graphics::primitives::{Line, Primitive};
use embedded_graphics::style::PrimitiveStyle;
pub struct DrawableCurve<C, I>
{
scaled_data: I,
color: C,
thickness: usize,
}
impl<C, I> DrawableCurve<C, I>
where
C: PixelColor,
I: Iterator<Item=Point>,
{
pub(in crate) fn new(data: I, color: C, thickness: usize) -> DrawableCurve<C, I> {
DrawableCurve {
scaled_data: data,
color,
thickness,
}
}
}
impl<C, I> Drawable<C> for DrawableCurve<C, I>
where C: PixelColor,
I: Iterator<Item=Point>,
{
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
let style = PrimitiveStyle::with_stroke(self.color, self.thickness as u32);
let mut iter = self.scaled_data.into_iter();
let mut prev = iter.next().unwrap();
for point in iter {
Line::new(prev, point)
.into_styled(style)
.draw(display)?;
prev = point;
}
Ok(())
}
}

View file

@ -5,6 +5,5 @@ pub mod axis;
pub mod polyplot;
pub mod single_plot;
mod drawable_curve;
mod range_conv;
mod drawable_axis;

View file

@ -22,16 +22,16 @@ impl<'a, C> PolyPlot<'a, C>
impl<'a, C> Drawable<C> for PolyPlot<'a, C>
where
C: PixelColor
C: PixelColor + Default,
{
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
for (curve, color) in self.curves {
curve.into_drawable_curve(
&self.top_left,
&self.bottom_right,
*color,
2
).draw(display)?;
).set_color(*color)
.set_thickness(2)
.draw(display)?;
}
Ok(())
}

View file

@ -111,9 +111,9 @@ impl<'a, C> Drawable<C> for DrawableSinglePlot<'a, C>
self.plot.curve.into_drawable_curve(
&self.top_left,
&self.bottom_right,
color,
thickness,
).draw(display)?;
).set_color(color)
.set_thickness(thickness)
.draw(display)?;
Ok(())
}
}