Koch curve: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
 
(9 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" />
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/prototype.js"></script>
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxgraphcore.js"></script>
<form><textarea id="inputtext" rows=10 cols=35 wrap="off" style="width:600px;">
<form><textarea id="inputtext" rows=10 cols=35 wrap="off" style="width:600px;">
function koch(x,level) {
function koch(x,level) {
Line 28: Line 25:
<input type="button" value="clear" onClick="clearturtle()">
<input type="button" value="clear" onClick="clearturtle()">
</form>
</form>
<div id="box" class="jxgbox" style="width:600px; height:600px;"></div>
</html>
<script language="JavaScript">
<jsxgraph width="600" height="400" box="box">
var brd = JXG.JSXGraph.initBoard('box', {originX: 300, originY: 300, unitX: 1, unitY: 1});
var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -100]});
var t = brd.createElement('turtle');
var t = brd.create('turtle');
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 43: Line 40:
}
}
run();
run();
</script>
</jsxgraph>
</html>


===References===
===References===
Line 51: Line 47:
===Source code===
===Source code===
<source lang="javascript">
<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) {
function koch(x,level) {
     if (level<1) {
     if (level<1) {
Line 56: Line 66:
     } else {
     } else {
         koch(x/3,level-1);
         koch(x/3,level-1);
         t.lt(60);
         t.lt(60);
         koch(x/3,level-1);
         koch(x/3,level-1);
Line 73: Line 84:


[[Category:Examples]]
[[Category:Examples]]
[[Category:Turtle Graphics]]
[[Category:Turtle Graphics]]
[[Category:Fractals]]
[[Category:Fractals]]
[[Category:Curves]]
[[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);