Showing posts with label Processing. Show all posts
Showing posts with label Processing. Show all posts

Friday, August 19, 2011

[Processing] spiral demo using Processing

//@ http://scriptdemo.blogspot.com
float myAngle, myScale, myAlpha0, myAlpha;
float shiftX, shiftY;
int rr,gg,bb;
// to control the shape of spiral
float minScale, dAngle, coefScale;
boolean isLine=false;
boolean isColor=true;

void setup()
{
  size(500,500);
  shiftX=width/2;
  shiftY=height/2;
  myAngle=0;
  myScale=1.0;
  minScale=0.01;
  coefScale=0.99;
  myAlpha0=8;
  dAngle=PI/50;

  noStroke();
  background(255);
  frameRate(30);
  smooth();
}

void draw()
{
   while(myScale>minScale)
   {
      pushMatrix();
      myAngle=myAngle+dAngle;
      myScale=myScale*coefScale;
      translate(shiftX,shiftY);
      rotate(myAngle);
      scale(myScale);
      if (isColor)
      {
         rr=ceil(255*sin(myAngle));
         gg=ceil(255*cos(myAngle));
         if (rr<=0) rr=rr+255;
         if (gg<=0) gg+=255;
         bb=(rr+gg)%256;
      } else {
        rr=10; gg=10; bb=10;
        if (isLine) myAlpha0=max(myAlpha0,20);
      }
      myAlpha=min(myAlpha0*(ceil(myAngle/TWO_PI)),255);
      if (isLine)
      {
        stroke(rr,gg,bb,myAlpha);
        noFill();
        line(-1*shiftX,shiftY,shiftX,shiftY);
      } else {
        noStroke();
        fill(bb,gg,rr,myAlpha);
        rect(-1*shiftX,shiftY,width,height);
      }
      popMatrix();
   }
}

void mouseClicked()
{
  isLine=!isLine;
  myAlpha=myAlpha0;
  myScale=1;
  myAngle=random(-PI,PI);
  background(255);
}
void mouseDragged()
{
   isColor=!isColor;
   myAlpha=myAlpha0;
   myScale=1;
   myAngle=random(-PI,PI);
   background(255);
}

[Processing] Transformation example in Processing

2D Transformations example:Online Demo
//@ http://scriptdemo.blogspot.com/
int numP;
myrose[] roses;
float rotAngle, myscale;
float shiftX, shiftY;

void setup()
{
  size(500,500);
  numP=36;
  smooth();
  roses=new myrose[numP];
  myscale=1;
  rotAngle=0;
  shiftX=width/2;
  shiftY=height/2;
  frameRate(30);
  for (int n=0; n<numP; n++)
  {
    roses[n]=new myrose();
  }
  noStroke();
}
void draw()
{
  background(255);
  translate(shiftX,shiftY);
  if (!mousePressed)
  {
    rotAngle=rotAngle+PI/100;
  }
  rotate(rotAngle);
  scale(myscale);
  pushMatrix();
  for (int n=0; n<numP; n++)
  {
   roses[n].myrose();
   roses[n].setangle(n*TWO_PI/numP);
   roses[n].showit();
  }
  popMatrix();
}

class myrose
{
  float myangle;
  int nres=20;
  float[] xx=new float[nres*2+1];
  float[] yy=new float[nres*2+1];
  float rr=0.4*width;
  float rr05=rr/2;
  float rry=0.1*rr;

  void myrose()
  {
     for (int n=-nres; n<=nres; n++)
     {
        xx[n+nres]=rr05+n*rr05/nres;
        yy[n+nres]=rry*sin(n*TWO_PI/nres);
     }
  }
  void showit()
  {
    pushMatrix();
    rotate(myangle);
    for (int n=0; n<xx.length; n++)
    {
      fill(n*255/xx.length,127);
      ellipse(xx[n],yy[n],5,5);
    }
    popMatrix();
  }
  void setangle(float ang)
  {
    myangle=ang;
  }
}

void mouseDragged()
{
  if (mouseButton==CENTER)
  {
    shiftX+=(mouseX-pmouseX);
    shiftY+=(mouseY-pmouseY);
  } else if (mouseButton==LEFT) {
    float d0=(pmouseX-width/2)*(pmouseX-width/2)+(pmouseY-height/2)*(pmouseY-height/2);
    float dc=(mouseX-width/2)*(mouseX-width/2)+(mouseY-height/2)*(mouseY-height/2);
    myscale=(sqrt(dc)-sqrt(d0));
  }
}

Thursday, March 3, 2011

[Processing] More Perlin Noise demo with Processing

