Monday, 23 August 2010

pre-creativePact efforts pt.2

This next one was more tricky, trying to connect two bezier curves so they always flow elegantly: meant finding a way to match the slopes.


Move your mouse around it to see it in action: this may be slow depending on your java
engine.





Here's the code.


void setup() {
  size(200,200);
  background(255);
  smooth();
}

void draw() {
 background(255);
 
 //skeleton points
 stroke(0);
 ellipse(0,0,5,5);
 ellipse(mouseX,mouseY,5,5);
 ellipse(200,200,5,5);
 ellipse(100,100,5,5);
 
 //control points 
  float Xvalue = 180;
 float a = atan2(mouseY-height/2, mouseX-width/2); //mobile point for testing
  float b = a + 2*PI; //reverse the angle
  float opp = tan(b)*80; //find Y value as X is supplied (one value must be supplied, or hypotenuse)
  if(degrees(a) > -90 && degrees(a) < 90) { //deal with flipping
    Xvalue = 200 - Xvalue; //flip x value through the midpoint
    opp *= -1; //flip Y value negative
  }
  println(Xvalue); 
  stroke(255,0,0);
  ellipse(Xvalue,opp+100,5,5); //Y value from centrepoint, end points of bezier
  
  bezier(0,0,50,10,mouseX,mouseY,100,100);
  stroke(0,255,0);
  bezier(100,100,Xvalue,opp+100,50,180,200,200);
  stroke(0);
  ellipse(50,10,5,5); //fixed control points
  ellipse(180,150,5,5);
  
 stroke(240);
 line(mouseX,mouseY,Xvalue,opp+100); //connector between control points
}

pre-creativePact efforts pt.1

I did a little exploratory work in April with Processing. I wanted to have a generative system for creating series of curves that output as time/pitch on a grid.


Ultimately this would be a generative system where a musical object could be looped with slight changes in each iteration. The curves would change each time, and where the curves break the grid is the time between each pitch event (x=time, y=pitch).


My creativePact will try to extend this.


This first effort was just in making a single randomly changing bezier curve. That was easy enough, but the accidental fanning effect was so nice I left it in: may find a use for it later.




Here's the code (highlighting is for Java so may not be entirely accurate)


int start = millis();


void setup() {
  size(400,400);
  background(255);
  smooth();
}

void draw() {
  float pointA = 0; 
  float pointB = 0;
  float pointC = 0;
  float pointD = 0;
  int steps = 40; //breaks the curve into X line segments
  
  int i;
  int distance = 20; //distance between gridlines
  int amount = 20; // amount of gridlines
  
  float[] pointsX = new float[100];
  float[] pointsY = new float[100];
  
  float[] intersections = new float[100];
  
  int q = 1; //counter, how many horizontal lines crossed
  int p = 1;
 
  int time = 1000;
  if(millis() - start > time) { // redraw after "time"
  fill(255,255,255,200); //transparent background layer
  rect(0,0,400,400);  
    
    pushMatrix(); //draw horizontal grid lines
    int k;
    for (k=0;k<amount;k++) {
     stroke(120);
     translate(0,distance);
     line(0,0,width,0);
     }
     popMatrix();
     
    pushMatrix(); //draw v grid erticallines
    for (k=0;k<amount;k++) {
     stroke(120);
     translate(distance,0);
     line(0,0,0,height);
     }
     popMatrix();


    //sets the 1st bezier curve 
    float thing = (20 + random(380)); //random control points
    float thing1 = (20 + random(380)); //random control points      
    for (int j = 0; j <= steps; j++) {
      float t = j / float(steps);
      float x = bezierPoint(0, thing, thing1, 400, t);
      float y = bezierPoint(0, thing1, thing, 400, t);
      stroke(0);
      point(x, y);
      line(pointA, pointB,x,y);
      pointA = x;  
      pointB = y;
      pointsX[j] = x;
      pointsY[j] = y; 
    }
    
    //finds and sets the elipses for the horizontal grid
    for (int m = 1; m < steps; m++) {
      if ((pointsY[m-1] <= (distance*q)) && (pointsY[m] >= (distance*q))) {
        float theta = atan2(pointsY[m] - pointsY[m-1] ,pointsX[m] - pointsX[m-1]); //FIX HERE check correct atan2 values, translate to 0,0!
        float opposite = ((distance*q) - pointsY[m-1]) / (tan(theta));
        stroke(255,0,0);
        int po = int(pointsX[m-1]+opposite);  
        int op = int(distance*q);
        ellipse(po, op, 5, 5);    
        q++;
      }
    continue;
    }
    
    
    
    //sets the 2nd bezier curve 
    float thing2 = (20 + random(380)); //random control points
    float thing3 = (20 + random(380)); //random control points      
    for (int j = 0; j <= steps; j++) {
      float t = j / float(steps);
      float x = bezierPoint(0, thing2, thing3, 400, t);
      float y = bezierPoint(0, thing3, thing2, 400, t);
      stroke(0);
      point(x, y);
      line(pointA, pointB,x,y); //fan accidentent happens here as pointA/B not connected to following pointC/D
      pointC = x;  
      pointD = y;
      pointsX[j] = x;
      pointsY[j] = y; 
    }
    
    //finds and sets the elipses for the horizontal grid
    for (int m = 1; m < steps; m++) {
      if ((pointsY[m-1] <= (distance*q)) && (pointsY[m] >= (distance*q))) {
        float theta = atan2(pointsY[m] - pointsY[m-1] ,pointsX[m] - pointsX[m-1]); //FIX HERE check correct atan2 values, translate to 0,0!
        float opposite = ((distance*q) - pointsY[m-1]) / (tan(theta));
        stroke(255,0,0);
        int po = int(pointsX[m-1]+opposite);  
        int op = int(distance*q);
        ellipse(po, op, 5, 5);    
        q++;
      }
    continue;
    }

   start = millis();
  }
}

Sunday, 22 August 2010

Intro to CreativePact 2010

Hi,

This is my blog page for CreativePact 2010, to keep track of developments/tangents and share code as it progresses. This is my first Pact, and I'll be working with Processing to do some generative audio/visual pieces.