// 中点変位法の実験 // // ActionScript入門Wiki - 中点変位法で線を描く // http://web.archive.org/web/20111017065847/http://www40.atwiki.jp/spellbound/pages/1690.html float h; float threshold = 2; // 初期化 void setup() { size(512, 512); smooth(8); frameRate(2); h = height / 2; drawWorld(); } // 描画 void draw() { drawWorld(); } // 図形を描画 void drawWorld() { background(255); PVector a = new PVector(0, h); PVector b = new PVector(width - 1, h); divide(a, b, h, threshold); } // 中点変位法で分割していく void divide(PVector a, PVector b, float h, float threshold) { if (abs(a.x - b.x) <= threshold) { // 2点の距離が一定値より小さくなったら分割終了 drawQuad(a, b); // 四角形を描画 return; } // 中点を求める PVector c = interpolate(a, b, 0.5); // 中点の高さ y を基準にして // -h/2 から h/2 の範囲でランダムに高さを変更 c.y += random(-h/2, h/2); // 点Aと点Cで更に分割 divide(a, c, h / 2, threshold); // 点Cと点Bで更に分割 divide(c, b, h / 2, threshold); } // 2点の中点を求める PVector interpolate(PVector a, PVector b, float t) { float mx = a.x + (b.x - a.x) * t; float my = a.y + (b.y - a.y) * t; PVector r = new PVector(mx, my); return r; } // 四角形を描画 void drawQuad(PVector a, PVector b) { float m = (a.y + b.y) / 2; float c = (m / (height - 1)) * 255; noStroke(); fill(c); quad(a.x, a.y, b.x + 1, b.y, b.x + 1, height, a.x, height); }