Turtle Graphics: Difference between revisions
From JSXGraph Wiki
| A WASSERMANN (talk | contribs)  New page: <html> <link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" /> <script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/pro... | A WASSERMANN (talk | contribs) No edit summary | ||
| Line 4: | Line 4: | ||
| <script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script> | <script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script> | ||
| <form><textarea id="input" rows=7 cols=35 wrap="off" > | <form><textarea id="input" rows=7 cols=35 wrap="off" > | ||
| </textarea | </textarea> | ||
| <input type="button" value="plot" onClick=" | <input type="button" value="plot" onClick="run()" style='margin-right:1em'>   | ||
| </form> | </form> | ||
| <div id="box" class="jxgbox" style="width:600px; height:600px;"></div> | <div id="box" class="jxgbox" style="width:600px; height:600px;"></div> | ||
| <script language="JavaScript"> | <script language="JavaScript"> | ||
|             var turtleObj = function(board,attributes) { | |||
|                 this.board = board; | |||
|                 if (attributes==null) { | |||
|                     this.attributes = { | |||
|                         strokeColor:'#000000' | |||
|                     }; | |||
|                 } else { | |||
|                     this.attributes = attributes; | |||
|                 } | |||
|                 this.attributes.straightFirst = false; | |||
|                 this.attributes.straightLast = false; | |||
|                 this.init(); | |||
|             }; | |||
|             turtleObj.prototype.init = function() { | |||
|                 this.points = []; | |||
|                 this.pos = [0,0]; | |||
|                 this.isPenDown = true; | |||
|                 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) { | |||
|                 if (len==0) { return; } | |||
|                 var dx = -len*Math.cos(this.dir*Math.PI/180.0); | |||
|                 var dy = len*Math.sin(this.dir*Math.PI/180.0); | |||
|                 var t = this.board.createElement('transform', [dx,dy], {type:'translate'}); | |||
|                 t.applyOnce(this.turtle); | |||
|                 t.applyOnce(this.turtle2); | |||
|                 this.pos[0] += dx; | |||
|                 this.pos[1] += dy; | |||
|                 var p = this.board.createElement('point',this.pos,{fixed:true,name:' ',visible:false}); | |||
|                 this.points.push(p); | |||
|                 if (this.isPenDown) { | |||
|                     this.board.createElement('line',[this.points[this.points.length-2],p],this.attributes); | |||
|                 } | |||
|                 this.board.update(); | |||
|             }; | |||
|             turtleObj.prototype.back = function(len) { | |||
|                 this.forward(-len); | |||
|             } | |||
|             turtleObj.prototype.right = function(angle) { | |||
|                 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() { | |||
|                 for(var el in this.board.objects) { | |||
|                     this.board.removeObject(el); | |||
|                 } | |||
|                 this.init(); | |||
|             } | |||
|             turtleObj.prototype.fd = function(len) { this.forward(len); }; | |||
|             turtleObj.prototype.bk = function(len) { this.back(len); }; | |||
|             turtleObj.prototype.lt = function(angle) { this.left(angle); }; | |||
|             turtleObj.prototype.rt = function(angle) { this.right(angle); }; | |||
|             var brd = JXG.JSXGraph.initBoard('jsxgbox', {originX: 300, originY: 300, unitX: 1, unitY: 1}); | |||
|             var t = new turtleObj(brd); | |||
| </script> | </script> | ||
| </html> | </html> | ||
| [[Category:Examples]] | [[Category:Examples]] | ||
Revision as of 15:28, 19 December 2008
