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).