WIP: Plot

This commit is contained in:
Michał Chodzikiewicz 2020-12-20 12:11:06 +01:00
parent 8523ca8215
commit 3f8971b921
5 changed files with 66 additions and 14 deletions

View file

@ -9,7 +9,7 @@ use embedded_graphics_simulator::{
OutputSettingsBuilder OutputSettingsBuilder
}; };
use embedded_plots::curve::{PlotPoint, CurvePoints}; use embedded_plots::curve::{PlotPoint, Curve};
fn main() -> Result<(), core::convert::Infallible> { fn main() -> Result<(), core::convert::Infallible> {
let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(480, 272)); let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(480, 272));
@ -20,7 +20,7 @@ fn main() -> Result<(), core::convert::Infallible> {
PlotPoint{x: 2,y: 1}, PlotPoint{x: 2,y: 1},
PlotPoint{x: 3,y: 0}, PlotPoint{x: 3,y: 0},
]; ];
CurvePoints::new(data.as_slice()) Curve::new(data.as_slice())
.into_drawable_curve(&(0..3),&(0..1),&Point{x: 20, y: 20}, &Point{x:450,y:250},RgbColor::WHITE) .into_drawable_curve(&(0..3),&(0..1),&Point{x: 20, y: 20}, &Point{x:450,y:250},RgbColor::WHITE)
.draw(&mut display)?; .draw(&mut display)?;

View file

@ -9,16 +9,22 @@ pub struct PlotPoint {
pub y: i32, pub y: i32,
} }
pub struct CurvePoints<'a>{ pub struct Curve<'a>{
points: &'a [PlotPoint], points: &'a [PlotPoint],
} }
impl<'a> CurvePoints<'a> { impl<'a> Curve<'a> {
pub fn new(points: &'a [PlotPoint]) -> CurvePoints { pub fn new(points: &'a [PlotPoint]) -> Curve {
CurvePoints{points} Curve {points}
} }
pub fn into_drawable_curve<C>(self, x_range: &'a Range<i32>, y_range: &'a Range<i32>, top_left : &'a Point, bottom_right: &'a Point, color: C) -> DrawableCurve<C,impl Iterator<Item=Point> + 'a> pub fn into_drawable_curve<C>(self,
x_range: &'a Range<i32>,
y_range: &'a Range<i32>,
top_left : &'a Point,
bottom_right: &'a Point,
color: C
) -> DrawableCurve<C,impl Iterator<Item=Point> + 'a>
where C: PixelColor where C: PixelColor
{ {
assert!(top_left.x < bottom_right.x); assert!(top_left.x < bottom_right.x);
@ -28,8 +34,14 @@ impl<'a> CurvePoints<'a> {
let it = self.points.iter() let it = self.points.iter()
.map(move |p| Point{ .map(move |p| Point{
x: p.x.scale_between_ranges(x_range,&Range{start: top_left.x, end: bottom_right.x}), x: p.x.scale_between_ranges(
y: p.y.scale_between_ranges(y_range,&Range{start: bottom_right.y, end: top_left.y}), x_range,
&Range{start: top_left.x, end: bottom_right.x}
),
y: p.y.scale_between_ranges(
y_range,
&Range{start: bottom_right.y, end: top_left.y}
),
}); });
DrawableCurve::new(it,color) DrawableCurve::new(it,color)
} }

View file

@ -1,4 +1,4 @@
use embedded_graphics::drawable::{Drawable, Pixel}; use embedded_graphics::drawable::{Drawable};
use embedded_graphics::DrawTarget; use embedded_graphics::DrawTarget;
use embedded_graphics::geometry::Point; use embedded_graphics::geometry::Point;
use embedded_graphics::pixelcolor::{PixelColor}; use embedded_graphics::pixelcolor::{PixelColor};
@ -13,7 +13,7 @@ pub struct DrawableCurve<C, I>
color: C, color: C,
} }
impl<'a, C,I> DrawableCurve<C,I> impl<C,I> DrawableCurve<C,I>
where where
C: PixelColor, C: PixelColor,
I: Iterator<Item=Point>, I: Iterator<Item=Point>,
@ -25,7 +25,7 @@ impl<'a, C,I> DrawableCurve<C,I>
} }
} }
} }
impl<'a, C,I> Drawable<C> for DrawableCurve<C,I> impl<C,I> Drawable<C> for DrawableCurve<C,I>
where C: PixelColor, where C: PixelColor,
I: Iterator<Item=Point>, I: Iterator<Item=Point>,
{ {
@ -34,7 +34,9 @@ impl<'a, C,I> Drawable<C> for DrawableCurve<C,I>
let mut iter = self.scaled_data.into_iter(); let mut iter = self.scaled_data.into_iter();
let mut prev = iter.next().unwrap(); let mut prev = iter.next().unwrap();
for point in iter { for point in iter {
Line::new(prev,point).into_styled(style).draw(display)?; Line::new(prev,point)
.into_styled(style)
.draw(display)?;
prev = point; prev = point;
} }
Ok(()) Ok(())

View file

@ -1,5 +1,6 @@
#![no_std] #![no_std]
pub mod curve; pub mod curve;
pub mod drawable_curve; pub mod plot;
mod drawable_curve;
mod range_conv; mod range_conv;

37
src/plot.rs Normal file
View file

@ -0,0 +1,37 @@
use crate::curve::Curve;
use embedded_graphics::drawable::Drawable;
use embedded_graphics::DrawTarget;
use embedded_graphics::prelude::Point;
use embedded_graphics::pixelcolor::PixelColor;
struct Plot<'a, C>
where
C: PixelColor
{
curves: &'a [Curve<'a>],
color: C,
}
impl<'a, C> Plot<'a, C> {
fn new(curves: &'a [Curve<'a>], color : C) -> Plot<C> {
Plot{curves, color}
}
}
impl<C> Drawable<C> for Plot<'_, C>
where
C: PixelColor
{
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), <D as DrawTarget<_>>::Error> {
for curve in self.curves {
curve.into_drawable_curve(
&(0..50),
&(-10..10),
&Point{x: 10,y: 10},
&Point{x: 470, y: 270},
self.color
).draw(display)?;
}
Ok(())
}
}