Implement bezier library's De Casteljau points function (#715)

* pre-rebase

* broken wasm

* hull lines

* update rust lib description

* update comments, handle match statement better, pass Point type to vue

* cleanup

* add linear case

* More idiomatic code

* Further simplifications to the algorithm and removal of more heap allocations

* Rename to de_casteljau_points and use colors for the iterations

* Small comment changes

* Improve colors

Co-authored-by: Jackie Chen <jackiechen73>
Co-authored-by: Keavon Chambers <keavon@keavon.com>
Co-authored-by: Hannah Li <hannahli2010@gmail.com>
This commit is contained in:
Jackie Chen
2022-07-27 00:35:43 -04:00
committed by Keavon Chambers
co-authored by Jackie Chen Keavon Chambers Hannah Li
parent dc0b38750c
commit 004c2aeff6
3 changed files with 57 additions and 0 deletions
@@ -281,6 +281,29 @@ export default defineComponent({
],
},
},
{
name: "De Casteljau Points",
callback: (canvas: HTMLCanvasElement, bezier: WasmBezierInstance, options: Record<string, number>): void => {
const hullPoints: Point[][] = JSON.parse(bezier.de_casteljau_points(options.t));
hullPoints.reverse().forEach((iteration: Point[], iterationIndex) => {
const colorLight = `hsl(${90 * iterationIndex}, 100%, 50%)`;
iteration.forEach((point: Point, index) => {
// Skip the anchor and handle points which are already drawn in black
if (iterationIndex !== hullPoints.length - 1) {
drawPoint(getContextFromCanvas(canvas), point, 4, colorLight);
}
if (index !== 0) {
const prevPoint: Point = iteration[index - 1];
drawLine(getContextFromCanvas(canvas), point, prevPoint, colorLight);
}
});
});
},
template: markRaw(SliderExample),
templateOptions: { sliders: [tSliderOptions] },
},
{
name: "Rotate",
callback: (canvas: HTMLCanvasElement, bezier: WasmBezierInstance, options: Record<string, number>): void => {
@@ -138,4 +138,14 @@ impl WasmBezier {
let bezier_points: Vec<Vec<Point>> = self.0.reduce(None).into_iter().map(bezier_to_points).collect();
to_js_value(bezier_points)
}
pub fn de_casteljau_points(&self, t: f64) -> JsValue {
let hull = self
.0
.de_casteljau_points(t)
.iter()
.map(|level| level.iter().map(|&point| Point { x: point.x, y: point.y }).collect::<Vec<Point>>())
.collect::<Vec<Vec<Point>>>();
to_js_value(hull)
}
}