‹ pakt13pakt15 ›

pakt14

See /f0blog/pact-februari/

//pakt14
//not good - wrong filling rule
int index;
float w, h;
float spread;
float spreadx, spready;
int n;
PGraphics grad;
void setup() {
  size(640, 480, JAVA2D);
  frameRate(60);
  smooth();
  noStroke();
  index= 0;
  w= width;
  h= height;
  n= 100;
  grad= createGraphics(width, height, JAVA2D);
  grad.beginDraw();
  grad.background(0);
  createGradient(grad, w*0.5, h*0.5, w, color(0.5*255, 0.5*255, 0.6*255), color(0, 0, 0));
  grad.endDraw();
  fill(255.0, 255.0, 255.0, 0.85*255.0);
}
void draw() {
  image(grad, 0, 0);
  float x= 0, y= 0;
  for (int i= 0; i<=n; i++) {
    spreadx= sin((i+index)*0.011)*sin((i+index)*0.013);
    spready= sin((i+index)*0.012)*sin((i+index)*0.014);
    spread= (spreadx*spready)*0.25+cos((index+i)*0.001);
    float yy= sin((i+index)*0.012+spready);
    float hh= yy*(cos(index*0.013));
    float xx= cos((i+index)*0.014+spreadx);
    float ww= xx*(sin(index*0.015));
    yy= yy*(h*spread)+(h*0.5);
    xx= xx*(h*spread)+(h*0.5);
    hh= hh*(h*spread)+(h*0.5);
    ww= ww*(h*spread)+(h*0.5);
    if (i==0) {
      x= xx;
      y= yy;
    } else if (i%5==1) {
      bezier(x, y, xx, yy, ww, hh, ww, hh);
      x= ww;
      y= hh;
    } else if (i%5==2) {
      bezier(x, y, yy, xx, hh, ww, hh, ww);
      x= hh;
      y= ww;
    } else if (i%5==3) {
      bezier(x, y, xx, yy, hh, ww, hh, ww);
      x= hh;
      y= ww;
    } else if (i%5==4) {
      bezier(x, y, yy, xx, ww, hh, ww, hh);
      x= ww;
      y= hh;
    } else {
      x= yy;
      y= xx;
    }
  }
  index++;
}
//Simple Radial Gradient by Ira Greenberg, modified by /f0
void createGradient (PGraphics ctx, float x, float y, float radius, color c1, color c2) {
  float px = 0, py = 0, angle = 0;
  // calculate differences between color components 
  float deltaR = red(c2)-red(c1);
  float deltaG = green(c2)-green(c1);
  float deltaB = blue(c2)-blue(c1);
  // hack to ensure there are no holes in gradient
  // needs to be increased, as radius increases
  float gapFiller = 8.0;
  for (int i=0; i< radius; i++) {
    for (float j=0; j<360; j+=1.0/gapFiller) {
      px = x+cos(radians(angle))*i;
      py = y+sin(radians(angle))*i;
      angle+=1.0/gapFiller;
      color c = color(
        (red(c1)+(i)*(deltaR/radius)), 
        (green(c1)+(i)*(deltaG/radius)), 
        (blue(c1)+(i)*(deltaB/radius)) 
        );
      ctx.stroke(c);
      ctx.rect(px, py, 1, 1);
    }
  }
  // adds smooth edge 
  // hack anti-aliasing
  ctx.noFill();
  ctx.strokeWeight(3);
  ctx.ellipse(x, y, radius*2, radius*2);
}