Koch curve: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<html>
<html>
<link rel="stylesheet" type="text/css" href="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraph.css" />
<form><textarea id="inputtext" rows=10 cols=35 wrap="off" style="width:600px;">
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script>
function koch(x,level) {
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script>
     if (level<1) {
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxturtle.js"></script>
<form><textarea id="inputtext" rows=3 cols=35 wrap="off" style="width:600px;">
function koch(x) {
     if (x<1) {
         t.fd(x);
         t.fd(x);
     } else {
     } else {
         koch(x/3);
         koch(x/3,level-1);
         t.lt(60);
         t.lt(60);
         koch(x/3);
         koch(x/3,level-1);
         t.rt(120);
         t.rt(120);
         koch(x/3);
         koch(x/3,level-1);
         t.lt(60);
         t.lt(60);
         koch(x/3);
         koch(x/3,level-1);
     }
     }
}
}
Line 23: Line 19:
t.setPos(-250,0);
t.setPos(-250,0);
t.rt(90);
t.rt(90);
koch(400);
koch(400,7);
          
          
</textarea><br />
</textarea><br />
Line 30: Line 26:
</form>
</form>
</html>
</html>
===Output===
<jsxgraph width="600" height="400" box="box">
<html>
var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -100]});
<div id="box" class="jxgbox" style="width:600px; height:600px;"></div>
var t = brd.create('turtle');
<script language="JavaScript">
var brd = JXG.JSXGraph.initBoard('box', {originX: 300, originY: 300, unitX: 1, unitY: 1});
var t = new JSXTurtleObj(brd);
function run() {
function run() {
   brd.suspendUpdate();
   brd.suspendUpdate();
   var code = $('inputtext').value;
   var code = document.getElementById('inputtext').value;
   if (code=='') { return; }
   if (code=='') { return; }
   eval(code);
   eval(code);
Line 47: Line 40:
}
}
run();
run();
</script>
</jsxgraph>
</html>


===References===
===References===
This example is from
* [http://www.mathcurve.com/fractals/koch/koch.shtml http://www.mathcurve.com/fractals/koch/koch.shtml]
* [http://de.wikipedia.org/wiki/Logo_(Programmiersprache) http://de.wikipedia.org/wiki/Logo_(Programmiersprache)]
 
===Source code===
<source lang="javascript">
var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -100]});
var t = brd.create('turtle');
function run() {
  brd.suspendUpdate();
  var code = document.getElementById('inputtext').value;
  if (code=='') { return; }
  eval(code);
  brd.unsuspendUpdate();
}
function clearturtle() {
  t.cs();
}
run();
 
function koch(x,level) {
    if (level<1) {
        t.fd(x);
    } else {
        koch(x/3,level-1);
 
        t.lt(60);
        koch(x/3,level-1);
        t.rt(120);
        koch(x/3,level-1);
        t.lt(60);
        koch(x/3,level-1);
    }
}
 
t.cs();
t.hideTurtle();
t.setPos(-250,0);
t.rt(90);
koch(400,7);
</source>


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

Latest revision as of 07:58, 8 June 2011


References

Source code

var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -100]});
var t = brd.create('turtle');
function run() {
  brd.suspendUpdate();
  var code = document.getElementById('inputtext').value;
  if (code=='') { return; }
  eval(code);
  brd.unsuspendUpdate();
}
function clearturtle() {
  t.cs();
}
run();

function koch(x,level) {
    if (level<1) {
        t.fd(x);
    } else {
        koch(x/3,level-1);

        t.lt(60);
        koch(x/3,level-1);
        t.rt(120);
        koch(x/3,level-1);
        t.lt(60);
        koch(x/3,level-1);
    }
}

t.cs();
t.hideTurtle();
t.setPos(-250,0);
t.rt(90);
koch(400,7);