2011年2月4日 星期五

FractalBall (Processing)

使用Processing進行Fractal去定義球

Trying to use Processing to describe a ball by Fractal.

首先簡單得到定義球的座標
球座標(x,y,z)=
(x=r \sin\theta \cos\phi\,\!,
y=r \sin\theta \sin\phi\,\!,
z=r \cos\theta\,\! 。)
並透過RotateY與RotateZ
去得到類似向量之分項

First, we get the definition of 
the x,y,z position on the Sphere Surface
based on
(x=r \sin\theta \cos\phi\,\!,
y=r \sin\theta \sin\phi\,\!,
z=r \cos\theta\,\! 。)
and use the RotateY and RotateZ
to make the vector like effect.

在Processing程式中
boxbox功能 即是定義碎形之程式
透過boxbox中的數字
分別定義
a= 第一個單元的尺寸
c =接下來單元縮小的尺寸
e f g 分別為繪製單元之位置
n =碎形次數

In the processing,
boxbox function is the function defines the fractal.
a = the size of the first component
c = the scaling factor of the following components
e f g = the location of the single component 
n= times of the factal

the code as follow:
void setup(){
background(255);//background color
size(700,700,P3D);//size
smooth();
}

void draw(){
  background(0);
  lights();
  translate(width / 3, height / 3,-height / 3);
  rotateY(map(mouseX, 0, width, 0, PI));
  rotateZ(map(mouseY, 0, height, 0, -PI));
  noStroke();
  translate(0, -300, 0);//camera

 int r2=500;// the balls radius
 int angle=30//numbers of the angle's increment

 for(int j =360; j>=0; j=j-angle){
   for(int i =360; i>=0; i=i-angle){

          pushMatrix();
          rotateZ((i*PI)/180);
          rotateY((j*PI)/180);
          translate((r2*sin((i*PI/180))*cos(j*PI/180)),r2*cos(i*PI/180), (r2*sin(i*PI/180)*sin(j*PI/180)));//ball coordination
          fill(i,j,i+j,i+j);//ball color             
          boxbox(200,0.8,0,0,0,5);
          popMatrix();  
     }
    }
}

void boxbox(float a, float c,float e,float f,float g, float n){
     translate(e,f,g);
     //box(random(10,a));//make random boxes 
     box(a);
     //sphere(random(0,a));//make random spheres
     //sphere(a);
          
     if(n>0){
     float k = 0.6*a ;
     float m=a*c;  
     boxbox(k,c,m,0,0,n-1);  
     }  
}
BOXES



BALLS

RANDOM BOXES

RANDOM BALLS










沒有留言:

張貼留言