# dobbs/turtle

1 parent 53820b7 commit 330e0ee310ea97d108a3f864b22ad444cbd914be committed Dec 22, 2011
Showing with 273 additions and 0 deletions.
1. +57 −0 tg.html
2. +216 −0 tg.js
57 tg.html
 @@ -0,0 +1,57 @@ + + +Turtle Geometry + + + + + + +
+

Turtle Geometry exercises

+ +
+► History
+
+
+

+ Exercises and examples from chapter one of + + Turtle Geometry by Abelson and diSessa + + implemented in javascript. Kinda blown away by this book: only on + chapter one and this page already offers a pretty fun playground for + exploring geometry and rudimentary graphics programming. +

+

+ Click some of the links below for examples and inspiration. You can + also type commands into the form below the turtle's sandbox. + Commands you type will be saved in the history below the sandbox. +

+

+ Pen controls include penup, pendown, + pensize pixels, and pencolor + color. Any CSS color name or hex value will work + for color: + Wikipedia: Web Colors +

+

+ Have a look at the source code for these exercises if you like: + tg.js +

+
+
+
+ +
216 tg.js
 @@ -0,0 +1,216 @@ +var tg = tg || {}; +(function (\$, Turtle, window, document, undefined) { + + function init(that) { + \$.extend(that, { + thing: function thing() { + var segments = [100, 100, 50, 50, 100, 25, 25]; + \$.each(segments, function (i, pixels) { + that.move(pixels).turn(90); + }); + that.move(50); + }, + + arcr: function arcr(r, deg) { + for(var i = 0; i < deg; i++) { + that.move(r).turn(1); + } + return that; + }, + + arcl: function arcl(r, deg) { + for(var i = 0; i < deg; i++) { + that.move(r).turn(-1); + } + return that; + }, + + arc: function arc(r, deg, ccw) { + var step = 2*Math.PI*r/360; + for(var i = 0; i < deg; i++) { + that.move(step).turn(ccw ? -1 : 1); + } + return that; + }, + + poly: function poly(side, angle) { + var sum = 0; + do { + that.move(side).turn(angle); + sum = sum + parseInt(angle); + } while (sum % 360 != 0); + return that; + }, + + newpoly: function newpoly(side, angle, reps) { + if (!reps) + reps = 360 / (3*parseInt(angle)); + for (var i=0; i'); + var command = tg.examples[example]; + \$link.html('' + example + ' ' + command); + \$link.click(function (e) { + e.preventDefault(); + \$commandline.val(command); + dispatchChangeEvent(\$commandline); + return false; + } ); + \$exercises.append(\$link); + }); + + tg.T = init(Turtle.interactiveTurtle); + + setTimeout(function flowerAndStem () { + tg.T.pensize(6).pencolor('green').turn(45).arc(100, 60).arc(100, 60, 'ccw'); + tg.T.home().pensize(12).pencolor('red'); + for(var i=0; i<5; i++) + tg.T.arc(80, 60).turn(120).arc(80, 60).turn(120+72); + tg.T.penup().turn(-90).move(150).turn(90).pendown().pencolor('blue').pensize(1); + }, 100); + }); + + + +})(jQuery, Turtle, this, this.document);