1. //neighbors_140903b

     
  2. //neighbors_140903a

    Brought yesterday’s code in p5.js back to Processing (with some simplification and changes)


    ArrayList<PointPosition> pointPosition = new ArrayList();
    boolean alternate = false;
    int threshold = 135,
        height = 750,
        width = 500;
    int[] directions = {
        1,
        -1,
        width,
        -width,
        (width*-1)-1,
        (width*-1)+1,
        width+1,
        width-1
      };

    void setup() {
      size(width,height);
      background(255);
      frameRate(12);
      seedPoints(width,height,10);
    }

    void draw() {
      for(PointPosition p:pointPosition){
        //
        int neighbors = 0;
        for (int d=0; d<directions.length; d++) {
          int n = p.i+directions[d];
          if (n >= 0 && n <pointPosition.size()) {
            if (brightness(pointPosition.get(n).c) > threshold) {
              neighbors++;
            }
          }
        }
        if (neighbors > 1 && neighbors < 6) {
          if (brightness(p.c) < threshold) {
            p.c = color(
                    255-red(p.c),
                    255-green(p.c),
                    255-blue(p.c)
                    );
          }
        } else {
          if (brightness(p.c) > threshold) {
            p.c = color(0);
          }
        }
        //
        p.update(p.c);
        p.render();
      }
      threshold = setThreshold(threshold,50,225);
    }

    class PointPosition {
      int i,x,y,w,h;
      color c;
      void update(color nc){
        c = nc;
      }
      void render(){
        stroke(c);
        fill(c);
        rect(x,y,w,h);
      }
    }

    int setThreshold(int threshold, int minThreshold, int maxThreshold) {
      if (threshold <= maxThreshold && threshold >= minThreshold && alternate) {
        threshold++;
      } else if (threshold <= maxThreshold && threshold >= minThreshold) {
        threshold—;
      } else if (alternate) {
        threshold = maxThreshold;
        alternate = !alternate;
      } else {
        threshold = minThreshold;
        alternate = !alternate;
      }
      return threshold;
    }

    void seedPoints(int width,int height,int scale){
      int index = 0;
      for (int i=0; i<width/scale; i++) {
        for (int j=0; j<height/scale; j++) {
          index++;
          PointPosition p = new PointPosition();
            p.i = index;
            p.x = i*scale;
            p.y = j*scale;
            p.w = scale;
            p.h = scale;
            p.c = color(
                    int(map(i,0,width/scale,0,255)),
                    int(map(j,0,height/scale,0,255)),
                    int(map(index,0,width/scale*height/scale,0,255))
                    );
          pointPosition.add(p);
        }
      }
    }

     
  3. //neighbors_140902a

    var width = 500,
        height = 500;
    var pointArray = [];
    var threshold = 153;
    var alternate = true;
    var directions = [
        (width * -1) - 1,
        width * -1,
        (width * -1) + 1,
        -1,
        1,
        width + 1,
        width,
        width - 1
    ];

    var generatePointArray = function(width, height, scale) {
        var pointArray = [];
        var index = 0;
        for (var i=0; i<width/scale; i++) {
            for (var j=0; j<height/scale; j++) {
                index++;
                pointArray.push({
                    x: i*scale,
                    y: j*scale,
                    w: scale,
                    h: scale,
                    i: index,
                    r: Math.ceil(Math.random()*255),
                    g: Math.ceil(Math.random()*255),
                    b: Math.ceil(Math.random()*255)
                });
            }
        }
        return pointArray;
    };

    var checkNeighbors = function(p,colors) {
        var neighbors = 0;
        _.forEach(directions, function(d) {
            var refIndex = p.i + d;
            if (!_.isUndefined(pointArray[refIndex])) {
                var statusCount = 0;
                _.forEach(colors,function(c){
                    if (pointArray[refIndex][c] >= threshold) {
                        statusCount++;
                    }
                });
                if (statusCount >= 2) {
                    neighbors++;
                }
            }
        });
        return neighbors;
    };

    var checkSection = function(p,colors) {
        var isAlive = false;
        var pCount = 0;
        _.forEach(colors,function(c){
            if (p[c] >= threshold) {
                pCount++;
            }
        });
        if (pCount >= 2) {
            isAlive = true;
        }
        return isAlive;
    };

    var update = function(p) {
        var colors = [‘r’,’g’,’b’];
        var neighbors = checkNeighbors(p,colors);
        var isAlive = checkSection(p,colors);
        if ((neighbors > 1 && neighbors < 4)) {
            if (!isAlive) {
                _.forEach(colors,function(c){
                    p[c] = 255-p[c];
                });
            }
        } else {
            if (isAlive) {
                _.forEach(colors,function(c){
                    p[c] = Math.ceil(Math.random()*threshold);
                });
            }
        }
    };

    var setThreshold = function(threshold,minThreshold,maxThreshold) {
        if (threshold >= minThreshold && threshold <= maxThreshold && alternate) {
            threshold++;
        } else if (threshold >= minThreshold && threshold <= maxThreshold) {
            threshold—;
        } else if (alternate) {
            threshold = maxThreshold;
            alternate = !alternate;
        } else {
            threshold = minThreshold;
            alternate = !alternate;
        }
        return threshold;
    };

    var render = function(p) {
        fill(p.r,p.g,p.b);
        stroke(p.r,p.g,p.b);
        rect(p.x,p.y,p.w,p.h);
    };

    function setup() {
        createCanvas(500, 500);
        frameRate(6);
        background(255);
        pointArray = generatePointArray(width, height, 5);
    };

    function draw() {
        _.forEach(pointArray,function(p){
            update(p);
            render(p);
        });
        threshold = setThreshold(threshold,50,225);
    };

     
  4. //neighbors_140831b

     
  5. //neighbors_140831a

     
  6. //neighbors_140830a
    ArrayList<PointPosition> pointPosition = new ArrayList();
    boolean alternate = false;
    int threshold = 200;
    int width = 500,
        height = 750,
        dx = 0,
        dy = 0;
    int[] directions = {
        1,
        -1,
        width,
        -width,
        (width*-1)-1,
        (width*-1)+1,
        width+1,
        width-1
      };
    int[] pDirections = {
        1,
        -1
      };

    void setup(){
      size(width,height);
      background(0);
      frameRate(24);
      seedPoints(width);
    }

    void draw(){
      scale(5);
      translate(-100,-100);
      for(PointPosition p:pointPosition){
        dx = pDirections[int(random(pDirections.length))];
        dy = pDirections[int(random(pDirections.length))];
        p.update(dx,dy);
        p.render();
      }
      loadPixels();
        for (int k=width+1; k<width*height-width-1; k++){
          int neighbors = 0;
          int pModifier = directions[int(random(directions.length))];
          for (int i=0; i<directions.length; i++) {
            if (brightness(pixels[k+directions[i]]) > threshold) {
              neighbors++;
            } else if (pModifier == directions[i]) {
              pModifier = directions[int(random(directions.length))];
            }
          }
          switch(neighbors) {
            case 0:
              if (brightness(pixels[k]) > threshold) {
                //pixels[k] = color(255);
                pixels[k] = color(
                    map(k,0,abs(cos(k)*k),0,255),
                    map(cos(k),0,1,0,255),
                    map(sin(k),0,1,0,255)
                  );
              }
              break;
            case 1:
              if (brightness(pixels[k]) > threshold) {
                pixels[k] = color(0);
              }
              break;
            case 2:
              pixels[k] = pixels[k+pModifier];
              break;
            case 3:
              if (brightness(pixels[k]) > threshold) {
                pixels[k] = pixels[k+pModifier];
              }
              break;
            default:
              pixels[k] = color(0);
              break;
          }
        }
      updatePixels();
      threshold = setThreshold(threshold,50,225);
    }

    class PointPosition {
      float x,y;
      color c;
      void update(int dx, int dy){
        x += dx;
        y += dy;
      }
      void render(){
        stroke(c);
        point(x,y);
      }
    }

    int setThreshold(int threshold, int minThreshold, int maxThreshold) {
      if (threshold <= maxThreshold && threshold >= minThreshold && alternate) {
        threshold++;
      } else if (threshold <= maxThreshold && threshold >= minThreshold) {
        threshold—;
      } else if (alternate) {
        threshold = maxThreshold;
        alternate = !alternate;
      } else {
        threshold = minThreshold;
        alternate = !alternate;
      }
      return threshold;
    }

    void seedPoints(int count){
      for (int i=0; i<count; i++) {
        PointPosition p = new PointPosition();
          p.c = color(
                  map(i,0,abs(cos(i)*count),153,255),
                  map(i,0,abs(sin(i)*count),0,255),
                  map(sin(i),0,1,135,255)
                  );
          p.x = width/2+int(sin(i)*width/3);//width/2+int(cos(i)*width/(i/int(count/10)));
          p.y = width/2+int(cos(i)*i);
        pointPosition.add(p);
      }
    }

    void mousePressed(){
      record = !record;
    }

     
  7. //dispersedStrata_140829b

     
  8. //dispersedStrata_140829a

     
  9. //dispersedStrata_140826b

     
  10. //dispersedStrata_140825b