Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //processing sketch, modded by fbz from an example from processing book
- //1d elementary cellular automata with tiff saving ability
- //handy script for output on a knitting machine
- //here is the scarf knit with this exact code: http://www.ravelry.com/projects/fbz/rule-110-scarf
- //Processing: A Programming Handbook for Visual Designers and Artists
- // http://processing.org/books/
- // http://processing.org/img/learning/Processing-Examples-002.zip
- int[] rules = { 0, 1, 1, 0, 1, 1, 1, 0 }; //which rule would you like?
- int gen = 1; // Generation
- color on = color(255);
- color off = color(0);
- void setup() {
- size(61, 900); // width, length. knit scarf in up to 75 wide and 640 to 700 long.
- // ^^ add extra 10 to 15 stitches in the width for an industrial knitting machine ^^
- //frameRate(8); // Slow down to 8 frames each second
- background(0);
- set(width-1, 0, on); //set the next to last pixel to white, handy for rule 110
- set(4, 0, on); //turn on a second pixel for the seed row
- set(42, 0, on); //turn on another pixel for the seed row to white
- }
- void draw() {
- // For each pixel, determine new state by examining current
- // state and neighbor states and ignore edges that have only
- // one neighbor
- for (int i = 1; i < width - 1; i++) {
- int left = get(i - 1, gen - 1); // Left neighbor
- int me = get(i, gen - 1); // Current pixel
- int right = get(i + 1, gen - 1); // Right neighbor
- if (rules(left, me, right) == 1) {
- set(i, gen, on);
- }
- }
- gen++; // Increment the generation by 1
- if (gen > height - 1) { // If reached the bottom of the screen,
- noLoop(); // stop the program
- }
- save("1dca_rule110_0.tif"); //save a tiff image of output in sketch folder, rename by hand.
- }
- // Implement the rules
- int rules(color a, color b, color c) {
- if ((a == on) && (b == on) && (c == on)) {
- return rules[0];
- }
- if ((a == on) && (b == on) && (c == off)) {
- return rules[1];
- }
- if ((a == on) && (b == off) && (c == on)) {
- return rules[2];
- }
- if ((a == on) && (b == off) && (c == off)) {
- return rules[3];
- }
- if ((a == off) && (b == on) && (c == on)) {
- return rules[4];
- }
- if ((a == off) && (b == on) && (c == off)) {
- return rules[5];
- }
- if ((a == off) && (b == off) && (c == on)) {
- return rules[6];
- }
- if ((a == off) && (b == off) && (c == off)) {
- return rules[7];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement