mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-29 07:48:11 +08:00
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:
committed by
Keavon Chambers
parent
a6c91204d6
commit
3c2fff4465
@@ -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);
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
|
||||
@@ -107,4 +107,13 @@ impl WasmBezier {
|
||||
let local_extrema = self.0.local_extrema();
|
||||
JsValue::from_serde(&serde_json::to_string(&local_extrema).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
pub fn rotate(&self, angle: f64) -> WasmBezier {
|
||||
WasmBezier(self.0.rotate(angle))
|
||||
}
|
||||
|
||||
pub fn line_intersection(&self, js_points: &JsValue) -> Vec<JsValue> {
|
||||
let line: [DVec2; 2] = js_points.into_serde().unwrap();
|
||||
self.0.line_intersection(line).iter().map(|&p| vec_to_point(&p)).collect::<Vec<JsValue>>()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user