graphics icon indicating copy to clipboard operation
graphics copied to clipboard

Add a `Plot` type for plotting 2D functions?

Open mitchmindtree opened this issue 8 years ago • 4 comments

I've come across a few cases where it'd be nice to be able to draw straight from some generic FnMut, i.e.

let f = |x: f64| x.sin() * 20.0;
let rect = ...;
Plot::new(...).draw(f, rect, draw_state, transform, g);

where f is the function used to plot the line, rect is the bounds for the plot and x is the x Scalar distance from the left rect edge.

Some use-cases (with rough pseudocode):

  • Easier drawing for envelopes: |x: f64| env.y(x)
  • Drawing audio waveforms:
|x: f64| {
    let idx = (x / width) * waveform.len();
    let amp = waveform[idx];
    amp * 30.0
};
  • Other kinds of mathematical functions.

I imagine it'd have builder params like line thickness, color etc.

Would be very useful for a Plot widget in conrod, which could then be used as a part of a more flexible EnvelopeEditor, WaveformDisplay, etc widgets.

Due to my graphics newbie-ness I'm unsure how to implement this myself, any help would be appreciated! Would certainly be interested in following along if someone does decide to implement this.

mitchmindtree avatar Dec 09 '15 04:12 mitchmindtree

@bvssvni after having a look, it looks like this would require adding another required method to the Graphics trait - is that correct? I'm curious to get your thoughts on the feasibility of this when you get the chance :smile:

mitchmindtree avatar Dec 19 '15 23:12 mitchmindtree

I guess another alternative is to use the Image type by rendering the "plot" to a texture?

mitchmindtree avatar Dec 20 '15 02:12 mitchmindtree

You could also pass a Line to a function that renders the graph.

bvssvni avatar Dec 20 '15 11:12 bvssvni

Yeah I'd considered using Line, but I thought it might be really inefficient for super detailed plots like waveforms? Maybe it's not though, I haven't even tried yet! Guess I'll see how that goes first :+1:

mitchmindtree avatar Dec 20 '15 12:12 mitchmindtree