Turtle snow fall: Difference between revisions
From JSXGraph Wiki
A WASSERMANN (talk | contribs) No edit summary  | 
				No edit summary  | 
				||
| (20 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
<html>  | <html>  | ||
<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 side(size, level) {  | function side(size, level) {  | ||
| Line 20: | Line 16: | ||
function snowflake(size, level) {  | function snowflake(size, level) {  | ||
     (3  |      for (var i=0; i<3; i++) {  | ||
         side(size, level);  |          side(size, level);  | ||
         t.rt(120);  |          t.rt(120);  | ||
     }  |      }  | ||
}  | }  | ||
function snow() {  | function snow() {  | ||
    t.setPos(Math.random()*300  |    counter++;  | ||
    snowflake(  |    if (counter>500) {  | ||
    active = setTimeout(snow,  |       counter = 0;  | ||
      t.cs();  | |||
   }  | |||
    t.setPos(Math.random()*600-300,Math.random()*600-300);  | |||
    snowflake(50*Math.random(),Math.floor(Math.random()*4));  | |||
    active = setTimeout(snow,200);  | |||
}  | }  | ||
t.setPenColor('#ffff00');  | |||
t.ht();  | |||
counter = 0;  | |||
snow();  | snow();  | ||
| Line 37: | Line 41: | ||
<input type="button" value="clear" onClick="clearturtle()">  | <input type="button" value="clear" onClick="clearturtle()">  | ||
</form>  | </form>  | ||
<  | </html>  | ||
<jsxgraph width="600" height="600" box="box">  | |||
var brd = JXG.JSXGraph.initBoard('box', {  | document.getElementById('box').style.backgroundColor = 'black';  | ||
var t =   | var brd = JXG.JSXGraph.initBoard('box', {boundingbox: [-300, 300, 300, -300]});  | ||
var t = brd.create('turtle');  | |||
function run() {  | function run() {  | ||
   brd.suspendUpdate();  |    brd.suspendUpdate();  | ||
   var code =   |    var code = document.getElementById('inputtext').value;  | ||
   if (code=='') { return; }  |    if (code=='') { return; }  | ||
   eval(code);  |    eval(code);  | ||
| Line 49: | Line 54: | ||
}  | }  | ||
function clearturtle() {  | function clearturtle() {  | ||
  clearTimeout(active);  | |||
   t.cs();  |    t.cs();  | ||
}  | }  | ||
run();  | run();  | ||
</  | </jsxgraph>  | ||
===References===  | ===References===  | ||
* [http://www.mathcurve.com/fractals/koch/koch.shtml http://www.mathcurve.com/fractals/koch/koch.shtml]  | * [http://www.mathcurve.com/fractals/koch/koch.shtml http://www.mathcurve.com/fractals/koch/koch.shtml]  | ||
===The source code===  | |||
<source lang="javascript">  | |||
function side(size, level) {  | |||
    if (level==0) {  | |||
        t.fd(size);  | |||
    } else {  | |||
        side(size/3, level-1);  | |||
        t.lt(60);  | |||
        side(size/3, level-1);  | |||
        t.rt(120);  | |||
        side(size/3, level-1);  | |||
        t.lt(60);  | |||
        side(size/3, level-1);  | |||
    }  | |||
}  | |||
function snowflake(size, level) {  | |||
    for (var i=0; i<3; i++) {  | |||
        side(size, level);  | |||
        t.rt(120);  | |||
    }  | |||
}  | |||
t.ht();  | |||
function snow() {  | |||
   counter++;  | |||
   if (counter>500) {  | |||
      counter = 0;  | |||
      t.cs();  | |||
   }  | |||
   t.setPos(Math.random()*600-300,Math.random()*600-300);  | |||
   snowflake(30*Math.random(),Math.floor(Math.random()*4));  | |||
   active = setTimeout(snow,200);  | |||
}  | |||
t.setPenColor('#ffff00');  | |||
counter = 0;  | |||
snow();</source>  | |||
[[Category:Examples]]  | [[Category:Examples]]  | ||
[[Category:Turtle Graphics]]  | [[Category:Turtle Graphics]]  | ||
[[Category:Fractals]]  | [[Category:Fractals]]  | ||
Latest revision as of 07:40, 9 June 2011
References
The source code
function side(size, level) {
    if (level==0) {
        t.fd(size);
    } else {
        side(size/3, level-1);
        t.lt(60);
        side(size/3, level-1);
        t.rt(120);
        side(size/3, level-1);
        t.lt(60);
        side(size/3, level-1);
    }
}
function snowflake(size, level) {
    for (var i=0; i<3; i++) {
        side(size, level);
        t.rt(120);
    }
}
t.ht();
function snow() {
   counter++;
   if (counter>500) {
      counter = 0;
      t.cs();
   }
   t.setPos(Math.random()*600-300,Math.random()*600-300);
   snowflake(30*Math.random(),Math.floor(Math.random()*4));
   active = setTimeout(snow,200);
}
t.setPenColor('#ffff00');
counter = 0;
snow();