Actually it is generated with the Perlin Noise Particle System from Dann Van Hasselt (http://www.openprocessing.org/visuals/?visualID=10475)
What I did is set the initial particle position in every loop (within draw()):
e.g., 
           myt=elapseFrames/10/TWO_PI;
           pos.x=0.5*width +(1-sin(myt))*cos(myt)*0.2*width;
           pos.y=0.5*height-(1-sin(myt))*sin(myt)*0.2*height;

Also you can change the color and particle details to get something like (online):
or a colourful one:

Perlin Noise demo with Processing

#copyright @ http://scriptdemo.blogspot.com
// inspired by the Perlin Noise Particle System from Daan van Hasselt (http://www.openprocessing.org/visuals/?visualID=10475)
// I recoded it with more additional features. Also it may be easier to understand without the class.
// Online test  from my dropbox (my Dropbox reference link)
float myw=400.0;
float myh=350.0;
float x, y;
float u=0;
float v=0;
float x0, y0, dx, dy;
float coefx, coefy, coefz;
boolean isAlive=false;
boolean isColor=true;
boolean isSmooth=true;
boolean isLoop=true;

int NumP=400;
float Npi;
void setup()
{
  Npi=2.3; coefx=0.009; coefy=0.011; coefz=0.0013;
  size(floor(myw),floor(myh));
  background(255,255,255);
  if (isSmooth) smooth();
}

void draw()
{

  for (int n=0; n<=NumP; n++)
  {
      if (isAlive)
      {
        float nv=noise(x*coefx,y*coefy,frameCount*coefz);
        if (isColor)
        {
           float mysin=sin((nv-0.3)*TWO_PI*Npi);
           float mycos=cos((nv-0.3)*TWO_PI*Npi);
           if (mycos>=0 & mysin >=0) fill(floor(abs(mysin)*255),floor(abs(mycos)*255),0,10);
           if (mycos>=0 & mysin <0) fill(floor(abs(mysin)*255),0,floor(abs(mycos)*255),10);
           if (mycos<0 & mysin >=0) fill(0,floor(abs(mycos)*255),floor(abs(mysin)*255),10);
           if (mycos<0 & mysin <0) fill(floor(abs(mysin)*255),floor(abs(mycos)*255),0,10);
        } else {
           fill(10,10);
        }
         u=u+cos((nv-0.3)*TWO_PI*Npi);
         v=v+sin((nv-0.3)*TWO_PI*Npi);
         u=u/2;
         v=v/2;
         x=x+u;
         y=y+v;
      } else {
        x0=0.5*myw;
        y0=0.5*myh;
        x=x0; y=y0;
        dx=100*frameCount/2000+50;
        dy=dx;
        isAlive=true;
      }
      noStroke();
      ellipse(x,y,1,1);
  }
  if (x>width || y> height || x <0 || y< 0) isAlive=false;
}

void mouseClicked()
{
  isLoop=!isLoop;
  if (!isLoop)
  {
     noLoop();
  } else {
     loop();
  }
}
Here are some demo videos:

Saturday, February 5, 2011

2D random walk using Processing

//To show 2D random [some limitation in the code] walk using Processing
// Copyright by http://scriptdemo.blogspot.com

myParticles[] myP;
int numP=2;
boolean IsLine=false;

void setup()
{
  size(400,400);
  background(0);
  myP=new myParticles[numP];

  for (int i=0; i<numP; i++)
  {
    myP[i]= new myParticles();
    myP[i].x=random(100,300);
    myP[i].y=random(100,300);
  }
  frameRate(50);
}

void draw()
{
  for(int i=0; i<numP; i++)
  {
    myP[i].move();
    if (ceil(myP[i].x)== width | ceil(myP[i].y) == height)
    {
      noLoop();
    }
  }
}
void mouseClicked()
{
  background(0);
}
void mouseReleased()
{
  for(int i=0; i<numP; i++)
  {
    myP[i].setxy(mouseX,mouseY);
  }
  IsLine=!IsLine;
  loop();
}

class myParticles
{

  public float x0;
  public float y0;
  public float x;
  public float y;

  public void myParticles()
  {
  }
  public void setxy(float tmpx, float tmpy)
  {
    x=tmpx;
    y=tmpy;
  }

  public void move()
  {
    for ( int i=20; i<=60; i=i+10 )
    {
      float tmpx, tmpy;
      float myangle;
      float dx=5;
      float dy=5;
      x0=x;
      y0=y;
      myangle=(i+floor(random(-5,5)))*PI/180.0;
      tmpx=dx*random(-1,1);
      tmpy=dy*random(-1,1);
      x=x+tmpx*cos(myangle)-tmpy*sin(myangle);
      y=y+tmpy*cos(myangle)+tmpx*sin(myangle);
      if ( x<0 ) { x=0.0; }
      if ( x>width ) { x=width; }
      if ( y<0 ) {y=0.0;}
      if ( y>height ) { y=height; }

      int rr=floor(random(255));
      int rg=floor(random(255));
      int rb=floor(random(255));
      stroke(rr,rr,rb);
      if (IsLine)
        {
          line(x0,y0,x,y);
        }
       else
      {
        point(x,y);
      }
    }
  }
}



ShowCalendar