> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.7V9yMFxTP6A/rev.1865
 * 
 * authors: 
 *   Matt Perkins

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// This sketch builds on a prior work, "i=0; #1", created by Matt Perkins
// http://studio.sketchpad.cc/sp/pad/view/ro.9l2nsTFUvhvqk/rev.1595

//this is generated sequential version of the "untitled sketch" that is
//the default in sketchpad.  :)

//kind of ugly because i am doing my own pixel manipulation here

//just wanted to mash up the idea of abstract sequential art
//with the generative play allowed here in processing.js.

//next version i'll probably try using PGraphics
//but i will say, dealing with linear bit flipping is
//a worthwhile thing.  (needs aliasing bad tho imho.)

// Pressing Control-R will render this sketch.

int i = 0;
int j = 0;
int k = 0; 

PImage cell;
int WIDTH = 300;
int IMAGESPERWIDTH = 4;
int MARGIN = 10;

int IMAGESIZE = (300- ((4+1) * 4))/4;

void setup() {  // this is run once.   

    cell = createImage(IMAGESIZE  , IMAGESIZE  , RGB);
    for (int k = 0; k < cell.pixels.length; k++) {
        cell.pixels[k] = color(255,255,255,255);
    }
    
    // set the background color
    background(255);
    
    // canvas size (Variable aren't evaluated. Integers only, please.)
    size(325, 325); 
      
    // smooth edges
    smooth();
    
    // limit the number of frames per second
    frameRate(30);
    
    // set the width of the line. 
    strokeWeight(1);

} 

void draw() {  // this is run repeatedly.  

    stroke(255,255, 255, 255);
    fill(255,255, 255, 255);
    
    int marginxoffset = MARGIN * (k+1);
    int imagexoffset =  IMAGESIZE * k;
    int marginyoffset = MARGIN * (j+1);
    int imageyoffset =  IMAGESIZE * j;
    
    rect(marginxoffset + imagexoffset, 
        marginyoffset + imageyoffset, IMAGESIZE-1  , IMAGESIZE-1  ); 
    
    cell = nextImage(cell,i);
    image(cell, 
            marginxoffset + imagexoffset , 
            marginyoffset + imageyoffset);
    
    i++;
    
    k++;    
    if (k >= IMAGESPERWIDTH)
    {
        j++;
        if (j >= IMAGESPERWIDTH)
        {    
            j=0;
        }
        k=0;
    }    
}

//next version use the PGraphics object.... doh!
PImage nextImage(PImage c, int i)
{
    //int coloroffset = i % 155;    
    int column = i % c.width;
    
    c = drawLine(c, column, 0, random(0, c.width), c.height,
         color(random(50), random(255), random(255), 100));
    
    c = drawRect(c, 0, 0, c.width-1, c.height-1, color(0,0,0,255));
    return c;
}

PImage drawRect(PImage image, float x, float y, float w, float h, Color c) {

    image = drawLine(image, x, y, x+w, y, c);
    image = drawLine(image, x, y, x, y+h, c);
    image = drawLine(image, x+w, y, x+w, y+h, c);
    image = drawLine(image, x, y+h, x+w, y+h, c);
    return image;
}

PImage drawLine(PImage image, 
                float x1, 
                float y1, 
                float x2, 
                float y2, 
                Color c) {

    float maxx = max(x1,x2);
    float minx = min(x1,x2);
    float maxy = max(y1,y2);
    float miny = min(y1,y2);

    if (x1 != x2)
    {
        float slope = ((y2 - y1) / (x2 - x1));
    
        if ((maxx-minx) > (maxy - miny)) {
            for(float xh=minx; xh<=maxx; xh++) {        
                float yh = (slope * xh) - (slope * x1) + y1;
                image = drawPoint(image, (int) xh, (int) yh, c);
            }    
        } else {
            for(float yv=miny; yv<maxy; yv++) {        
                float xv = (yv + (slope * x1) - y1) / slope;
                image = drawPoint(image, (int) xv, (int) yv, c);
            }    
        }
    } else {  //our slope would be infinite... linear breakdown!
         for(float yv=miny; yv<maxy; yv++) {                       
            image = drawPoint(image, (int) x1, (int) yv, c);
         }             
    }
    return image;
}

PImage drawPoint(PImage image, int x, int y, Color c) {
    
    if ((x > image.width) || (x < 0))
        return image;
    
    if ((y > image.height) || (y < 0))
        return image;
                        
    image.pixels[y * image.width + x] = c;
    return image;
}