Turtle Graphics: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
 
(66 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This is a very basic implementation of turtle graphics in JavaScript with [http://jsxgraph.org JSXGraph].
[http://u2d.com/turtle_js/index.html CanvasTurtle] does the same on browsers which support the ''canvas'' element.
* [[List of available commands]]
===Snowflake and Branches Example===
<html>
<html>
<link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" />
<form><textarea id="input1" rows=7 cols=35 wrap="off" style="width:300px; float:left;">
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script>
function side(size, level) {
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script>
    if (level==0) {
<form><textarea id="input" rows=7 cols=35 wrap="off" style="width:600px">
        t.fd(size);
</textarea><br />
        return;
<input type="button" value="run" onClick="run()">
    }
</form>
    side(size/3, level-1);
<div id="box" class="jxgbox" style="width:600px; height:600px;"></div>
    t.lt(60);
<script language="JavaScript">
    side(size/3, level-1);
            var turtleObj = function(board,attributes) {
    t.rt(120);
                this.board = board;
    side(size/3, level-1);
                if (attributes==null) {
    t.lt(60);
                    this.attributes = {
    side(size/3, level-1);
                        strokeColor:'#000000'
}
                    };
                } else {
                    this.attributes = attributes;
                }
                this.attributes.straightFirst = false;
                this.attributes.straightLast = false;
                this.init();
            };


            turtleObj.prototype.init = function() {
function snowflake(size, level) {
                this.points = [];
    for (var i=0;i<3;i++) {
                this.pos = [0,0];
        side(size, level);
                this.isPenDown = true;
        t.rt(120);
                this.dir = 90;
    };
                var p = this.board.createElement('point',this.pos,{fixed:true,name:' ',visible:false});
}
                this.points.push(p);
                this.turtle = this.board.createElement('point',this.pos,{fixed:true,name:' ',visible:false});
                this.turtle2 = this.board.createElement('point',[this.pos[0],this.pos[1]+20],{fixed:true,name:' ',visible:false});
                this.board.createElement('line',[this.turtle,this.turtle2],
                        {lastArrow:true,strokeColor:'#ff0000',straightFirst:false,straightLast:false});
            }


            turtleObj.prototype.forward = function(len) {
t.clearScreen();
                if (len==0) { return; }
t.hideTurtle();
                var dx = -len*Math.cos(this.dir*Math.PI/180.0);
t.setPenSize(1)
                var dy = len*Math.sin(this.dir*Math.PI/180.0);
t.setPenColor("#000000")
                var t = this.board.createElement('transform', [dx,dy], {type:'translate'});
t.lt(30);
                t.applyOnce(this.turtle);
t.setPos(0,-100);
                t.applyOnce(this.turtle2);
snowflake(250, 4);
                this.pos[0] += dx;
</textarea>
                this.pos[1] += dy;
<textarea id="input2" rows=7 cols=35 wrap="off" style="width:300px">
                var p = this.board.createElement('point',this.pos,{fixed:true,name:' ',visible:false});
function branch(length, level)
                this.points.push(p);
{
    if(level == 0) return
    t.fd(length)
    t.lt(45)
    branch(length/2, level-1)
    t.rt(90)
    branch(length/2, level-1)
    t.lt(45)
    t.bk(length)
}


                if (this.isPenDown) {
function lbranch(length, angle, level)
                    this.board.createElement('line',[this.points[this.points.length-2],p],this.attributes);
{
                }
    t.fd(2*length)
                this.board.update();
    node(length, angle, level)
            };
    t.bk(2*length)
           
}
            turtleObj.prototype.back = function(len) {
function rbranch(length, angle, level)
                this.forward(-len);
{
            }
    t.fd(length)
           
    node(length, angle, level)
            turtleObj.prototype.right = function(angle) {
    t.bk(length)
                this.dir += angle;
}
                var t = this.board.createElement('transform', [-angle*Math.PI/180.0,this.turtle], {type:'rotate'});
                t.applyOnce(this.turtle2);
                this.board.update();
            }
           
            turtleObj.prototype.left = function(angle) {
                this.right(-angle);
            }
           
            turtleObj.prototype.penUp = function() {
                this.isPenDown = false;
            }
           
            turtleObj.prototype.penDown = function() {
                this.isPenDown = true;
            }


            turtleObj.prototype.clear = function() {
function node(length, angle, level)
                for(var el in this.board.objects) {
{
                    this.board.removeObject(el);
    if (level == 0) return;
                }
    t.lt(angle)
                this.init();
    lbranch(length, angle, level -1)
            }
    t.rt(2*angle)
    rbranch(length, angle, level-1)
    t.lt(angle)
}


            turtleObj.prototype.fd = function(len) { this.forward(len); };
t.clearScreen()
            turtleObj.prototype.bk = function(len) { this.back(len); };
t.hideTurtle();
            turtleObj.prototype.lt = function(angle) { this.left(angle); };
//branch(100,6)
            turtleObj.prototype.rt = function(angle) { this.right(angle); };


            var brd = JXG.JSXGraph.initBoard('box', {originX: 300, originY: 300, unitX: 1, unitY: 1});
t.setPenSize(5)
            var t = new turtleObj(brd);
t.setPenColor("#008800")
</script>
t.setPos(30, -150)
lbranch(25, 20, 7)
</textarea><br />
<input type="button" value="run example 1" onClick="run(1)">
<input type="button" value="run example 2" onClick="run(2)">
</form>
</html>
</html>
<jsxgraph width="600" height="600" box="box">
var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -300]});
var t = brd.create('turtle');
function run(nr) {
  brd.suspendUpdate();
  eval(document.getElementById('input'+nr).value);
  brd.unsuspendUpdate();
}
</jsxgraph>
===References===
* The snowflake and branches example have been adapted from the excellent [http://u2d.com/turtle_js/index.html CanvasTurtle]
===The turtle graphics code===
<source lang="html4strict">
<form><textarea id="input1" rows=7 cols=35 wrap="off" style="width:300px; float:left;">
turtle code...
</textarea>
<input type="button" value="run example 1" onClick="run(1)">
</source>
<source lang="javascript">
var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -300]});
var t = brd.create('turtle');
function run(nr) {
  brd.suspendUpdate();
  eval($('input'+nr).value);
  brd.unsuspendUpdate();
}
</source>


[[Category:Examples]]
[[Category:Examples]]
[[Category:Turtle Graphics]]
[[Category:Fractals]]

Latest revision as of 07:40, 9 June 2011

This is a very basic implementation of turtle graphics in JavaScript with JSXGraph. CanvasTurtle does the same on browsers which support the canvas element.

Snowflake and Branches Example


References

  • The snowflake and branches example have been adapted from the excellent CanvasTurtle

The turtle graphics code

<form><textarea id="input1" rows=7 cols=35 wrap="off" style="width:300px; float:left;">
turtle code...
</textarea>
<input type="button" value="run example 1" onClick="run(1)">
var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -300]});
var t = brd.create('turtle');

function run(nr) {
  brd.suspendUpdate();
  eval($('input'+nr).value);
  brd.unsuspendUpdate();
}