1 | use plotters::prelude::*; |
2 | use std::ops::Range; |
3 | |
4 | const OUT_FILE_NAME: &'static str = "plotters-doc-data/mandelbrot.png" ; |
5 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
6 | let root = BitMapBackend::new(OUT_FILE_NAME, (800, 600)).into_drawing_area(); |
7 | |
8 | root.fill(&WHITE)?; |
9 | |
10 | let mut chart = ChartBuilder::on(&root) |
11 | .margin(20) |
12 | .x_label_area_size(10) |
13 | .y_label_area_size(10) |
14 | .build_cartesian_2d(-2.1f64..0.6f64, -1.2f64..1.2f64)?; |
15 | |
16 | chart |
17 | .configure_mesh() |
18 | .disable_x_mesh() |
19 | .disable_y_mesh() |
20 | .draw()?; |
21 | |
22 | let plotting_area = chart.plotting_area(); |
23 | |
24 | let range = plotting_area.get_pixel_range(); |
25 | |
26 | let (pw, ph) = (range.0.end - range.0.start, range.1.end - range.1.start); |
27 | let (xr, yr) = (chart.x_range(), chart.y_range()); |
28 | |
29 | for (x, y, c) in mandelbrot_set(xr, yr, (pw as usize, ph as usize), 100) { |
30 | if c != 100 { |
31 | plotting_area.draw_pixel((x, y), &MandelbrotHSL::get_color(c as f64 / 100.0))?; |
32 | } else { |
33 | plotting_area.draw_pixel((x, y), &BLACK)?; |
34 | } |
35 | } |
36 | |
37 | // To avoid the IO failure being ignored silently, we manually call the present function |
38 | root.present().expect("Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir" ); |
39 | println!("Result has been saved to {}" , OUT_FILE_NAME); |
40 | |
41 | Ok(()) |
42 | } |
43 | |
44 | fn mandelbrot_set( |
45 | real: Range<f64>, |
46 | complex: Range<f64>, |
47 | samples: (usize, usize), |
48 | max_iter: usize, |
49 | ) -> impl Iterator<Item = (f64, f64, usize)> { |
50 | let step = ( |
51 | (real.end - real.start) / samples.0 as f64, |
52 | (complex.end - complex.start) / samples.1 as f64, |
53 | ); |
54 | return (0..(samples.0 * samples.1)).map(move |k| { |
55 | let c = ( |
56 | real.start + step.0 * (k % samples.0) as f64, |
57 | complex.start + step.1 * (k / samples.0) as f64, |
58 | ); |
59 | let mut z = (0.0, 0.0); |
60 | let mut cnt = 0; |
61 | while cnt < max_iter && z.0 * z.0 + z.1 * z.1 <= 1e10 { |
62 | z = (z.0 * z.0 - z.1 * z.1 + c.0, 2.0 * z.0 * z.1 + c.1); |
63 | cnt += 1; |
64 | } |
65 | return (c.0, c.1, cnt); |
66 | }); |
67 | } |
68 | #[test] |
69 | fn entry_point() { |
70 | main().unwrap() |
71 | } |
72 | |