2025年11月16日 星期日

week 12. Object-Oriented Programming



1.  Mandala Project reference:
http://sciencefictioninterfaces.tumblr.com/

2. object oriented programming:
  https://processing.org/tutorials/objects/
  https://www.youtube.com/watch?v=YcbcfkLzgvs

3. simple particle system

EXercise:
  draw 'array-like' object with OOP
Challenge:
  draw Mandala with OOP 
   (define your own object class for mandala object) 

reference:


mandala OOP example:

Mandala myMandala;
void setup() {
  size(600, 600);
  myMandala = new Mandala();
}

void draw() {
background(0);
 fill(0,0,0);
  rect(0,0,width,height);
myMandala.drawMandala();
myMandala.updateMandala();
}

class Mandala{
float radius;
int segment = 12;
float centerX;
float centerY;
int[] pattern;


Mandala(){
 stroke(255);
 centerX = height/2;
 centerY = width/2;
 pattern = new int[8];
 for (int i= 0; i< 8; i++)
   pattern[i]=(int) random(4);
}
void drawMandala()
{
  background(0);
   fill(0,0,0);
  rect(0,0,width,height);
  radius = 30.0;
  segment = 12;
  noFill();
  pushMatrix(); 
    translate(centerX, centerY);
     rotate(frameCount/40.0);
     translate(-centerX, -centerY);
  for (int i= 0; i< 8; i++){
   radius=radius+25.0;  
   segment=segment+ 6;
    
   
    spiral(centerX,centerY,radius,segment, pattern[i]);
    
  }
  popMatrix();
}
void spiral(float x_c,float y_c, float r, int seg, int pattern){
    for(int i =0;i<seg;i++){
    float theta = i*2*PI/seg;  
     float x = x_c+cos(theta)*r;  //算出x的位置
     float y = y_c+sin(theta)*r;  //算出y的位置
//      point(x,y);  // 畫出點,也能用其他形狀
    ellipse(x_c, y_c, 2*r, 2*r);
    if (pattern == 0) ellipse(x_c, y_c, 2*r+8, 2*r+8);
   if (pattern == 1) drawPattern1(x,y,theta);
   if (pattern == 2) drawPattern2(x,y,theta);  
   if (pattern == 3) drawPattern3(x,y,theta);     
   //pattern  
    
    }
}
void drawPattern1(float x, float y, float theta){
    pushMatrix();
      translate(x,y);
      rotate(theta);
    point(5, 0);
    line(0,10, 20,0);
    line(0,-10, 20, 0);
     popMatrix();
}
void drawPattern2(float x, float y, float theta){
    pushMatrix();
      translate(x,y);
      rotate(theta);
    ellipse(5, 0, 5, 5);
    arc(0, 0, 30, 20, PI*1.5, PI*2.5);
     arc(0, 0, 25, 15, PI*1.5, PI*2.5);
     popMatrix();
}
void drawPattern3(float x, float y, float theta){
    pushMatrix();
      translate(x,y);
      rotate(theta);
     beginShape();
       curveVertex(0,  8);
       curveVertex(0,  8);
       curveVertex(16,  6);
       curveVertex(16,  -6);
       curveVertex(0, -8);
       curveVertex(0, -8);
      endShape();
    beginShape();
       curveVertex(0,  10);
       curveVertex(0,  10);
       curveVertex(20,  8);
       curveVertex(20,  -8);
       curveVertex(0, -10);
       curveVertex(0, -10);
      endShape();
//    bezier(0,-10, 15, -9, 0, 0, 18,0);
//    bezier(0,10, 15, 9,0, 0, 18,0);

     popMatrix();
}
void updateMandala()
{

}  
}

沒有留言:

張貼留言