Skip to content

Commit

Permalink
add interactive demo for subpath insideness
Browse files Browse the repository at this point in the history
  • Loading branch information
indierusty committed Jan 7, 2025
1 parent 04143b9 commit 8425c1c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions website/other/bezier-rs-demos/src/features-subpath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ const subpathFeatures = {
),
inputOptions: [intersectionErrorOptions, minimumSeparationOptions],
},
"inside-other": {
name: "Inside (Other Subpath)",
callback: (subpath: WasmSubpathInstance, options: Record<string, number>): string =>
subpath.inside_subpath(
[
[40, 40],
[160, 40],
[160, 80],
[200, 100],
[160, 120],
[160, 160],
[40, 160],
[40, 120],
[80, 100],
[40, 80],
],
options.error,
options.minimum_separation,
),
inputOptions: [intersectionErrorOptions, minimumSeparationOptions],
},
curvature: {
name: "Curvature",
callback: (subpath: WasmSubpathInstance, options: Record<string, number>, _: undefined): string => subpath.curvature(options.t, SUBPATH_T_VALUE_VARIANTS[options.TVariant]),
Expand Down
15 changes: 15 additions & 0 deletions website/other/bezier-rs-demos/wasm/src/subpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,21 @@ impl WasmSubpath {
wrap_svg_tag(format!("{subpath_svg}{rectangle_svg}{intersections_svg}"))
}

pub fn inside_subpath(&self, js_points: JsValue, error: f64, minimum_separation: f64) -> String {
let array = js_points.dyn_into::<Array>().unwrap();
let points = array.iter().map(|p| parse_point(&p));
let other = Subpath::<EmptyId>::from_anchors(points, true);

let is_inside = self.0.is_inside_subpath(&other, Some(error), Some(minimum_separation));
let color = if is_inside { RED } else { BLACK };

let self_svg = self.to_default_svg();
let mut other_svg = String::new();
other.curve_to_svg(&mut other_svg, CURVE_ATTRIBUTES.replace(BLACK, color));

wrap_svg_tag(format!("{self_svg}{other_svg}"))
}

pub fn curvature(&self, t: f64, t_variant: String) -> String {
let subpath = self.to_default_svg();
let t = parse_t_variant(&t_variant, t);
Expand Down

0 comments on commit 8425c1c

Please sign in to comment.