openobject.org

My Lil' Pattern Maker

From Physical Programming

Contents

Screen Capture

image:MyLilPatternMaker.jpg

Description

The sketch works by taking information from the user's mouse co-ordinates and using that information to draw lines. The lines are drawn multiple times in a grid using two for() loops (one nested inside the other). The first loop deals with the columns of the grid while the second deals with the rows. The size of the grid is controlled by the variable gridsize. The colour is also chosen relative to the mouse position along with some random element. The "R" key draws random unrelated lines. The "N" key starts a new pattern and resets the sketch. I originally wanted to make a sketch that generated fractals (like this [1]), but abandoned that because I lacked the skills needed.

Download

MyLilPatternMaker.pde.zip

References

Code

///////////////////////////////////
//
//  Name: My Lil' Pattern Maker
//  Author: Daniel Kerris
//
///////////////////////////////////

//Initiliase variables
float x = 0;
float y = 0;
float a = 0;
float b = 0;
float mx = 1;
float my = 1;
int gridsize = 70;

//Setup
void setup() {
  size(400,400);
  background(255);
  stroke(0,0,0,255); //using stroke in the form of stroke(r,g,b,alpha);
  noFill();
  smooth();
}

//Draw
void draw() {
  //background(255);
  for (int i = -gridsize; i < 400; i = i+gridsize) {
    for (int j = -gridsize; j < 400; j = j+gridsize) {
      line(i+x, j+y, i+a, j+b);
    }
  }
}


//Mouse
void mouseDragged() {
  //Set all variables relative to mouse co-ordinates
  a = mouseX / 5;
  b = mouseY / 5; 
  x = mouseY / 5;
  y = mouseX / 5;
  //Set color relative to mouseX
  mx = (mouseX/300); //Gives us a percentage
  my = (mouseY/300);
  stroke(mx*255,my*255,random(255),255);
}

//Keyboard
void keyPressed() {
  //If R key is pressed, randomise variables
  if (key == 'r') {
    b = random(gridsize*10)/10;
    a = random(gridsize*10)/10;
    x = random(gridsize*10)/10;
    y = random(gridsize*10)/10;
  }
  if (key == 'n') {
    a = 0;
    b = 0;
    x = 0;
    y = 0;
    background(255); //clear screen
  }
}