/ proof-of-concept / hitandrun-interactive
Sign in
proof-of-concept / 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