diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
commit | 9835e2ae736235810b4ea1c162ca5e65c547e770 (patch) | |
tree | 3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/plotters/examples/animation.rs | |
parent | Releasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff) | |
download | rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/plotters/examples/animation.rs')
-rw-r--r-- | vendor/plotters/examples/animation.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/plotters/examples/animation.rs b/vendor/plotters/examples/animation.rs new file mode 100644 index 000000000..dab7d451d --- /dev/null +++ b/vendor/plotters/examples/animation.rs @@ -0,0 +1,65 @@ +use plotters::prelude::*; + +fn snowflake_iter(points: &[(f64, f64)]) -> Vec<(f64, f64)> { + let mut ret = vec![]; + for i in 0..points.len() { + let (start, end) = (points[i], points[(i + 1) % points.len()]); + let t = ((end.0 - start.0) / 3.0, (end.1 - start.1) / 3.0); + let s = ( + t.0 * 0.5 - t.1 * (0.75f64).sqrt(), + t.1 * 0.5 + (0.75f64).sqrt() * t.0, + ); + ret.push(start); + ret.push((start.0 + t.0, start.1 + t.1)); + ret.push((start.0 + t.0 + s.0, start.1 + t.1 + s.1)); + ret.push((start.0 + t.0 * 2.0, start.1 + t.1 * 2.0)); + } + ret +} + +const OUT_FILE_NAME: &'static str = "plotters-doc-data/animation.gif"; +fn main() -> Result<(), Box<dyn std::error::Error>> { + let root = BitMapBackend::gif(OUT_FILE_NAME, (800, 600), 1_000)?.into_drawing_area(); + + for i in 0..8 { + root.fill(&WHITE)?; + + let mut chart = ChartBuilder::on(&root) + .caption( + format!("Koch's Snowflake (n_iter = {})", i), + ("sans-serif", 50), + ) + .build_cartesian_2d(-2.0..2.0, -1.5..1.5)?; + + let mut snowflake_vertices = { + let mut current: Vec<(f64, f64)> = vec![ + (0.0, 1.0), + ((3.0f64).sqrt() / 2.0, -0.5), + (-(3.0f64).sqrt() / 2.0, -0.5), + ]; + for _ in 0..i { + current = snowflake_iter(¤t[..]); + } + current + }; + + chart.draw_series(std::iter::once(Polygon::new( + snowflake_vertices.clone(), + &RED.mix(0.2), + )))?; + + snowflake_vertices.push(snowflake_vertices[0]); + chart.draw_series(std::iter::once(PathElement::new(snowflake_vertices, &RED)))?; + + root.present()?; + } + + println!("Result has been saved to {}", OUT_FILE_NAME); + + Ok(()) +} + +#[test] +fn entry_point() { + main().unwrap() +} |