Implement function to find intersections between a Bezier and a linear line (#708)

* Implement line intersection for quadratics, begin work for cubic

* Implement line intersection for cubic beziers, add tests for cubic root finding

* Rename function and update comments

* Minor refactor and adjust comments

* Address PR comments
This commit is contained in:
Hannah Li
2022-07-04 19:29:25 -04:00
committed by Keavon Chambers
parent a6c91204d6
commit 3c2fff4465
4 changed files with 322 additions and 12 deletions
@@ -250,6 +250,45 @@ export default defineComponent({
});
},
},
{
name: "Rotate",
callback: (canvas: HTMLCanvasElement, bezier: WasmBezierInstance, options: Record<string, number>): void => {
const context = getContextFromCanvas(canvas);
const rotatedBezier = bezier
.rotate((options.angle * Math.PI) / 180)
.get_points()
.map((p) => JSON.parse(p));
drawBezier(context, rotatedBezier, null, { curveStrokeColor: COLORS.NON_INTERACTIVE.STROKE_1, radius: 3.5 });
},
template: markRaw(SliderExample),
templateOptions: {
sliders: [
{
variable: "angle",
min: -90,
max: 90,
step: 5,
default: 15,
},
],
},
},
{
name: "Line Intersection",
callback: (canvas: HTMLCanvasElement, bezier: WasmBezierInstance): void => {
const context = getContextFromCanvas(canvas);
const line = [
{ x: 150, y: 150 },
{ x: 30, y: 30 },
];
const mappedLine = line.map((p) => [p.x, p.y]);
drawLine(context, line[0], line[1], COLORS.NON_INTERACTIVE.STROKE_1);
const intersections: Point[] = bezier.line_intersection(mappedLine).map((p) => JSON.parse(p));
intersections.forEach((p: Point) => {
drawPoint(context, p, 3, COLORS.NON_INTERACTIVE.STROKE_2);
});
},
},
],
};
},