1 | use plotters::prelude::*; |
2 | fn pdf(x: f64, y: f64) -> f64 { |
3 | const SDX: f64 = 0.1; |
4 | const SDY: f64 = 0.1; |
5 | const A: f64 = 5.0; |
6 | let x = x as f64 / 10.0; |
7 | let y = y as f64 / 10.0; |
8 | A * (-x * x / 2.0 / SDX / SDX - y * y / 2.0 / SDY / SDY).exp() |
9 | } |
10 | |
11 | const OUT_FILE_NAME: &'static str = "plotters-doc-data/3d-plot2.gif" ; |
12 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
13 | let root = BitMapBackend::gif(OUT_FILE_NAME, (600, 400), 100)?.into_drawing_area(); |
14 | |
15 | for pitch in 0..157 { |
16 | root.fill(&WHITE)?; |
17 | |
18 | let mut chart = ChartBuilder::on(&root) |
19 | .caption("2D Gaussian PDF" , ("sans-serif" , 20)) |
20 | .build_cartesian_3d(-3.0..3.0, 0.0..6.0, -3.0..3.0)?; |
21 | chart.with_projection(|mut p| { |
22 | p.pitch = 1.57 - (1.57 - pitch as f64 / 50.0).abs(); |
23 | p.scale = 0.7; |
24 | p.into_matrix() // build the projection matrix |
25 | }); |
26 | |
27 | chart |
28 | .configure_axes() |
29 | .light_grid_style(BLACK.mix(0.15)) |
30 | .max_light_lines(3) |
31 | .draw()?; |
32 | |
33 | chart.draw_series( |
34 | SurfaceSeries::xoz( |
35 | (-15..=15).map(|x| x as f64 / 5.0), |
36 | (-15..=15).map(|x| x as f64 / 5.0), |
37 | pdf, |
38 | ) |
39 | .style_func(&|&v| (VulcanoHSL::get_color(v / 5.0)).into()), |
40 | )?; |
41 | |
42 | root.present()?; |
43 | } |
44 | |
45 | // To avoid the IO failure being ignored silently, we manually call the present function |
46 | root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir" ); |
47 | println!("Result has been saved to {}" , OUT_FILE_NAME); |
48 | |
49 | Ok(()) |
50 | } |
51 | #[test] |
52 | fn entry_point() { |
53 | main().unwrap() |
54 | } |
55 | |