L-System
初出 1968 · Aristid Lindenmayer — Introduced by Aristid Lindenmayer in 1968 to model plant development; generalised for graphics in the 1990 book. · 年表 ↗
第三者・冠名 名称 — 「L-system」は Aristid Lindenmayer への献名("L")。Lindenmayer は 1968 年に形式を developmental systems として導入し、略称は後の形式言語/グラフィクスの共同体が定着させた。
ひとことで 公理と書き換え規則の集合から自己相似的な文字列を生成し、それをタートルグラフィックスで描く手法。植物の枝分かれが代表例。
コード
上のデモの実際のスケッチ(サンドボックスで再現可能)。これで全部です。
l-system/index.html
const SEED = Math.floor(Math.random()*1e9);
function setup() {
randomSeed(SEED);
const s = Math.min(windowWidth, windowHeight);
createCanvas(s, s);
background('#f5f1e8');
noFill();
stroke('#2c3a26');
strokeCap(ROUND);
// Classical stochastic L-system from Lindenmayer for a 3D-ish plant:
// axiom: X
// rules: X → F+[[X]-X]-F[-FX]+X (or one of several variants)
// F → FF
// alphabet: F (forward), +/- (rotate), [/] (push/pop turtle state)
const RULES = random([
{ X: 'F+[[X]-X]-F[-FX]+X', F: 'FF' },
{ X: 'F[+X]F[-X]+X', F: 'FF' },
{ X: 'F-[[X]+X]+F[+FX]-X', F: 'FF' },
]);
const ANGLE = random(20, 28) * Math.PI / 180;
let str = 'X';
const iters = 5;
for (let i = 0; i < iters; i++) {
let next = '';
for (const ch of str) next += RULES[ch] ?? ch;
str = next;
}
const step = s * 0.014;
strokeWeight(s * 0.0017);
// Turtle starts at the bottom center, heading up (−Y).
let x = width / 2;
let y = height * 0.98;
let theta = -Math.PI / 2;
const stack = [];
for (const ch of str) {
if (ch === 'F') {
const nx = x + Math.cos(theta) * step;
const ny = y + Math.sin(theta) * step;
line(x, y, nx, ny);
x = nx; y = ny;
} else if (ch === '+') {
theta += ANGLE + (Math.random() - 0.5) * 0.04;
} else if (ch === '-') {
theta -= ANGLE + (Math.random() - 0.5) * 0.04;
} else if (ch === '[') {
stack.push([x, y, theta]);
} else if (ch === ']') {
[x, y, theta] = stack.pop();
}
}
}仕組み
記号のアルファベット・公理(初期文字列)・書き換え規則を定義し、生成→描画する:(1) 公理から始め、規則で全記号を同時に置換、これを n 世代繰り返す(文字列は多くは指数的に伸びる)、(2) タートルグラフィックスとして解釈 — F=前進して描画、+/−=固定角の回転、[/]=タートル状態の退避/復帰(枝分かれを実現)。例:公理 F、規則 F→F[+F]F[−F]F で数反復後に茂みが得られる。確率的規則で変化を、パラメトリック/文脈依存 L-system で成長や葉脈をモデル化できる。
パラメータ
axiom: string — The initial string the rewriting starts from.rules: string — Production rules that replace each symbol on every pass.iterations: number — Number of rewriting passes; structure grows exponentially in this value.angle: number — Turn angle applied when the turtle interprets + / − symbols.stepLength: number — Forward distance drawn per F symbol.
系譜
よく組み合わされるアルゴリズム
このアルゴリズムを使う作品 (2)
参考文献
最終更新 2026-07-16