Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, August 19, 2011

[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

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);
      }
    }
  }
}



Sunday, January 30, 2011

compile java program with javac

#!/bin/bash
# compile java program with javac
# usage:
#            myjava xxx.java
# copyright @ http://scriptdemo.blogspot.com

if [ $# -eq 0 ]; then
   sed -n '2,4p' myjava
   exit
else
   srcfile=$1
   nf=`echo ${srcfile} | awk -F. '{print NF}'`
   if [ $nf -eq 1 ]; then
      exefilename=${srcfile}
      srcfile=${srcfilename}.java
   elif [ $nf -eq 2 ]; then
      exefilename=`echo ${srcfile} | awk -F. '{print $1}'`
   else
      myjava
      exit
   fi

   if [ -e ${srcfile} ]; then
      javac ${srcfile}
      java -classpath . ${exefilename}
   elif [ -e ${exefilename} ];then
      java -classpath . ${exefilename}
   else
      echo "${srcfile} can\'t be found in current directory!"
      exit
   fi
fi

Tuesday, January 18, 2011

compile java program with gcj

#!/bin/bash
# compile java program with gcj
# usage:
#            mygcj myMain.java [otherJavaFiles]
# copyright @ http://scriptdemo.blogspot.com
if [ $# -eq 0 ]; then
   sed -n '2,4p' mygcj
   exit
else
   numfile=$#
   IsMain=1
   myfiles=""
   for srcfile in $*
   do
      nf=`echo ${srcfile} | awk -F. '{print NF}'`
      if [ $nf -eq 1 ]; then
         srcfilename=${srcfile}
         srcfile=${srcfilename}.java
      elif [ $nf -eq 2 ]; then
         srcfilename=`echo ${srcfile} | awk -F. '{print $1}'`
      fi
      [ ! -e ${srcfile} ] && echo "${srcfile} can\'t be found in current directory!" && exit
      if [ $IsMain -eq 1 ]; then
         mainfile=$srcfilename; IsMain=0
      fi
      myfiles="${myfiles} ${srcfile}"
   done
   gcj ${myfiles} --main=${mainfile} -o ${mainfile}
fi

ShowCalendar