We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › Kaleidoscope
Page Index Toggle Pages: 1
Kaleidoscope (Read 1060 times)
Kaleidoscope
Aug 27th, 2008, 5:23am
 
Hello all.  Here is my first Processing project and, incidentally, my first real Java program.  

http://skymeadowmedia.com/GenerativeArt/Kaleidoscope1/

Will post the code in separate posts.

A second version using OPENGL and some other enhancements -- is here:

http://skymeadowmedia.com/GenerativeArt/Kaleidoscope2/

marcia
Re: Kaleidoscope
Reply #1 - Aug 27th, 2008, 5:24am
 
//-------first half of the code-------

// Kaleidoscope
// Marcia Stockton
// 24 August 2008

PGraphics kw;            // kaleidoscope workspace - superimposed floating objects
PGraphics fi;            // final composed image buffer with the reflections
int d = 600;             // dimension
int numShapes = 9;       // but up to 20 when re-randomized in mousePressed
Shape[] shapes;
int maxVertices = 9;     // randomized in mousePressed
int minVertices = 3;

void setup() {
 size(d, d);                         // drawing canvas
 kw = createGraphics(d, d, P3D);     // the kaleidoscope background thing
 fi = createGraphics(d, d, P3D);
 frameRate(60);                      // default is 60
 smooth();
 shapes = new Shape[20];             // create 20 arrays, max number randomized when MousePressed.
 mousePressed();                     // initialize some parameters in a standard way
 background(0);
 noStroke();
}

void draw() {
 kw.beginDraw();
 kw.background(0);
 kw.endDraw();
 for (int i=1; i<numShapes; i++) {
   shapes[i].rot();
   shapes[i].place();
   shapes[i].trans();
   mirror();                      // calculate pixels for the final display buffer fi
   image(fi,0,0);                 // display it
 }
}

class Shape {
 int n;                        // number of vertices
 float[] xvertices, yvertices; // constant
 float[] xverticesR, yverticesR; // as rotated
 float cx, cy;                  // adjust for center
 color c;                       // its color
 int t;                         // opacity (255 = completely opaque)
 float angle;                   // amount to rotate
 float rots;                    // rotations
 float xincr, yincr;            // translation movement
 color st;                      // color of stroke (shape outline)

 Shape() {          
   n = int(random(minVertices,maxVertices));  // how many vertices
   xvertices = new float[n];
   yvertices = new float[n];
   xverticesR = new float[n];
   yverticesR = new float[n];
   for (int i=0; i<n; i++) {                 // choose the vertices
     xvertices[i] = random(d*.66);
     yvertices[i] = random(d*.66);
   }
   cx = (max(xvertices)-min(xvertices))/2;   // calculate center of shape
   cy = (max(yvertices)-min(yvertices))/2;
   for (int i=0; i<n; i++) {                  // shift shape so it's centered
     xvertices[i] = xvertices[i]-cx;
     xverticesR[i]= xvertices[i];
     yvertices[i] = yvertices[i]-cy;
     yverticesR[i]= yverticesR[i];
   }
   t = int(random(.25*255,.75*255));        // alpha value specifying opacity
   colorMode(HSB,255);                      // select color for shape
   c = color( int(random(255)), int(random(148,255)), int(random(148,255)));  // try H,S,B
   /*  if (random(100)>50) {                    // 50% chance the shape has a visible outline
    st = (0);
    }
    else {
    st = c;
    } */
   angle = random(-PI, PI)/180;           // determine shape's rotation speed
   rots = 0;                              // initialize number of rotation                
   xincr = random(-d/50,d/50);            // establish translation speed
   yincr = random(-d/50,d/50);
 }    

//--------code continues in the next post
Re: Kaleidoscope
Reply #2 - Aug 27th, 2008, 5:25am
 
//-----Code continued from previous post---------

 void rot() {
   for(int i=0; i<n; i++) {
     float x = xvertices[i];               // get vertices from object
     float y = yvertices[i];
     float r = sqrt(pow(x, 2) + pow(y, 2)); // Convert to polar:  compute radius
     float theta = atan2 (x, y);            // compute angle
     theta = theta + angle * rots++;        // rotate by angle
     float x1 =r * cos(theta + angle);      // compute new points
     float y1 = r * sin(theta + angle);     // put them back in object array
     xverticesR[i] = x1;
     yverticesR[i] = y1;  
   }
 }

 void trans() {                             // adjust object position based on its individual movement
   for(int i=0; i<n; i++) {
     xverticesR[i] = xvertices[i] + xincr;
     yverticesR[i] = yvertices[i] + yincr;
   }
 }

 void place() {                             // draw the repositioned shape on the kw workspace
   kw.beginDraw();
   kw.fill(c,t);                            // the color c and also the alpha value for opacity, t
   //    kw.stroke(st);
   kw.noStroke();                           // nostroke looks better
   //    kw.stroke(0);
   kw.beginShape();                         // build the shape in the kw buffer
   for(int i = 0; i<n; i++ ) {
     kw.vertex(xverticesR[i]+cx, yverticesR[i]+cy);  // define all the vertices
   }
   kw.endShape(CLOSE);
   kw.endDraw();
 }

 void changeTrajectory() {
   xincr = xincr + random(-d/100,d/100);          // modify translation speed
   yincr = yincr + random(-d/100,d/100);
 }

 void changeRot(){
   angle = angle + random(-PI, PI)/360;          // modify shape's rotation speed
   rots = 0;                          
 }
}

void mirror() {
 color p;
 kw.beginDraw();
 fi.beginDraw();
 for (int y=0; y<(d/2); y++) {  // copy wedge-shaped 1/8 of square
   for (int x=y; x<(d/2); x++) {
     p = kw.get(x,y);           // get one pixel from the workspace
     fi.set (x,y,p);            // put it to the "final" composed workspace
     fi.set (y,x,p);
     fi.set (x,d-y-1,p);
     fi.set (y,d-x-1,p);  
     fi.set (d-x-1,y,p);
     fi.set (d-x-1,d-y-1,p);
     fi.set (d-y-1,x,p);
     fi.set (d-y-1,d-x-1,p);    
   }
 }
 fi.endDraw();
 kw.endDraw();
}

void mousePressed() {    // randomize many of the variables, clear screen, create new shapes
 kw.beginDraw();        // clear the buffer
 kw.fill(0);
 kw.rect(0,0,d,d);      // erase by drawing canvas-sized black rectangle
 kw.endDraw();
 maxVertices = int(random(4,9));
 minVertices = int(random(3,maxVertices));
 numShapes = int(random(4,20));
 for (int i=1; i<numShapes; i++) {
   shapes[i] = new Shape();
 }
}

//---------end of code---------
Page Index Toggle Pages: 1