pub trait DrawView {
fn draw(&self, view: &View, canvas: &mut ViewCanvas<'_>);
}
Expand description
A drawable component on the view.
Required Methods§
sourcefn draw(&self, view: &View, canvas: &mut ViewCanvas<'_>)
fn draw(&self, view: &View, canvas: &mut ViewCanvas<'_>)
Draw the component on the given canvas.
Simply draw on the given canvas lines and/or points. See ViewCanvas
for more
informations.
view
is provided only to give context if needed. It gives access to the domain, codomain,
size of the view, etc… See View
for more informations.
Examples
This draws a rectangle centered on both the x and y axis.
use termplot::{ViewCanvas, DrawView, Domain, Size, Plot, View};
struct Rect;
impl DrawView for Rect {
fn draw(&self, _: &View, canvas: &mut ViewCanvas) {
canvas.line(-2.0, 2.0, 2.0, 2.0);
canvas.line(2.0, 2.0, 2.0, -2.0);
canvas.line(-2.0, -2.0, 2.0, -2.0);
canvas.line(-2.0, 2.0, -2.0, -2.0);
}
}
let mut plot = Plot::default();
plot.set_domain(Domain(-5.0..5.0))
.set_codomain(Domain(-5.0..5.0))
.set_size(Size::new(50, 50))
.add_plot(Box::new(Rect));
println!("{plot}");