Exercise 52


// P_2_1_2_02.pde
// 
// Generative Gestaltung, ISBN: 978-3-87439-759-9
// First Edition, Hermann Schmidt, Mainz, 2009
// Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
// Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
//
// http://www.generative-gestaltung.de
/**
 * changing module color and positions in a grid
 * 	 
 * MOUSE
 * position x          : offset x
 * position y          : offset y
 * left click          : random position
 * 
 * KEYS
 * 1-3                 : different sets of colors
 * 0                   : default
 * arrow up/down       : background module size
 * arrow left/right    : foreground module size
 * s                   : save png
 * p                   : save pdf
 */

import processing.pdf.*;
import java.util.Calendar;

boolean savePDF = false;

color moduleColorBackground = color(0);
color moduleColorForeground = color(255);

color moduleAlphaBackground = 100;
color moduleAlphaForeground = 100;

float moduleRadiusBackground = 30;
float moduleRadiusForeground = 15;

color backColor = color(255);


float tileCount = 20;
int actRandomSeed = 0;

void setup(){
  size(600, 600);
}

void draw() {
  if (savePDF) beginRecord(PDF, timestamp()+".pdf");

  translate(width/tileCount/2, height/tileCount/2);

  colorMode(HSB, 360, 100, 100, 100);
  background(backColor);
  smooth();
  noStroke();

  randomSeed(actRandomSeed);

  for (int gridY=0; gridY < tileCount; gridY++) {
    for (int gridX=0; gridX < tileCount; gridX++) {
      float posX = width/tileCount * gridX;
      float posY = height/tileCount * gridY;

      float shiftX =  random(-1, 1) * mouseX/20;
      float shiftY =  random(-1, 1) * mouseY/20;

      fill(moduleColorBackground, moduleAlphaBackground);
      ellipse(posX+shiftX, posY+shiftY, moduleRadiusBackground, moduleRadiusBackground);
    }
  }

  for (int gridY=0; gridY < tileCount; gridY++) {
    for (int gridX=0; gridX < tileCount; gridX++) {

      float posX = width/tileCount * gridX;
      float posY = height/tileCount * gridY;

      fill(moduleColorForeground, moduleAlphaForeground);
      ellipse(posX, posY, moduleRadiusForeground, moduleRadiusForeground);
    }
  }

  if (savePDF) {
    savePDF = false;
    endRecord();
  }
}

void mousePressed() {
  actRandomSeed = (int) random(100000);
}

void keyReleased(){
  if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
  if (key == 'p' || key == 'P') savePDF = true;

  if (key == '1'){
    if (moduleColorBackground == color(0)) {
      moduleColorBackground = color(273, 73, 51);
    } 
    else {
      moduleColorBackground = color(0);
    } 
  }
  if (key == '2'){
    if (moduleColorForeground == color(360)) {
      moduleColorForeground = color(323, 100, 77);
    } 
    else {
      moduleColorForeground = color(360);
    } 
  }

  if (key == '3'){
    if (moduleAlphaBackground == 100) {
      moduleAlphaBackground = 50;
      moduleAlphaForeground = 50;
    } 
    else {
      moduleAlphaBackground = 100;
      moduleAlphaForeground = 100;
    } 
  }


  if (key == '0'){  
    moduleColorBackground = color(0);
    moduleColorForeground = color(360);
    moduleAlphaBackground = 100;
    moduleAlphaForeground = 100;
    moduleRadiusBackground = 20;
    moduleRadiusForeground = 10;
  }

  if (keyCode == UP) moduleRadiusBackground += 2;
  if (keyCode == DOWN) moduleRadiusBackground = max(moduleRadiusBackground-2, 10);
  if (keyCode == LEFT) moduleRadiusForeground = max(moduleRadiusForeground-2, 5);
  if (keyCode == RIGHT) moduleRadiusForeground += 2;

}

