From a47a0b797fe6f9e2c16b2374d6d12422764a91a3 Mon Sep 17 00:00:00 2001 From: Simeon Widdis Date: Sun, 5 Mar 2023 12:19:51 -0800 Subject: [PATCH] Implement Path end method No longer requires accessing internals for the same functionality --- guide/src/changelog.md | 1 + nannou/src/geom/path.rs | 7 +++++++ nannou/tests/geom_tests.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/guide/src/changelog.md b/guide/src/changelog.md index 79f4b6fb8..42a0ba927 100644 --- a/guide/src/changelog.md +++ b/guide/src/changelog.md @@ -15,6 +15,7 @@ back to the origins. - Move `nannou_conrod` and `nannou_timeline` into a new repository: https://github.com/nannou-org/nannou_conrod. Both crates are deprecated in favour of `nannou_egui`. +- Add method for validated path ending with non-closed paths. --- diff --git a/nannou/src/geom/path.rs b/nannou/src/geom/path.rs index 28bf762f3..59b31f1c0 100644 --- a/nannou/src/geom/path.rs +++ b/nannou/src/geom/path.rs @@ -116,6 +116,13 @@ impl Builder { self } + /// Closes the current sub path without resetting the position to the first position of the + /// current sub-path. + pub fn end(mut self) -> Self { + self.builder.end(false); + self + } + /// Add a quadratic bezier curve to the path. pub fn quadratic_bezier_to(mut self, ctrl: Point2, to: Point2) -> Self { self.builder diff --git a/nannou/tests/geom_tests.rs b/nannou/tests/geom_tests.rs index 99693bffc..075863c37 100644 --- a/nannou/tests/geom_tests.rs +++ b/nannou/tests/geom_tests.rs @@ -1,3 +1,7 @@ +use std::matches; + +use lyon::path::Event; +use nannou::geom::path; use nannou::prelude::*; #[test] @@ -13,3 +17,29 @@ fn angle_test() { let vector = vec2(70.7, -60.8); assert_eq!(vector.angle(), -0.7102547457375739); } + +#[test] +fn path_builder_end_test() { + let mut builder = path(); + builder = builder.begin(vec2(-50.0, 0.0)); + builder = builder.quadratic_bezier_to(vec2(0.0, 25.0), vec2(50.0, 0.0)); + builder = builder.end(); + let path = builder.build(); + assert!(matches!( + path.iter().last(), + Some(Event::End { close: false, .. }) + )); +} + +#[test] +fn path_builder_close_test() { + let mut builder = path(); + builder = builder.begin(vec2(-50.0, 0.0)); + builder = builder.quadratic_bezier_to(vec2(0.0, 25.0), vec2(50.0, 0.0)); + builder = builder.close(); + let path = builder.build(); + assert!(matches!( + path.iter().last(), + Some(Event::End { close: true, .. }) + )); +}