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 = 25;
var segment_size = 0.01;

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

function turtle_reset(t) {
  t.pos = new vec2();
  t.dir = new vec2(0, segment_size);
  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 len1 = segment_size;
var len2 = segment_size/2;

// given a string of instructions, draw it:
function turtle_draw(t, code, lines) {
  for (var i = 0; i < code.length; i++) {
    var c = code.substr(i, 1);
    if (c == "F") {
      // drawline
      var p2 = t.dir.clone().len(len1).add(t.pos);
      //draw2D.line(t.pos, p2);
      lines.push(t.pos.clone());
      lines.push(p2);
      t.pos = p2;
    } else if (c == "f") {
      // drawline
      var p2 = t.dir.clone().len(len2).add(t.pos);
      //draw2D.line(t.pos, p2);
      lines.push(t.pos);
      lines.push(p2);
      t.pos = p2;
    } else if (c == "+") {
      // turn turtle:
      t.dir.rotate(t.spin * angle1);
    } else if (c == "-") {
      // turn turtle:
      t.dir.rotate(-t.spin * angle1);
    } else if (c == ">") {
      // turn turtle:
      t.dir.rotate(t.spin * angle2);
    } else if (c == "<") {
      // turn turtle:
      t.dir.rotate(-t.spin * angle2);
    } else if (c == "|") {
      // mirror turtle:
      var t1 = turtle_clone(t);
      // flip it around:
      t1.spin = -t1.spin;
      var code1 = code.substr(i + 1);
      turtle_draw(t1, code1, lines);
    } else if (c == "=") {
      // flip it around:
      var t1 = turtle_clone(t);
      t1.dir.negate();
      var code1 = code.substr(i + 1);
      turtle_draw(t1, code1, lines);
    }
  }
}


// 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({
    genome: g,
  });
}

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

  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);
  }
  return {
    genome: g
  };
}

function update() {
  angle1 = Math.PI / 3 * Math.sin(now);
  angle2 = Math.PI / 8 * Math.sin(4 * now);
  //len1 = segment_size * (1 + 0.25*Math.cos(3 * now));
  //len2 = segment_size * (0.5 + 0.125*Math.cos(15 * now));
}

function draw() {
  
  for (var i = 0; i < pop.length; i++) {
    var p = pop[i];
    write(i, p.genome);
    
    // position each phenotype:
    turtle_reset(turtle);
    turtle.pos.set((0.5 + i) / pop.length, 0.5);

    var lines = [];
    turtle_draw(turtle, p.genome, lines); 
    draw2D.lines(lines);
  }
}

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