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.

            
              var lexicon = "FFFFffffff+++--->>><<<rw|=.".split("");
var code = "";
var steplen = 1/40;
var turnangle1 = Math.PI / 2;
var turnangle2 = Math.PI / 2;
var genome_size = 25;
var pop_grid_size = 3;
var pop_size = pop_grid_size*pop_grid_size;
var pop = [];
var t0 = now;

function make_genome() {
  var g = [];
  for (var i = 0; i < genome_size; i++) {
    var gene = lexicon[random(lexicon.length)];
    g.push(gene);
  }
  return g.join("");
}

function reset() {
  for (var i=0; i<pop_size; i++) {
    var o = {
      genome: make_genome()
    }
    write(i, o.genome);
    pop.push(o);
  }
  
  
  code = make_genome()
  console.log(code);
}
reset();

var which = 0;
function mouse(e, p) {
  var c = Math.floor( p[0] * pop_grid_size );
  var r = Math.floor( p[1] * pop_grid_size );
  var i = c + pop_grid_size * r;
  which = i;
  if (e == "down") {
    //reset();
    t0 = now;
    
    var parent = pop[i];
    
    for (var j=0; j<pop_size; j++) {
      if (i == j) continue;
      
      // get parent genomes:
      var parent1_genome = parent.genome;
      var parent2_genome = pop[j].genome;
      // 50% chance of swapping parents around:
      if (random(2) == 1) {
        var tmp = parent1_genome;
        parent1_genome = parent2_genome;
        parent2_genome = tmp;
      }
      // insert a segment of parent 2 into parent 1:
      // pick two cut points
      var cut1 = random(parent1_genome.length);
      var cut2 = random(parent2_genome.length);
      // ensure cut1 <= cut2
      if (cut1 > cut2) {
        var tmp = cut1;
        cut1 = cut2;
        cut2 = tmp;
      }
      // get the cut sections
      var sec1 = parent2_genome.substring(0, cut1);
      var sec2 = parent1_genome.substring(cut1, cut2);
      var sec3 = parent2_genome.substring(cut2);
      // and join:
      child = sec1 + sec2 + sec3;
      
      // chance of randomizing genome:
      if (random() < 1/pop_size) {
        var list = child.split("");
        list = shuffle(list);
        child = list.join("");
      }
      
      // mutate a gene:
      var mutation_rate = 1/child.length;
      var mutated = [];
      for (k=0; k<child.length; k++) {
        if (random() < mutation_rate) {
          var gene = lexicon[random(lexicon.length)];
          mutated.push(gene);
        } else {
          mutated.push(child[k]);
        }
      }
      child = mutated.join("");
      
      // replace in population:
      pop[j].genome = child;
    }
  }
}

function update() {
  turnangle1 = Math.PI * (1.5+Math.cos(now)) / 8;
  turnangle2 = Math.PI * (1.5+Math.sin(now * 4)) / 16;
}

function interpret(turtle, code) {
  for (var i = 0; i < code.length; i++) {
    var c = code[i];
    switch (c) {
      case "=":
        // fork another turtle with flipped chirality
        var t1 = {
          pos: turtle.pos.clone(),
          dir: turtle.dir.clone().negate(),
          chirality: turtle.chirality,
          color: turtle.color,
        };
        // run the rest of the string on it too:
        var remain = code.substring(i + 1);
        // run the second turtle:
        interpret(t1, remain);
        break;
      case "|":
        // fork another turtle with flipped chirality
        var t1 = {
          pos: turtle.pos.clone(),
          dir: turtle.dir.clone(),
          chirality: -turtle.chirality,
          color: turtle.color,
        };
        // run the rest of the string on it too:
        var remain = code.substring(i + 1);
        // run the second turtle:
        interpret(t1, remain);
        break;
      case "r":
        turtle.color = "red";
        break;
      case "w":
        turtle.color = "white";
        break;
      case ".":
        draw2D.color(turtle.color);
        draw2D.rect(turtle.pos, steplen);
        break;
      case "F":
        // move forward drawing a line
        var p2 = turtle.dir.clone().mul(steplen).add(turtle.pos);
        draw2D.color(turtle.color);
        draw2D.line(turtle.pos, p2);
        turtle.pos = p2;
        break;
      case "f":
        // move forward drawing a line
        var p2 = turtle.dir.clone().mul(steplen * 0.5).add(turtle.pos);
        draw2D.color(turtle.color);
        draw2D.line(turtle.pos, p2);
        turtle.pos = p2;
        break;
      case "+":
        // turn positively
        turtle.dir.rotate(turtle.chirality * turnangle1);
        break;
      case "-":
        // turn the other way
        turtle.dir.rotate(turtle.chirality * -turnangle1);
        break;
      case ">":
        // turn positively
        turtle.dir.rotate(turtle.chirality * turnangle2);
        break;
      case "<":
        // turn the other way
        turtle.dir.rotate(turtle.chirality * -turnangle2);
        break;

    }
  }
}

function draw() {
  
  var age = Math.floor((now - t0) * 10);
  
  for (var i=0; i<pop_size; i++) {
    var o = pop[i];
    write(i, o.genome);
    
    draw2D.push()
      .scale(1/pop_grid_size)
      .translate(i%pop_grid_size, Math.floor(i/pop_grid_size))
      .translate(0.5, 0.5);
    
    if (i == which) {
      draw2D.color(0.2, 0.2, 0.2);
      draw2D.rect();
    }
    
    var turtle = {
      pos: new vec2(),
      dir: new vec2(0, 1),
      chirality: 1,
      color: "white",
    };
    interpret(turtle, o.genome.substring(0, age));

    draw2D.pop();
   }
}
            
          
!
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