Difference between revisions of "Koch curve"

From JSXGraph Wiki
Jump to navigationJump to search
Line 5: Line 5:
 
<script type="text/javascript" src="http://jsxgraph.uni-bayreuth.de/distrib/jsxturtle.js"></script>
 
<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;">
 
<form><textarea id="inputtext" rows=3 cols=35 wrap="off" style="width:600px;">
function koch(x) {
+
function koch(x,level) {
     if (x<1) {
+
     if (level<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 23:
 
t.setPos(-250,0);
 
t.setPos(-250,0);
 
t.rt(90);
 
t.rt(90);
koch(400);
+
koch(400,7);
 
          
 
          
 
</textarea><br />
 
</textarea><br />
Line 29: Line 29:
 
<input type="button" value="clear" onClick="clearturtle()">
 
<input type="button" value="clear" onClick="clearturtle()">
 
</form>
 
</form>
</html>
 
===Output===
 
<html>
 
 
<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">
Line 52: Line 49:
 
===References===
 
===References===
 
This example is from
 
This example is from
* [http://de.wikipedia.org/wiki/Logo_(Programmiersprache) http://de.wikipedia.org/wiki/Logo_(Programmiersprache)]
+
* [http://www.mathcurve.com/fractals/koch/koch.shtml http://www.mathcurve.com/fractals/koch/koch.shtml]
 +
 
 +
===Source code===
 +
<source lang="javascript">
 +
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]]

Revision as of 17:02, 21 December 2008


References

This example is from

Source code

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);