mirror of
https://gitlab.com/feliix42/embedded-plots.git
synced 2024-11-22 18:06:30 +00:00
Refactor curve to builder pattern
This commit is contained in:
parent
a555d74a25
commit
5139122a95
5 changed files with 67 additions and 58 deletions
65
src/curve.rs
65
src/curve.rs
|
@ -1,10 +1,16 @@
|
||||||
use core::ops::{Range};
|
use core::ops::{Range};
|
||||||
|
|
||||||
use crate::range_conv::Scalable;
|
use crate::range_conv::Scalable;
|
||||||
use crate::drawable_curve::DrawableCurve;
|
|
||||||
use embedded_graphics::prelude::*;
|
|
||||||
use itertools::{Itertools, MinMaxResult::MinMax, MinMaxResult};
|
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 struct PlotPoint {
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32,
|
pub y: i32,
|
||||||
|
@ -43,8 +49,6 @@ impl<'a> Curve<'a> {
|
||||||
pub fn into_drawable_curve<C>(&self,
|
pub fn into_drawable_curve<C>(&self,
|
||||||
top_left: &'a Point,
|
top_left: &'a Point,
|
||||||
bottom_right: &'a Point,
|
bottom_right: &'a Point,
|
||||||
color: C,
|
|
||||||
thickness: usize,
|
|
||||||
) -> DrawableCurve<C, impl Iterator<Item=Point> + '_>
|
) -> DrawableCurve<C, impl Iterator<Item=Point> + '_>
|
||||||
where C: PixelColor
|
where C: PixelColor
|
||||||
{
|
{
|
||||||
|
@ -64,10 +68,61 @@ impl<'a> Curve<'a> {
|
||||||
&Range { start: bottom_right.y, end: top_left.y },
|
&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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
|
@ -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(())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,6 +5,5 @@ pub mod axis;
|
||||||
pub mod polyplot;
|
pub mod polyplot;
|
||||||
pub mod single_plot;
|
pub mod single_plot;
|
||||||
|
|
||||||
mod drawable_curve;
|
|
||||||
mod range_conv;
|
mod range_conv;
|
||||||
mod drawable_axis;
|
mod drawable_axis;
|
||||||
|
|
|
@ -22,16 +22,16 @@ impl<'a, C> PolyPlot<'a, C>
|
||||||
|
|
||||||
impl<'a, C> Drawable<C> for PolyPlot<'a, C>
|
impl<'a, C> Drawable<C> for PolyPlot<'a, C>
|
||||||
where
|
where
|
||||||
C: PixelColor
|
C: PixelColor + Default,
|
||||||
{
|
{
|
||||||
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
|
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
|
||||||
for (curve, color) in self.curves {
|
for (curve, color) in self.curves {
|
||||||
curve.into_drawable_curve(
|
curve.into_drawable_curve(
|
||||||
&self.top_left,
|
&self.top_left,
|
||||||
&self.bottom_right,
|
&self.bottom_right,
|
||||||
*color,
|
).set_color(*color)
|
||||||
2
|
.set_thickness(2)
|
||||||
).draw(display)?;
|
.draw(display)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,9 +111,9 @@ impl<'a, C> Drawable<C> for DrawableSinglePlot<'a, C>
|
||||||
self.plot.curve.into_drawable_curve(
|
self.plot.curve.into_drawable_curve(
|
||||||
&self.top_left,
|
&self.top_left,
|
||||||
&self.bottom_right,
|
&self.bottom_right,
|
||||||
color,
|
).set_color(color)
|
||||||
thickness,
|
.set_thickness(thickness)
|
||||||
).draw(display)?;
|
.draw(display)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue