mirror of
https://gitlab.com/feliix42/embedded-plots.git
synced 2024-11-22 18:06:30 +00:00
Draft: Axis
This commit is contained in:
parent
54b3ba7db6
commit
1f0e729777
5 changed files with 83 additions and 41 deletions
82
src/axis.rs
Normal file
82
src/axis.rs
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
use embedded_graphics::primitives::Line;
|
||||||
|
use embedded_graphics::drawable::Drawable;
|
||||||
|
use embedded_graphics::DrawTarget;
|
||||||
|
use core::ops::Range;
|
||||||
|
use embedded_graphics::prelude::*;
|
||||||
|
use embedded_graphics::style::{PrimitiveStyle};
|
||||||
|
use core::borrow::Borrow;
|
||||||
|
|
||||||
|
enum Orientation {
|
||||||
|
X {
|
||||||
|
x1: i32,
|
||||||
|
x2: i32,
|
||||||
|
y: i32,
|
||||||
|
},
|
||||||
|
Y {
|
||||||
|
y1: i32,
|
||||||
|
y2: i32,
|
||||||
|
x: i32,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Scale {
|
||||||
|
Fixed {
|
||||||
|
interval: usize,
|
||||||
|
},
|
||||||
|
RangeFraction {
|
||||||
|
fraction: usize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Axis<C> {
|
||||||
|
orientation: Orientation,
|
||||||
|
range: Range<i32>,
|
||||||
|
scale: Scale,
|
||||||
|
color: C
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C> Axis<C>
|
||||||
|
where
|
||||||
|
C: PixelColor,
|
||||||
|
{
|
||||||
|
fn new(orientation: Orientation, range: Range<i32>, scale: Scale, color: C) -> Axis<C> {
|
||||||
|
Axis { orientation, range, scale, color }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl<C> Drawable<C> for Axis<C>
|
||||||
|
where
|
||||||
|
C: PixelColor,
|
||||||
|
{
|
||||||
|
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
|
||||||
|
let lines = match self.scale {
|
||||||
|
Scale::Fixed { interval } => {
|
||||||
|
self.range.step_by(interval)
|
||||||
|
}
|
||||||
|
Scale::RangeFraction { fraction } => {
|
||||||
|
let len = self.range.len();
|
||||||
|
self.range.step_by(len / fraction)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match self.orientation {
|
||||||
|
Orientation::X{x1,x2,y} => {
|
||||||
|
Line{start: Point{x: x1,y}, end: Point{x: x2,y}}
|
||||||
|
.into_styled(PrimitiveStyle::with_stroke(self.color,1))
|
||||||
|
.draw(display)?;
|
||||||
|
for line in lines {
|
||||||
|
line.abs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Orientation::Y{y1,y2,x} => {
|
||||||
|
Line{start: Point{x,y: y1}, end: Point{x,y: y2}}
|
||||||
|
.into_styled(PrimitiveStyle::with_stroke(self.color,1))
|
||||||
|
.draw(display)?;
|
||||||
|
for line in lines {
|
||||||
|
line.abs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,8 +6,6 @@ use embedded_graphics::primitives::{Line, Primitive};
|
||||||
use embedded_graphics::style::PrimitiveStyle;
|
use embedded_graphics::style::PrimitiveStyle;
|
||||||
|
|
||||||
pub struct DrawableCurve<C, I>
|
pub struct DrawableCurve<C, I>
|
||||||
where
|
|
||||||
I: Iterator<Item=Point>,
|
|
||||||
{
|
{
|
||||||
scaled_data: I,
|
scaled_data: I,
|
||||||
color: C,
|
color: C,
|
||||||
|
|
|
@ -6,3 +6,4 @@ pub mod single_plot;
|
||||||
|
|
||||||
mod drawable_curve;
|
mod drawable_curve;
|
||||||
mod range_conv;
|
mod range_conv;
|
||||||
|
mod axis;
|
||||||
|
|
37
src/plot.rs
37
src/plot.rs
|
@ -1,37 +0,0 @@
|
||||||
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(())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,8 +5,6 @@ use embedded_graphics::prelude::Point;
|
||||||
use embedded_graphics::pixelcolor::PixelColor;
|
use embedded_graphics::pixelcolor::PixelColor;
|
||||||
|
|
||||||
pub struct PolyPlot<'a, C>
|
pub struct PolyPlot<'a, C>
|
||||||
where
|
|
||||||
C: PixelColor
|
|
||||||
{
|
{
|
||||||
curves: &'a [(Curve<'a>, C)],
|
curves: &'a [(Curve<'a>, C)],
|
||||||
top_left: Point,
|
top_left: Point,
|
||||||
|
|
Loading…
Reference in a new issue