/ concept-collection / hitandrun-interactive
Sign in
concept-collection / hitandrun-interactive
hitandrun-interactive
Go to fileHistoryFork
.githubInteractive hit-and-run sampling of a 2D convex region
appInteractive hit-and-run sampling of a 2D convex region
helpersInteractive hit-and-run sampling of a 2D convex region
.gitignoreInteractive hit-and-run sampling of a 2D convex region
.numblignoreInteractive hit-and-run sampling of a 2D convex region
CLAUDE.mdInteractive hit-and-run sampling of a 2D convex region
hitandrun_demo.mInteractive hit-and-run sampling of a 2D convex region
hitandrun_sampler.mInteractive hit-and-run sampling of a 2D convex region
numbl-project.jsonAdd figure-only view (#figure/sampler)
README.mdAdd figure-only view (#figure/sampler)

Interactive hit-and-run sampling#

Runs in the browser via numbl β€” no install.

πŸŽ› Just the figure β†’#

The figure-only view runs the sampler and drops you straight into the interactive figure (it shows the run's output first, then the figure). The full developer view β€” file tree, editable code, console β€” is below.

β–Ά Open hitandrun_demo.m and click Run#

A random 2D convex region is generated and N points are drawn uniformly from it by hit-and-run: from the current point, pick a random direction, take the chord where that line crosses the region, and jump to a uniform point on it. Repeat. The figure shows the region and the samples.

Controls:

  • Samples β€” set N (re-runs the sampler).
  • Resample β€” new samples, same region.
  • New region β€” a fresh region.
  • Play movie β€” step through the algorithm: each step draws the chord and the point that landed on it.

How it works#

The script and figure talk both ways: the script sends the region + samples via uihtml(..., 'Data', ...), and the controls call back with sendToMATLAB('resample' | 'newRegion', ...), which re-runs the sampler and returns new points via sendEventToHTMLSource. The script is stateless β€” the figure owns the region and passes it back with each request.

JIT-compiled kernel#

The loop in hit_and_run.m runs once per sample, so numbl JS-JIT-compiles it to JavaScript β€” about 30Γ— faster than its interpreter, which is what keeps large N instant. The %!numbl:assert_jit directive asserts this happens (it errors rather than silently falling back). It relies on numbl's scalar-rand() JIT support; rng(seed) still controls the shared PRNG.

Deploy#

Pushing to main builds the app and publishes the project to GitHub Pages via the deploy workflow.

↑↓ move↡openescclose