1# Interactive hit-and-run sampling
3Runs in the browser via [numbl](https://numbl.org) โ no install.
5## ๐ [Just the figure โ](https://concept-collection.github.io/hitandrun-interactive/#figure/sampler)
7The figure-only view runs the sampler and drops you straight into the
8interactive figure (it shows the run's output first, then the figure). The full
9developer view โ file tree, editable code, console โ is below.
11## โถ [Open `hitandrun_demo.m`](hitandrun_demo.m) and click **Run**
13A random 2D region is generated and `N` points are drawn uniformly from it by
14**hit-and-run**: from the current point, pick a random direction, take the chord
15where that line crosses the region, and jump to a uniform point on it. Repeat.
16The figure shows the region and the samples. The region is convex by default,
17but you can switch to a non-convex **dumbbell** โ two convex bulbs joined by a
18narrow tube โ where a single line can enter and leave the region more than once.
20Controls:
22- **Samples** โ set `N` (re-runs the sampler).
23- **Resample** โ new samples, same region.
24- **New region** โ a fresh region (of the current type).
25- **non-convex region** โ toggle between a convex region and the non-convex
26 dumbbell. The non-convex region uses the slower general sampler, so `N` is
27 capped lower.
28- **local segment only** *(non-convex only)* โ by default the step samples
29 across *every* segment where the line crosses the region (the standard walk,
30 uniform on the whole region); check this to instead sample only within the
31 segment that contains the current point โ a local walk that can't jump across
32 the gap between two bulbs, so it does not sample uniformly.
33- **Play movie** โ step through the algorithm: each step draws the chord โ one
34 segment for a convex region, possibly several for a non-convex one (or just
35 the local segment) โ and the point that landed on it.
37## How it works
39- [`hitandrun_demo.m`](hitandrun_demo.m) โ driver: `addpath('helpers')`, seed, call the sampler.
40- [`hitandrun_sampler.m`](hitandrun_sampler.m) โ opens the figure, sends data, handles resample requests, and dispatches to the convex or non-convex sampler.
41- `helpers/` โ [`make_region.m`](helpers/make_region.m) (convex ellipse or dumbbell region), [`hit_and_run.m`](helpers/hit_and_run.m) (convex chord), and [`hit_and_run_general.m`](helpers/hit_and_run_general.m) (arbitrary simple polygon).
42- `app/` โ a single-file React app that draws the region and samples on a canvas.
44The script and figure talk both ways: the script sends the region + samples via
45`uihtml(..., 'Data', ...)`, and the controls call back with
46`sendToMATLAB('resample' | 'newRegion', ...)` (each carrying whether the region
47is `convex` and, for non-convex, the `local` sampling mode), which re-runs the
48sampler and returns new points via `sendEventToHTMLSource`. The script is
49stateless โ the figure owns the region and passes it back with each request.
51## Convex vs. non-convex
53`hitandrun_sampler.m` picks the sampler by region type:
55- **Convex** โ [`hit_and_run.m`](helpers/hit_and_run.m) takes the single chord
56 where the line crosses the region (an inward-half-plane intersection). Its
57 loop runs once per sample, so numbl JS-JIT-compiles it to JavaScript โ about
58 30ร faster than its interpreter, which is what keeps large `N` instant. The
59 `%!numbl:assert_jit` directive asserts this happens (it errors rather than
60 silently falling back). It relies on numbl's scalar-`rand()` JIT support;
61 `rng(seed)` still controls the shared PRNG.
62- **Non-convex** โ [`hit_and_run_general.m`](helpers/hit_and_run_general.m)
63 finds *every* crossing along the line and keeps the in-region segments (a
64 point-in-polygon test on each interval's midpoint), so concavities are handled
65 correctly. It then samples in one of two modes: **union** (default) picks a
66 point uniformly across all those segments, which samples the whole region
67 uniformly; **local** (the *local segment only* checkbox) restricts to the
68 single segment straddling the current point โ a local walk that can't jump the
69 gap between the two bulbs, so it does *not* sample uniformly. The sort +
70 polygon tests don't JIT, so this runs in the interpreter and `N` is capped
71 lower. `make_region(false)` builds the dumbbell, which is not star-shaped and
72 need not contain the origin, so the sampler finds an interior start by
73 rejection rather than assuming one.
75## Deploy
77Pushing to `main` builds the app and publishes the project to GitHub Pages via
78the [deploy workflow](.github/workflows/deploy.yml).