2025年3月13日 星期四

生命之歌 week04

      用角度旋轉

 //week04-01-atan2-dy-dx-cos-sin

void setup(){
  size(600,300);
}
void draw(){
  background(#C0FFEE);//粉青咖啡色
  ellipse(150,150,100,100);//big eye
  ellipse(150+25,150,50,50);
  float dx = mouseX-150, dy = mouseY-150;
  float a = atan2(dy,dx);//atan=angle,算出角度
  ellipse(150+cos(a)*25,150+sin(a)*25,50,50);
  //ellipse(150+25,150,50,50);
}







//week04-02-atan2-for-x-dx-dy-cos-sin
void setup(){
  size(600,300);
}
void draw(){
  background(#C0FFEE);//粉青咖啡色,裡面是0
  for(int x = 150;x<=450;x+=300){ //迴圈,做出兩個
    ellipse(x,150,100,100);//big eye
    
    float dx = mouseX-x, dy = mouseY-150;
    float a = atan2(dy,dx);//atan=angle,算出角度
    ellipse(x+cos(a)*25,150+sin(a)*25,50,50);
  }
}



歐拉轉動

//week04-03-rotateX-radians-mouseY
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2,height/2);
  //rotateY(radians(mouseX));//turn left or right
  rotateX(radians(-mouseY));//turn up or down
  box(200);
}


//week04-04-rotateZ
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2,height/2);
  rotateZ(radians(mouseX));
  ellipse(0,0,100,150);
}

    小機器手臂



//week04-05-robot-arm-pushMatrix-T-R-T-box-popMatrix
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2,height/2);
  pushMatrix();
    translate(0,100);
    box(50);//台座
    pushMatrix();
      translate(0,-25);
      rotateZ(radians(mouseX));
      translate(0,-50);
      box(10,100,10);
    popMatrix();
  popMatrix();
  
}

    逐步拆解



長條轉動

//week04-05a-rotateZ-translate-box
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2,height/2);//move to the center(畫面)
 
      //以下兩行分別註解並排列組合,觀察
      rotateZ(radians(frameCount));//對z軸旋轉
      translate(0,-50);//把下端移到中心
      box(10,100,10);//可轉動長條
    
  
}

跟著滑鼠移動的旋轉長條

//week04-05b-translate-box-mouseX-mouseY-rotateZ-translate-box
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  //translate(width/2,height/2);//move to the center(畫面)
 
      translate(mouseX,mouseY);
      rotateZ(radians(frameCount));//對z軸旋轉
      translate(0,-50);//把下端移到中心
      box(10,100,10);//可轉動長條
    
  
}

公轉/自轉


//week04-06-push-translate-rotate-sphere-pop
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  pushMatrix();
    translate(mouseX,mouseY);
    rotateY(radians(frameCount));
    sphere(100);
  popMatrix();
}



//week04-07-sun-earth
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2,height/2);
  sphere(50);
  rotateY(radians(frameCount));
  pushMatrix();
    translate(150,0);
    rotateY(radians(frameCount));
    sphere(30);
  popMatrix();
}

    


//week04-08-sun-earth-moon
void setup(){
  size(400,400,P3D);
}
void draw(){
  background(128);
  translate(width/2,height/2);
  sphere(50);//sun
  rotateY(radians(frameCount));
  pushMatrix();
    translate(150,0);
    rotateY(radians(frameCount));
    sphere(30);//earth
    pushMatrix();
      translate(50,0);
      rotateY(radians(frameCount));
      sphere(10);//moon
     popMatrix();
  popMatrix();
}


貼入地球圖案

//week04-09-earth-texture-image
//google:earth map texture
PImage img = loadImage("earth.jpg");
size(600,300);
image(img, 0, 0, 600, 300);


ctrl+k開啟檔案總管
    

3d地球

https://www.solarsystemscope.com/textures/

//week04-10-earth-texture-image-createShape-setTexture-shape
//google:earth map texture

size(400,400,P3D);
PShape earth = createShape(SPHERE, 100);
PImage img = loadImage("earth.jpg");
earth. setTexture(img);
shape(earth);

地球自轉

//week04-11-earth-setTexture-translate- rotate
PShape earth;
void setup(){
  size(400,400,P3D);
  earth = createShape(SPHERE, 100);
  PImage img = loadImage("earth.jpg");
  earth.setTexture(img);
}
void draw(){
  background(0);
  translate(width/2,height/2);
  rotateY(radians(frameCount));
  shape(earth);
}

//week04-12-moon-setTexture-translate- rotate
PShape moon;
void setup(){
  size(400,400,P3D);
  moon = createShape(SPHERE, 100);
  PImage img = loadImage("moon.jpg");
  moon.setTexture(img);
}
void draw(){
  background(0);
  translate(width/2,height/2);
  rotateY(radians(frameCount));
  shape(moon);
}
//week04-13-sun-setTexture-translate- rotate
PShape sun;
void setup(){
  size(400,400,P3D);
  sun = createShape(SPHERE, 100);
  PImage img = loadImage("sun.jpg");
  sun.setTexture(img);
}
void draw(){
  background(0);
  translate(width/2,height/2);
  //rotateY(radians(frameCount));
  shape(sun);
}


//week04-14-sun-earth-moon-setTexture
PShape sun,earth,moon;
void setup(){
  size(400,400,P3D);
  sun = createShape(SPHERE, 50);
  PImage img = loadImage("sun.jpg");
  sun.setTexture(img);
  earth = createShape(SPHERE, 30);
  img = loadImage("earth.jpg");
  earth.setTexture(img);
  moon = createShape(SPHERE, 10);
  img = loadImage("moon.jpg");
  moon.setTexture(img);
}
void draw(){
  background(128);
  translate(width/2,height/2);
  shape(sun);//sphere(50);//sun
  rotateY(radians(frameCount));
  pushMatrix();
    translate(150,0);
    rotateY(radians(frameCount));
    shape(earth);//sphere(30);//earth
    pushMatrix();
      translate(50,0);
      rotateY(radians(frameCount));
      shape(moon);//sphere(10);//moon
     popMatrix();
  popMatrix();
}




沒有留言:

張貼留言