Convex hull of draggable points
Compute the convex hull of a point set and keep it up to date while you drag any point, in a few lines of TypeScript with VisQuill.
import { type Point, Reactive, VisQuill, ConvexHulls } from "@visquill/visquill-gdk";
import './styles.css';
/**
* Interactive convex hull demonstrating reactive geometry computation.
* Drag any of the 20 handles to see the hull update in real time.
* The hull is recomputed whenever any point moves, showing how
* Reactive.do() can depend on an entire array of points.
*
* @param div - The div element that will contain the graphic
*/
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-geometry-convex-hull-");
const canvas = rvg.canvas.layer();
const controls = rvg.canvas.layer();
// ─── Create Handles ──────────────────────────────────────────────────────
const handles: Point[] = [];
for (let i = 0; i < 20; i++) {
const handle = controls.handles.disk("handle", [100 + Math.random() * 500, 200 + Math.random() * 500])
handles.push(handle);
}
// ─── Create Convex Hull ──────────────────────────────────────────────────
const polygon = canvas.visuals.polygon("contour");
Reactive.do(handles, () => {
ConvexHulls.topStart(handles, polygon);
}, true);
// ─── Instructions ────────────────────────────────────────────────────────
rvg.canvas.text.labelAt(
"Drag any point to update the convex hull",
[350, 30],
"instructions"
);
}The convex hull of twenty draggable points, recomputed on every drag. ConvexHulls.topStart writes
it into the existing polygon, and one Reactive.do over all handles does the wiring.
How it works
Reactive.do takes any iterable of senders, so the whole array of handles can be passed at once. The reaction runs when any handle moves. ConvexHulls.topStart writes the hull into the polygon that is already on the canvas, so no new shape is created per drag step.
const polygon = canvas.visuals.polygon("contour");
Reactive.do(handles, () => {
ConvexHulls.topStart(handles, polygon);
}, true);
The handles live on a second layer above the polygon, so the filled hull never covers them.
API
Reactive.do: runs a function now and whenever its senders changeConvexHulls.topStart: the convex hull of a set of points, starting at the topmost point
See also
geometry/03-offset-polygongeometry/polylines-09-convex-hull




