## 2016年4月30日土曜日

### JavaScript - Conditionals and Recursion(fractal, Koch Curve, snowflake, Paper.js)

Think Python (Allen B. Downey (著)、 O'Reilly Media)のChapter 5.(Conditionals and Recursion)のExercises 5-6(No. 1277)をJavaScriptで取り組んでみる。

Exercises 5-6(No. 1277)

コード(Emacs)

``` <!DOCTYPE html> <html> <head> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.9.25/paper-full.min.js"></script> <script type="text/paperscript" canvas="snowflake" src="sample.js"></script> </head> <body> <canvas id="snowflake" resize></canvas> <br> <button id="run0">run</button> </body> </html> ``` ``` /*jslint node : true, continue : true, devel : true, indent : 2, maxerr : 50, newcap : true, nomen : true, plusplus : true, regexp : true, sloppy : true, vars : false, white : true */ /*global document, Path, Point */ document.querySelector('#run0').onclick = function () { var path = {path: new Path(), start:new Point(50, 50), angle:0}, koch = function (path, length) { if (length < 3) { path.start += [length * Math.cos(path.angle), length * Math.sin(path.angle)]; path.path.lineTo(path.start); } else { length /= 3; koch(path, length); path.angle -= Math.PI / 3; koch(path, length); path.angle += 2 * Math.PI / 3; koch(path, length); path.angle -= Math.PI / 3; koch(path, length); } }, snowflake = function (path, length) { var i; for (i = 0; i < 3; i += 1) { koch(path, length); path.angle += 2 * Math.PI / 3; } }; path.path.strokeColor = 'black'; snowflake(path, 100); }; ```