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/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 隅の値の線形補間で辺上に配置する。全セルの線分をつなぐと閉じた等値線になり、閾値を上下させると輪郭が移動する。
パラメータ
threshold: number — Iso-value at which the contour is extracted from the scalar field.resolution: number — Grid cell size; smaller cells give smoother contours.interpolate: boolean — Whether edge crossings are linearly interpolated for smoother lines.
系譜
よく組み合わされるアルゴリズム
- Perlin / Simplex Noise · 13 作品
- HSL / Hue Cycle · 10 作品
- Bezier / Catmull-Rom Splines · 7 作品
- Fragment Shader (GLSL) · 5 作品
- Flow Field / Vector Field · 4 作品
- Delaunay Triangulation · 3 作品
- Particle System · 3 作品
- Quadtree / Recursive Quad Split · 2 作品
- Voronoi Diagram · 2 作品
- Audio-Reactive · 2 作品
- Poisson Disk Sampling · 1 作品
- 3D Mesh / Geometry · 1 作品
このアルゴリズムを使う作品 (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