9fbdc3eInteractive hit-and-run sampling of a 2D convex regionJeremy Magland 1# Interactive hit-and-run sampling
3Runs in the browser via [numbl](https://numbl.org) — no install.
5## ▶ [Open `hitandrun_demo.m`](hitandrun_demo.m) and click **Run**
7A random 2D convex region is generated and `N` points are drawn uniformly from
8it by **hit-and-run**: from the current point, pick a random direction, take the
9chord where that line crosses the region, and jump to a uniform point on it.
10Repeat. The figure shows the region and the samples.
12Controls:
14- **Samples** — set `N` (re-runs the sampler).
15- **Resample** — new samples, same region.
16- **New region** — a fresh region.
17- **Play movie** — step through the algorithm: each step draws the chord and the
18 point that landed on it.
20## How it works
22- [`hitandrun_demo.m`](hitandrun_demo.m) — driver: `addpath('helpers')`, seed, call the sampler.
23- [`hitandrun_sampler.m`](hitandrun_sampler.m) — opens the figure, sends data, handles resample requests.
24- `helpers/` — [`make_region.m`](helpers/make_region.m) and [`hit_and_run.m`](helpers/hit_and_run.m).
25- `app/` — a single-file React app that draws the region and samples on a canvas.
27The script and figure talk both ways: the script sends the region + samples via
28`uihtml(..., 'Data', ...)`, and the controls call back with
29`sendToMATLAB('resample' | 'newRegion', ...)`, which re-runs the sampler and
30returns new points via `sendEventToHTMLSource`. The script is stateless — the
31figure owns the region and passes it back with each request.
33## JIT-compiled kernel
35The loop in [`hit_and_run.m`](helpers/hit_and_run.m) runs once per sample, so
36numbl JS-JIT-compiles it to JavaScript — about 30× faster than its interpreter,
37which is what keeps large `N` instant. The `%!numbl:assert_jit` directive
38asserts this happens (it errors rather than silently falling back). It relies on
39numbl's scalar-`rand()` JIT support; `rng(seed)` still controls the shared PRNG.
41## Deploy
43Pushing to `main` builds the app and publishes the project to GitHub Pages via
44the [deploy workflow](.github/workflows/deploy.yml).