cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              // the alphabet of possible symbols:
var alphabet = "FFFfff<>+-|=".split("");
var mutation_rate = 0.2;
var genome_size = 20;

// create a graphics turtle:
var turtle = {};

function turtle_reset(t) {
  t.pos = new vec2();
  t.dir = new vec2(0, 1);
  t.spin = 1;
}

turtle_reset(turtle);

function turtle_clone(t) {
  return {
    pos: t.pos.clone(),
    dir: t.dir.clone(),
    spin: t.spin,
  };
}

var angle1 = Math.PI / 2;
var angle2 = Math.PI / 3;

var uid = (function() {
  var id = 0;
  return function() {
    return "t" + (id++);
  }
})();

// generate drawing code from the turtle code
// "out" is an array to be filled with lines of JavaScript code
// "code" is the turtle code 
// "t" is the variable name of the current turtle
function turtle_code_generate(out, code, t) {
  for (var i = 0; i < code.length; i++) {
    var c = code.substr(i, 1);
    //out.push("// " + c);
    if (c == "F") {
      // draw a line
      out.push("lines.push("+t+".pos);");
      out.push(t + ".pos = " + t + ".dir.clone().add(" + t + ".pos)");
      out.push("lines.push("+t+".pos);");
      
    } else if (c == "f") {
      // draw a short line
      out.push("lines.push("+t+".pos);");
      out.push(t + ".pos = " + t + ".dir.clone().mul(0.5).add(" + t + ".pos)");
      out.push("lines.push("+t+".pos);");
    } else if (c == "+") {
      // turn turtle:
      out.push(t + ".dir.rotate(" + t + ".spin * angle1);");
    } else if (c == "-") {
      // turn turtle:
      out.push(t + ".dir.rotate(-" + t + ".spin * angle1);");
    } else if (c == ">") {
      // turn turtle:
      out.push(t + ".dir.rotate(" + t + ".spin * angle2);");
    } else if (c == "<") {
      // turn turtle:
      out.push(t + ".dir.rotate(-" + t + ".spin * angle2);");
    } else if (c == "|") {
      // mirror turtle:
      var t1 = uid();
      out.push(t1 + " = { pos: " + t + ".pos.clone(), dir: " + t + ".dir.clone(), spin: -" + t + ".spin };");
      var code1 = code.substring(i + 1);
      turtle_code_generate(out, code1, t1);
    } else if (c == "=") {
      // flip it around:
      var t1 = uid();
      out.push(t1 + " = { pos: " + t + ".pos.clone(), dir: " + t + ".dir.clone().negate(), spin: " + t + ".spin };");
      var code1 = code.substring(i + 1);
      turtle_code_generate(out, code1, t1);
    }
  }
}

function turtle_code(code) {
  var out = ["// turtle:"]; // container of lines of code
  turtle_code_generate(out, code, "t0");
  out = out.join("\n");
  //console.log(out);
  try {
    var f = new Function("t0", "lines", out);
    return f;
  } catch(e) {
    console.log(e);
  } 
}

// now the population:
var pop = [];
for (var i = 0; i < 8; i++) {
  var g = "";
  for (var j = 0; j < genome_size; j++) {
    g += alphabet[random(alphabet.length)];
  }
  pop.push({
    code: g,
    draw: turtle_code(g)
  });
}

function geno_child(parent) {
  var g = parent.code;

  if (random() < 0.1) {
    // reshuffle:
    var cut0 = random(g.length);
    var cut1 = random(g.length);
    var a = g.slice(0, cut0);
    var b = g.slice(cut1);
    g = b + a;
  } else {
    // mutate one gene:
    var c = alphabet[random(alphabet.length)];
    var cut = random(g.length);
    g = g.substring(0, cut) + c + g.substring(cut + 1);
  }
  g.draw = turtle_code(g);
  return {
    code: g,
    draw: turtle_code(g)
  }
}

function update() {
  angle1 = Math.PI / 3 * Math.sin(now);
  angle2 = Math.PI / 8 * Math.cos(4 * now);
}

function draw() {
  for (var i = 0; i < pop.length; i++) {
    var g = pop[i];

    draw2D.push()
      .translate((0.5 + i) / pop.length, 0.5)
      .scale(0.01);

    turtle_reset(turtle);

    write(i, g.code);
    var lines = [];
    g.draw(turtle, lines);
    
    draw2D.lines(lines);
    
    draw2D.pop();
  }
}

function mouse(e, p) {
  if (e == "down") {
    var choice = Math.floor(p[0] * pop.length);
    var parent = pop[choice];
    for (var i = 0; i < pop.length; i++) {
      pop[i] = geno_child(parent);
    }
  }
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console