// timestamp
String timestamp() {
  Calendar now = Calendar.getInstance();
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

Exercise 51


// P_2_1_3_01.pde
// 
// Generative Gestaltung, ISBN: 978-3-87439-759-9
// First Edition, Hermann Schmidt, Mainz, 2009
// Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
// Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni
//
// http://www.generative-gestaltung.de

/**
 * changing circle amount, size and position in a grid
 * 	 
 * MOUSE
 * position x          : circle amount and size
 * position y          : circle position
 * left click          : random position
 * 
 * KEYS
 * s                   : save png
 * p                   : save pdf
 */

import processing.pdf.*;
import java.util.Calendar;

boolean savePDF = false;

float tileCountX = 10;
float tileCountY = 10;
float tileWidth, tileHeight;

int count = 0;
int colorStep = 15;
int circleCount;
float endSize, endOffset;

int actRandomSeed = 0;


void setup() { 
  size(800, 800);
  tileWidth = width / tileCountX;
  tileHeight = height / tileCountY;
} 


void draw() { 
  if (savePDF) beginRecord(PDF, timestamp()+".pdf");

  smooth();
  noFill();
  stroke(0, 128);
  background(255); 
  randomSeed(actRandomSeed);

  translate((width/tileCountX)/2, (height/tileCountY)/2);

  circleCount = mouseX/30 + 1;
  endSize = map(mouseX, 0, width, tileWidth/2.0, 0);
  endOffset = map(mouseY, 0, height, 0, (tileWidth-endSize)/2);

  for (int gridY=0; gridY <= tileCountY; gridY++) {
    for (int gridX=0; gridX <= tileCountX; gridX++) {  
      pushMatrix();
      translate(tileWidth*gridX, tileHeight*gridY);
      scale(1, tileHeight/tileWidth);

      int toggle = (int) random(0, 4);
      if (toggle == 0) rotate(-HALF_PI);  
      if (toggle == 1) rotate(0);  
      if (toggle == 2) rotate(HALF_PI);  
      if (toggle == 3) rotate(PI);  

      // draw module
      for (int i=0; i < circleCount; i++) {
        float diameter = map(i, 0, circleCount-1, tileWidth, endSize);
        float offset = map(i, 0, circleCount-1, 0, endOffset);
        ellipse(offset, 0, diameter, diameter);
      }
      popMatrix();
    }
  }

  if (savePDF) {
    savePDF = false;
    endRecord();
  }
} 


void mousePressed() {
  actRandomSeed = (int) random(100000);
}


void keyReleased() {
  if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
  if (key == 'p' || key == 'P') savePDF = true;
}


// timestamp
String timestamp() {
  Calendar now = Calendar.getInstance();
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

Exercise 50


/*-------- Circle Pattern ----------------*/

int margin;
int res = 4;
int padding;
 
color c [] = {
  #75a665,
  #5c801a,
  #5a7e18,
  #735742,
  #afb837,
  #564311,
  #32320e
};
 
void setup ()
{
  size (700, 700);
  smooth();
  frameRate (10);
  margin = width/10;
  padding = width /30;
}
 
void draw ()
{
  background (237);
  randomSeed (year() + day() + hour() + minute() + (second ()/ 20 % 2));
  noStroke();
 
  int d = int ((width-2*margin-(res-1)*padding) /(float) res);
  int D = d;
  for (int x = margin; x < width-margin; x+=d+padding)
  {
 
    for (int y = margin; y < width-margin; y+=d+padding)
    {
      D = d;
      int [] cc = new int [3];
      int i = 0;
      while (i < cc.length)
      {
        int index = (int) random (c.length);
        boolean taken = false;
        for (int j = 0; j < i; j++)
        {
          if (cc[j] == index)
          {
            taken = true;
            break;
          }
        }
        if (!taken)
        {
          D = (int) random (D/2, D);
          fill (c[index]);
          ellipse (x+d/2, y+d/2, D, D);
          i++;
        }
      }
    }
  }
}
 
void mouseDragged ()
{
  if (mouseButton == LEFT)
  {
    margin = (int) map (mouseX, 0, width, 0, width/3);
    padding = (int) map (mouseY, 0, width, 0, height/15);
  }
  else res = (int) map (mouseX, 0, width, 1, 50);
}
 
void keyPressed ()
{
  setup();
}

Bonus: Glitch



PImage img;
int min = 40, max = 80;
int r = 10;
 
void setup() {
  img = loadImage("https://s-media-cache-ak0.pinimg.com/736x/2c/f6/a5/2cf6a58f75b368949cf98db03b925f1b.jpg");
  size(img.width, img.height);
  image(img,0,0);
}
 
void draw() {
  int w = (int)random(min, max);
  int h = int(w*3);
  int x = (int)random(0, width-w);
  int y = (int)random(0, height-h);
  PImage tmp = createImage(w, h, RGB);
  tmp.loadPixels();
  float rd = random(1);
  for (int px=0; px < w; px++) {
    for (int py=0; py < h; py++) {
      if (rd>0.1) {
      tmp.pixels[py*w+px]=img.get(x+px, y+py);
      } else {
      tmp.pixels[py*w+px]=color(0);
      }
    }
  }
  tmp.updatePixels();
  
  int v = (int)random(-r, r);
  fill(100,100);
  noStroke();
  int e = 2;
  rect(x+v+1,y+v+1,w+e,h+e);
  fill(255,255);  
  rect(x+v-e/2,y+v-e/2,w+e,h+e);
  
  image(tmp, x+v, y+v);
}
 
void keyPressed() {
  save(random(123456)+".jpg");
 
}

Exercise 45

using rotation and sine cosine functions to draw patterns.


float k = 2/7.0;
int scaleIt = 400;

void setup() {
  size(800, 800);
  background(20);
  colorMode(HSB);
  strokeWeight(0.01);
  smooth();
}

void draw() {
  translate(width/2, height/2);
  //scale(width/2, height/2);
  float t = frameCount / 20.0;
  float x = cos(k*t) * sin(t) * scaleIt;
  float y = cos(k*t) * cos(t) * scaleIt; 
  stroke(255);
  line(0, 0, x, y);
}

The chart below shows the results of different parameters, from the wikipedia page on the rose formula.
Rose-rhodonea-curve-7x9-chart

Exercise 44


int num=10, sw=8, fc;
float r;
long rs;
boolean save = false;
 
void setup() {
  size(500, 500);
  //colorMode(HSB, 360, 100, 100);
  noFill();
  rs = (long) random(100);
  strokeWeight(sw);
  strokeCap(SQUARE);
}
 
void draw() {
  randomSeed(rs);
  background(#14133B);
  for (int i=0; i < 3; i++) {
    arcs(width/2, height/2);
  }
  if (save) {
    if (frameCount%2==0 && frameCount < fc+241) saveFrame("frames/image-###.png");
  }
}
 
void arcs(float x, float y) {
  pushMatrix();
  translate(x, y);
  rotate(r);
  for (int i=0; i < num; i++) {
    //stroke(360.0/num*i, 100, 100, 120);
    float lerpAmount = 1.0/num*i;
    color col = lerpColor(#9E023B, #FFC675, lerpAmount);
    stroke(col, 220);
    float start = random(TWO_PI);
    float end = start + random(PI/5, PI/3);
    float scal = map(sin(r+TWO_PI/num*i), -1, 1, .5, 1.5);
    arc(0, 0, width*.7-i*4*sw, height*.7-i*4*sw, start, end*scal);
  }
  r += .0523/2;
  popMatrix();
}
 
void keyPressed() {
  save = true;
  fc = frameCount;
}

Exercise 43

int num=50, d=100, frames=120;
float theta;

void setup() {
  size(540, 540, P2D);
  blendMode(SCREEN);
  stroke(255);
  noStroke();
  fill(255);
  //noFill();
}

void draw() {
  background(0);
  for (int j=0; j < 3; j++) {
    if (j==0) fill(255, 0, 0);
    if (j==1) fill(0, 255, 0);
    if (j==2) fill(0, 0, 255);
    for (int i=0; i < num; i++) {
      float angle = TWO_PI/num*i;
      float x = width/2 + j*5 + cos(angle)*d;
      float y = height/2 + sin(angle)*d;
      pushMatrix();
      translate(x, y);
      rotate(theta+angle);
      float d2=d*1.5;
      arc(0, 0, d2, d2, 0, radians(5));
      popMatrix();
    }
  }
  theta += TWO_PI/frames;
  // if (frameCount <= frames) saveFrame("frames/image-###.png");
}

Exercise 42


int frames = 235, num=30;
float theta;
 
void setup() {
  size(750, 750);
  rectMode(CENTER);
}
 
void draw() {
  background(20);
  noFill();
  translate(width/2, height/2);
  for (int i=0; i < num; i++) {
    pushMatrix();
    float offSet=TWO_PI/num*i;
    rotate(offSet);
    float sz = map(sin(theta), -1, 1, width*.15, width*.2);
    float x = map(sin(theta), -1, 1, sz, width*.2);
    translate(x, 0);
    pushMatrix();
    rotate(theta);
      stroke(200);
    if (i%2==0) {
      ellipse(0, 0, sz, sz*2);
      float px = cos(theta)*sz/2;
      float py = sin(theta)*sz;
      float sz2 = 20;
      strokeWeight(2);
      ellipse(px, py, sz2, sz2);
      strokeWeight(1);
    } else {
      fill(255,20);
      noStroke();
      ellipse(0, 0, sz*.7, sz*2*.7);
      float px = cos(theta+PI)*sz*.35;
      float py = sin(theta+PI)*sz*.7;
      float sz2 = 5;
      fill(255);
      ellipse(px, py, sz2, sz2);
      noFill();
    }
    popMatrix();
    popMatrix();
  }
  theta += TWO_PI/frames;
  //if (frameCount<=frames) saveFrame("image-###.png");
}

Exercise 41


int frames = 220, num=30;
float theta;
 
void setup() {
  size(750, 750);
}
 
void draw() {
  background(20);
  stroke(#9EC3CE, 255);
  strokeWeight(2);
  noFill();
  translate(width/2, height/2);
  for (int i=0; i < num; i++) {
    pushMatrix();
    float offSet=TWO_PI/num*i;
    rotate(offSet);
    float sz = map(sin(theta),-1,1,width*.15,width*.2);
    float x = map(sin(theta), -1, 1, sz, width*.2);
    translate(x, 0);
    pushMatrix();
    rotate(theta);
    if (i%2==0) {
      ellipse(0, 0, sz, sz*2);
    } else {
      ellipse(0, 0, sz*.7, sz*2*.7);
    }
    popMatrix();
    popMatrix();
  }
  theta += TWO_PI/frames;
  //if (frameCount<=frames) saveFrame("image-###.gif");
}

Exercise 40

Outputting an animation as video takes two steps

  1. saving frames of the animation as separate image files
  2. compiling the sequence of images into a movie using MovieMaker tool

int frames = 200, num=20;
float theta;
 
void setup() {
  size(750, 750);
}
 
void draw() {
  background(20);
  fill(255, 30);
  stroke(255, 50);
  translate(width/2, height/2);
  for (int i=0; i < num; i++) {
    pushMatrix();
    float offSet=TWO_PI/num*i;
    rotate(offSet);
    float sz = width*.15;
    float x = map(sin(theta), -1, 1, sz, width*.2);
    translate(x, 0);
    pushMatrix();
    rotate(theta);
    ellipse(0, 0, sz, sz*2);
    ellipse(0, 0, sz*.7, sz*2*.7);
    popMatrix();
    popMatrix();
  }
  theta += TWO_PI/frames;
  //if (frameCount<=frames) saveFrame("image-###.png");
}