L-systems: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
Line 31: Line 31:
var t = new JSXTurtleObj(brd);
var t = new JSXTurtleObj(brd);


function run() {
function expander(level,axiom,rules) {
  brd.suspendUpdate();
    this.rules = rules;
  var code = $('inputtext').value;
     this.source = (level>1) ? new expander(level-1,axiom,rules) : (new function(axiom) {
  if (code=='') { return; }
  t.cs();
  t.hideTurtle();
  eval(code);
  function expander(level) {
     this.source = (level>1) ? new expander(level-1) : (new function() {
         // Axiom:
         // Axiom:
         this.code = axiom;
         this.code = axiom;
        alert(this.code);
         this.pos = 0;
         this.pos = 0;
         this.next = function() {
         this.next = function() {
Line 56: Line 51:
             var pattern = this.source.next();
             var pattern = this.source.next();
             if (!pattern) return null // Finished
             if (!pattern) return null // Finished
             this.code = rules[pattern];
             this.code = this.rules[pattern];
         }
         }
         return this.code[this.pos++];
         return this.code[this.pos++];
     }
     }
  }
}
  var generator = new expander(level);
 
  function plotter(generator) {
function plotter(generator,symbols) {
     for (var c; c=generator.next(); c) {
     for (var c; c=generator.next(); c) {
         switch(symbols[c]) {
         switch(symbols[c]) {
Line 92: Line 87:
     }
     }
     return null;
     return null;
  }
}


   plotter(generator);
function run() {
  brd.suspendUpdate();
  var code = $('inputtext').value;
  if (code=='') { return; }
  t.cs();
  t.hideTurtle();
  eval(code);
  var generator = new expander(level,axiom,rules);
   plotter(generator,symbols);
   brd.unsuspendUpdate();
   brd.unsuspendUpdate();
}
}

Revision as of 18:39, 30 December 2008