/ concept-collection / hitandrun-interactive
Sign in
concept-collection / hitandrun-interactive
hitandrun-interactive / README.md
50 lines · 2.3 KBCodeBlameHistory
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.
67636f7Add figure-only view (#figure/sampler)Jeremy Magland 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.
9fbdc3eInteractive hit-and-run sampling of a 2D convex regionJeremy Magland 11## β–Ά [Open `hitandrun_demo.m`](hitandrun_demo.m) and click **Run**
13A random 2D convex region is generated and `N` points are drawn uniformly from
14it by **hit-and-run**: from the current point, pick a random direction, take the
15chord where that line crosses the region, and jump to a uniform point on it.
16Repeat. The figure shows the region and the samples.
18Controls:
20- **Samples** β€” set `N` (re-runs the sampler).
21- **Resample** β€” new samples, same region.
22- **New region** β€” a fresh region.
23- **Play movie** β€” step through the algorithm: each step draws the chord and the
24 point that landed on it.
26## How it works
28- [`hitandrun_demo.m`](hitandrun_demo.m) β€” driver: `addpath('helpers')`, seed, call the sampler.
29- [`hitandrun_sampler.m`](hitandrun_sampler.m) β€” opens the figure, sends data, handles resample requests.
30- `helpers/` β€” [`make_region.m`](helpers/make_region.m) and [`hit_and_run.m`](helpers/hit_and_run.m).
31- `app/` β€” a single-file React app that draws the region and samples on a canvas.
33The script and figure talk both ways: the script sends the region + samples via
34`uihtml(..., 'Data', ...)`, and the controls call back with
35`sendToMATLAB('resample' | 'newRegion', ...)`, which re-runs the sampler and
36returns new points via `sendEventToHTMLSource`. The script is stateless β€” the
37figure owns the region and passes it back with each request.
39## JIT-compiled kernel
41The loop in [`hit_and_run.m`](helpers/hit_and_run.m) runs once per sample, so
42numbl JS-JIT-compiles it to JavaScript β€” about 30Γ— faster than its interpreter,
43which is what keeps large `N` instant. The `%!numbl:assert_jit` directive
44asserts this happens (it errors rather than silently falling back). It relies on
45numbl's scalar-`rand()` JIT support; `rng(seed)` still controls the shared PRNG.
47## Deploy
49Pushing to `main` builds the app and publishes the project to GitHub Pages via
50the [deploy workflow](.github/workflows/deploy.yml).
↑↓ move↡openescclose