Marching Squares / Contours

初出 1987 · William E. Lorensen、Harvey Cline — 2D case of Marching Cubes — Lorensen & Cline (1987). · 年表 ↗

コミュニティ・通称 名称 — 記述的命名。Marching Cubes(Lorensen & Cline, 1987) の2次元版で、名は3次元版に倣ったもので独立した命名ではない。

ひとことで スカラー場から等値線を抽出する 2D 手法。Marching Cubes の 2D 版で、メタボールや等高線図に使う。

Marching Squares / Contours · Open in new tab ↗ · Motion begins only when you press Run.

コード

上のデモの実際のスケッチ(サンドボックスで再現可能)。これで全部です。

marching-squares/index.html
const SEED = Math.floor(Math.random()*1e9);

function setup() {
  noiseSeed(SEED); randomSeed(SEED);
  const s = Math.min(windowWidth, windowHeight);
  createCanvas(s, s);
  background('#0a0a0a');
  strokeCap(ROUND);

  // Sample a noise field on a coarse grid, then run marching-squares
  // to extract iso-lines for several thresholds.
  const G = 60;                // grid resolution (G x G cells)
  const cs = s / G;            // cell size
  const FREQ = 0.04;
  const ISOS = [0.4, 0.5, 0.6, 0.7];
  const COLORS = ['#264653', '#2a9d8f', '#e9c46a', '#e76f51'];

  // Build the scalar field.
  const f = new Array((G + 1) * (G + 1));
  for (let y = 0; y <= G; y++) {
    for (let x = 0; x <= G; x++) {
      f[y * (G + 1) + x] = noise(x * FREQ, y * FREQ);
    }
  }

  strokeWeight(s * 0.0025);
  for (let i = 0; i < ISOS.length; i++) {
    stroke(COLORS[i]);
    const iso = ISOS[i];
    for (let y = 0; y < G; y++) {
      for (let x = 0; x < G; x++) {
        const tl = f[y * (G + 1) + x];
        const tr = f[y * (G + 1) + x + 1];
        const br = f[(y + 1) * (G + 1) + x + 1];
        const bl = f[(y + 1) * (G + 1) + x];
        // 4-bit corner classification
        let c = 0;
        if (tl > iso) c |= 8;
        if (tr > iso) c |= 4;
        if (br > iso) c |= 2;
        if (bl > iso) c |= 1;
        if (c === 0 || c === 15) continue;
        // edge midpoints (linearly interpolated)
        const px = x * cs, py = y * cs;
        const T = [px + lerpEdge(tl, tr) * cs, py];
        const R = [px + cs, py + lerpEdge(tr, br) * cs];
        const B = [px + lerpEdge(bl, br) * cs, py + cs];
        const L = [px, py + lerpEdge(tl, bl) * cs];
        function lerpEdge(a, b) { return (iso - a) / (b - a); }
        // segment table for each case
        switch (c) {
          case 1: case 14: seg(L, B); break;
          case 2: case 13: seg(B, R); break;
          case 3: case 12: seg(L, R); break;
          case 4: case 11: seg(T, R); break;
          case 5: seg(L, T); seg(B, R); break;
          case 6: case 9: seg(T, B); break;
          case 7: case 8: seg(L, T); break;
          case 10: seg(T, R); seg(L, B); break;
        }
      }
    }
  }
}
function seg(a, b) { line(a[0], a[1], b[0], b[1]); }

デモのソースを開く ↗

仕組み

スカラー場をグリッドで標本化し閾値(等値)を選ぶ。4 隅のセルごとに、(1) 各隅を内(≥閾値)/外で分類して 4 ビット索引(16 ケース)を作り、(2) ルックアップ表で輪郭が横切る辺と結び方を求め(2 つの曖昧な鞍点ケースは中心値で解決)、(3) 各交点を 2 隅の値の線形補間で辺上に配置する。全セルの線分をつなぐと閉じた等値線になり、閾値を上下させると輪郭が移動する。

パラメータ

系譜

関連
Metaballs、Reaction-Diffusion

よく組み合わされるアルゴリズム

このアルゴリズムを使う作品 (26)

Alien DNA
shvembldr
Ancient Courses of Fictional Rivers
robert-hodgin
Anticyclone
william-mapan
Archetype
kjetil-golid
Avalon
r0zk0
Cells
hevey
Cytographia
golan-levin
Diggly Collects All
dca
entretiempos
marcelo-soria-rodriguez
Fake Internet Money
steve-pikelny
Flows
ryley-o-eth
Glasshouse INAT
aleksandra-jovanic
Low Tide
artem-verkhovskiy-x-andy-shaw
Marfa Yucca
daniel-calderon-arenas
Metaballs
stanko-tadic
Neural Sediments
eko33
Para Bellum
matty-mariansky
Perpetua
punch-card-collective
Pizza 1o1
kala
Recursion
hevey
send/receive
snowfro
Space Birds
mark-cotton
Strata
vebj-rn-isaksen
Supermental
rosenlykke
The Opera
luke-shannon
Túnel Dimensional
autonomoses

最終更新 2026-07-16