2025年3月20日 星期四

生命之歌 week05

 先把中心點移到中間

//week05-1-i-love-you

size(300,300);
stroke(255,0,0);//red line
for(int xx=0; xx<300;xx++){//大的x 0...300
  for(int yy=0; yy<300;yy++){
    float x = (xx-150)/100.0;//減一半,除100
    float y = -(yy-150)/100.0;//減一半,除100
      // 0..300變-150..+150 變 -1.5 ~ +1.5
    float d = x*x +y*y -1;
    if(d*d*d - x*x*y*y*y < 0) point(xx,yy);
  }

}






//week05-1b-i-love-you
size(300,300);
stroke(255,0,0);//red line
translate(width/2 , height/2);//translate(150,150);
for(int xx=-150; xx<150;xx++){//大的x -150...150
  for(int yy=-150; yy<150;yy++){//大的y -150...150
    float x = xx/100.0;//除100 -1.5..+1.5
    float y = -yy/100.0;//除100 -1.5..1.5
      // 0..300變-150..+150 變 -1.5 ~ +1.5
    float d = x*x +y*y -1;
    if(d*d*d - x*x*y*y*y < 0) point(xx,yy);
  }

}


//week05-2-for-for-ellipse-arc-arc-radians-360
//角度 degrees vs. radians 弧度/ 逕度
size(600,600);
background(0);
for(int i=0; i<6; i++){
  for(int j=0; j<6; j++){
    int now = i*6 + j;//每個circle的編號
    ellipse(50 + j*100, 50+i*100, 80, 80);
    arc(50+j*100, 50+i*100, 60, 60, 0, now, PIE);
    arc(50+j*100, 50+i*100, 40, 40, 0, radians(now*10), PIE);
    text(now, j*100, i*100+30);
  }
}
                                    

    滑鼠移動,角度變化

只有180度

//week05-3-atan2-dy-dx-text-radians-degrees
//degrees vs. radians 結合 week04-01 and week 05-2
void setup(){
  size(400,400);
}
void draw(){
  background(128);
  line(200,200,400,200);
  line(200,200,mouseX,mouseY);
  float dx = mouseX-200, dy = mouseY -200;
  float a = atan2(dy,dx);//神奇的三角函式 ,可找到 arc 弧的 radians
  //atan2()的值,介於 - pi ... +PI
  arc(200,200,200,200,0,a,PIE);//把算出來的ARC RADIANS 拿來用
  textSize(30);
  text("radians:" +a,100,100);
  text("degrees:" + degrees(a),100,130);
}



整個

//week05-3b-atan2-dy-dx-text-radians-degrees
//degrees vs. radians 結合 week04-01 and week 05-2
void setup(){
  size(400,400);
}
void draw(){
  background(128);
  line(200,200,400,200);
  line(200,200,mouseX,mouseY);
  float dx = mouseX-200, dy = mouseY -200;
  float a = atan2(dy,dx);//神奇的三角函式 ,可找到 arc 弧的 radians
  //atan2()的值,介於 - pi ... +PI
  if(a<0) arc(200,200,200,200,a,0,PIE);
  else arc(200,200,200,200,0,a,PIE);
  textSize(30);
  text("radians:" +a,100,100);
  text("degrees:" + degrees(a),100,130);
}


    自轉

//wee05-4-translate-mouseX-mouseY-rotate-radians-frameCount
//比較rotate 與 translate 順序
void setup(){
  size(400,400);
}
void draw(){
  background(204);
  //請做以下測試:把下面2行,掉一下順序
  //會發現:畫東西之前的 translate()才有效果
  //在電腦圖學理,畫圖時,會照著之前<累積移動,旋轉>來放東西
  translate(mouseX,mouseY);//move to mouse
  rotate(radians(frameCount));//1秒有60 frame轉60度 要變radians單位
  rect(-50,-5,100,10);//寬度100棒子,放左上角
}


 公轉
//wee05-5-rotate-radians-frameCount-translate-mouseX-mouseY
//比較rotate 與 translate 順序
void setup(){
  size(400,400);
}
void draw(){
  background(204);
  //請做以下測試:把下面2行,掉一下順序
  //會發現:畫東西之前的 translate()才有效果
  //在電腦圖學理,畫圖時,會照著之前<累積移動,旋轉>來放東西
  
  rotate(radians(frameCount)*10);//1秒有60 frame轉60度 要變radians單位
  translate(mouseX,mouseY);//move to mouse
  rect(-50,-5,100,10);//寬度100棒子,放左上角
}
//week05-6-pushMatrix-popMatrix-bad
void setup(){
  size(400,400);
}
void draw(){
  background(204);
  
  
  
  translate(width/2,height/2);//把(下面)移到畫面中心
  rotate(radians(frameCount)*10);
  rect(-50,-5,100,10);
  
  
  translate(width/2-100,height/2);//把(下面)移到畫面中心
  rotate(radians(frameCount)*10);
  rect(-50,-5,100,10);
}
-----------------------------------------------------------------------------
//week05-6-pushMatrix-popMatrix-good
void setup(){
  size(400,400);
}
void draw(){
  background(204);
  
  
  pushMatrix();
    translate(width/2,height/2);//把(下面)移到畫面中心
    rotate(radians(frameCount)*10);
    rect(-50,-5,100,10);
  popMatrix();
  
  pushMatrix();
    translate(width/2-100,height/2);//把(下面)移到畫面中心
    rotate(radians(frameCount)*10);
    rect(-50,-5,100,10);
  popMatrix();
}

    pushMatrix:設定矩陣數值

    popMatrix:重製矩陣數值

//week05-7-many-pushmatrix-popmatrix
void setup(){
  size(500,500);
 }
 void draw(){
   background(204);
   for(int x=50;x<500;x += 100){
     for(int y=50;y<500;y += 100){
       pushMatrix();//today host
         translate(x,y);
         rotate(radians(frameCount));
         rect(-50,-5,100,10);//100 length stick
        popMatrix();//today host
     }
   }
 }

沒有留言:

張貼留言