デジモンアドベンチャー5話「電光!カブテリモン」で光子郎がX-Basic(と思われる言語)でテントモンをカブテリモンに進化させていた。
これが何をするものなのか気になって、JavaScriptに移植してみた。
fractalっていう再帰的関数を定義してあるからどんなかっこいい図が出来るんだろうと期待したけど、拍子抜けだった。
それにしても光子郎、小学4年生でこれは頭良すぎるんじゃないか。
今回始めてJavaScriptのPrototypeを弄った。
これは面白い。
図形描画にはHTML5のCanvasを使った。
実行結果
Firefox 3.6.10で動作確認。
1から2までのfloat値を入力する。
http://g-storage.appspot.com/share/eth0jp/digimon_evolution.html
JavaScript版
function Tentomon() {}; Tentomon.prototype = { s : 0, evolution : function() { this.s = 0; while (isNaN(this.s) || this.s<1 || this.s>=2) { this.s = parseFloat(prompt("ratio 1 to 2", "")); } this.s = (this.s - 1) / 10 + 1; this.screen(1, 2, 1, 1); this.s = Math.sqrt(this.s * this.s - 1); var x0 = 100, x1 = 412, y0 = 0, y1 = 0; this.fractal(x0, x1, y0, y1); this.line(100, 50, 412, 50, 255, 65535); }, fractal : function(x0, x1, y0, y1, sp) { var l, r, x2, y2; l = Math.sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0)) if (l<2 || sp>=0) { this.line(x0, y0/3+50, x1, y1/3+50, 255, 65535); return; } r = Math.random() + Math.random() + Math.random() - 2; x2 = (x0+x1)/2 + this.s * (y1-y0) * r; y2 = (y0+y1)/2 + this.s * (x1-x0) * r; sp = sp + 1; this.fractal(x0, x2, y0, y2, sp); this.fractal(x2, x1, y2, y1, sp); }, screen : function(size, mode, resolution, graphic) { var canvas = document.createElement('canvas'); var width = 512; var height = 512; canvas.id = "canvas"; canvas.setAttribute('width', width + 'px'); canvas.setAttribute('height', height + 'px'); canvas.style.cssText = 'position: absolute; top: 0; left: 0; z-index: 100;'; document.body.appendChild(canvas); }, line : function(x1, y1, x2, y2, p, ls) { var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); context.beginPath(); context.moveTo(x1, y1); context.lineTo(x2, y2); context.stroke(); } }; window.addEventListener('load', function(){new Tentomon().evolution();}, false);
X-Basic
X-Basicやった事ないからもしかしたら間違えてる部分あるかも。
100 /* func sample. coast creation */ 110 float s 120 while s<1 or s>=2 130 input "ratio 1 to 2";s 140 endwhile 150 s = (s - 1) / 10 + 1 160 screen 1, 2, 1, 1 170 s = sqr(s * s - 1) 180 float x0 = 100, x1 = 412, y0 = 0, y1 = 0 190 fractal(x0, x1, y0, y1) 200 line(100, 50, 412, 50, 255, 65535) 210 end 220 func fractal(x0:float, x1:float, y0:float, y1:float, sp:int) 230 float l, r, x2, y2 240 l = sqr((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0)) 250 if l<2 or sp>=0 then { 260 line(x0, y0/3+50, x1, y1/3+50, 255, 65535) ; return() 270 } 280 r = rnd() + rnd() + rnd() - 2 290 x2 = (x0+x1)/2 + s * (y1-y0) * r 300 y2 = (y0+y1)/2 + s * (x1-x0) * r 310 sp = sp + 1 320 fractal(x0, x2, y0, y2, sp) 330 fractal(x2, x1, y2, y1, sp) 340 endfunc