Offset a polygon outward
Grow a convex polygon by a fixed clearance with parallel edges; drag the points or the distance slider. TypeScript with VisQuill.
import { type Point, Reactive, VisQuill, ConvexHulls, Offsets, Points, Segments } from "@visquill/visquill-gdk";
import './styles.css';
/**
* Demonstrates polygon offsetting: an outer polygon is computed by expanding
* the convex hull outward by a fixed distance. Drag any handle to see both
* the hull and its offset update reactively. Use the slider to adjust the
* offset distance.
*
* @param div - The div element that will contain the graphic
*/
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-geometry-polygon-offset-");
const canvas = rvg.canvas.layer();
const controls = rvg.canvas.layer();
const distance = canvas.values.real(50);
// ─── 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 and Offset ───────────────────────────────────────
const polygon = canvas.visuals.polygon("contour");
const offset = canvas.visuals.polygon("offset");
Reactive.do([...handles, distance], () => {
ConvexHulls.topStart(handles, polygon);
Offsets.polygon(polygon, distance, true, 1, offset);
}, true);
// ─── Distance Slider ─────────────────────────────────────────────────────
const track = controls.visuals.segment("track", [100, 50], [300, 50]);
const slider = controls.handles.disk("handle", [100, 50]);
const label = controls.text.label("", "slider-label");
Segments.pointAt(track, 0.5, slider);
// Keep the handle on the track; the distance is how far it is from the track's start.
Reactive.do([slider], () => {
Points.closestPointOnSegment(slider, track, slider);
Points.distanceToPoint(track.at(0), slider, distance);
label.value = distance.value.toFixed(0) + "px";
Points.moveDown(slider, 25, label);
}, true);
// ─── Instructions ────────────────────────────────────────────────────────
rvg.canvas.text.labelAt(
"Drag any point — the offset polygon maintains the specified distance.",
[350, 30],
"instructions"
);
}The convex hull of twenty draggable points, grown outward with Offsets.polygon. The distance is a
reactive value set by a slider.
How it works
The distance is a Real from canvas.values.real(). A Real is a sender like a handle, so it goes into the same Reactive.do as the points. The reaction rebuilds the hull and then its offset:
Reactive.do([...handles, distance], () => {
ConvexHulls.topStart(handles, polygon);
Offsets.polygon(polygon, distance, true, 1, offset);
}, true);
The slider is a plain handle. Its own reaction pulls it back onto the track and writes its distance from the track's start into distance:
Reactive.do([slider], () => {
Points.closestPointOnSegment(slider, track, slider);
Points.distanceToPoint(track.at(0), slider, distance);
// …
}, true);
API
Reactive.do: runs a function now and whenever its senders changeConvexHulls.topStart: the convex hull of a set of pointsOffsets.polygon: a polygon offset from a baseline polygon to its left or right sidePoints.closestPointOnSegment: the point on a segment closest to a given pointPoints.distanceToPoint: distance between two points
Notes
Offsets.polygon is meant for convex baselines; a concave one can give a self-intersecting result. The fourth argument sets how many extra points round each corner.
See also
geometry/02-convex-hull




