1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
extern crate piston_window;
extern crate graphics as piston_graphics;

use piston_window::*;
use piston_graphics::*;

pub fn plot(
	x: &[f64], 
	y: &[f64], 
	min_x: f64, 
	min_y: f64, 
	max_x: f64, 
	max_y: f64) 
{
	assert!(min_x < max_x && min_y < max_y);

    let (window_width, window_height) = (600, 600);

    let mut window: PistonWindow = WindowSettings::new("2d_line_plot", [window_width, window_height]).exit_on_esc(true).build().unwrap();
    while let Some(e) = window.next() 
    {
        window.draw_2d(&e, |context, graphics| {
            clear([1.0, 1.0, 1.0, 1.0], graphics);

            let line = Line::new([0.0, 0.0, 0.0, 1.0], 1.0);

            let mut prev: Option<(f64, f64)> = None;
            for (x, y) in x.into_iter().zip(y.into_iter())
            {
            	match prev
            	{
            		Some((px, py)) => {
            			let coords = [
            				(px - min_x) / (max_x - min_x) * (window_width as f64), 
            				(py - min_y) / (max_y - min_y) * (window_height as f64), 
            				(x - min_x) / (max_x - min_x) * (window_width as f64), 
            				(y - min_y) / (max_y - min_y) * (window_height as f64)];
            			
            			line.draw(
            			    coords, 
            			    &context.draw_state, 
            			    context.transform,
            			    graphics);
            		}
            		None => {}
            	}
            	prev = Some((*x, *y));
            }
        });
    }
}

// fn main()
// {
// 	plot(
//         &[0.0, 300.0, 300.0, 600.0],
//         &[0.0, 0.0, 600.0, 600.0],
// 		0.0, 
// 		0.0, 
// 		1200.0,
// 		1200.0);
// }

#[cfg(test)]
mod tests 
{
	use super::*;

    #[test]
    fn it_works() 
    {

    }
}