2024年12月16日 星期一

week15

 //week15_01_circle_collision_class_Ball_new

void setup(){

  size(640,340);

}

Ball ball=new Ball(100,200,20);

void draw(){

  ball.display();

}

class Ball{

  float x,y,r;

  Ball(int x0,int y0,int r0){

    x=x0;

    y=y0;

    r=r0;

  }

  void display(){

    ellipse(x,y,r+r,r+r);

  }

}


//week15_01_circle_collision_class_Ball_new
void setup(){
  size(640,340);
}
Ball ball=new Ball(100,200,20);
void draw(){
  ball.display();
}
class Ball{
  float x,y,r;
  Ball(int x0,int y0,int r0){
    x=x0;
    y=y0;
    r=r0;
  }
  void display(){
    ellipse(x,y,r+r,r+r);
  }
}


//week15_02_circle_collision_class_Ball_update void setup(){ size(640,340); } Ball ball=new Ball(100,200,20); void draw(){ ball.display(); ball.update(); }

class Ball{ float x,y,r; float vx,vy; Ball(int x0,int y0,int r0){ x=x0; y=y0; r=r0; vx=random(-10,+10); vy=random(-10,+10); } void update(){ if(x+vx>640||x+vx<0)vx=-vx; if(y+vy<0||y+vy>360)vy=-vy; x+=vx; y+=vy; } void display(){ ellipse(x,y,r+r,r+r); } }

//week15_02_circle_collision_class_Ball_update
void setup(){
  size(640,340);
}
Ball ball=new Ball(100,200,20);
void draw(){
  ball.display();
  ball.update();
}
class Ball{
  float x,y,r;
  float vx,vy;
  Ball(int x0,int y0,int r0){
    x=x0;
    y=y0;
    r=r0;
    vx=random(-10,+10);
    vy=random(-10,+10);
  }
  void update(){
    if(x+vx>640||x+vx<0)vx=-vx;
    if(y+vy<0||y+vy>360)vy=-vy;
    x+=vx;
    y+=vy;
  }
  void display(){
    ellipse(x,y,r+r,r+r);
  }
}




//week15_03_circle_collision_detection_two_ball void setup(){ size(640,340); } Ball ball=new Ball(100,200,60); Ball ball2=new Ball(300,200,60); void draw(){ background(51); if(ball.checkCollision(ball2)) fill(#FFAAAA); else fill(255); ball.display(); ball.update(); ball2.display(); ball2.update(); } class Ball{ float x,y,r; float vx,vy; Ball(int x0,int y0,int r0){ x=x0; y=y0; r=r0; vx=random(-10,+10); vy=random(-10,+10); } boolean checkCollision(Ball other){ if(dist(x,y,other.x,other.y)<r+other.r)return true; else return false; } void update(){ if(x+vx>640||x+vx<0)vx=-vx; if(y+vy<0||y+vy>360)vy=-vy; x+=vx; y+=vy; } void display(){ ellipse(x,y,r+r,r+r); } }

//week15_03_circle_collision_detection_two_ball
void setup(){
  size(640,340);
}
Ball ball=new Ball(100,200,60);
Ball ball2=new Ball(300,200,60);
void draw(){
  background(51);
  if(ball.checkCollision(ball2)) fill(#FFAAAA);
  else fill(255);
  ball.display();
  ball.update();
  ball2.display();
  ball2.update();
}
class Ball{
  float x,y,r;
  float vx,vy;
  Ball(int x0,int y0,int r0){
    x=x0;
    y=y0;
    r=r0;
    vx=random(-10,+10);
    vy=random(-10,+10);
  }
  boolean checkCollision(Ball other){
    if(dist(x,y,other.x,other.y)<r+other.r)return true;
    else return false;
  }
  void update(){
    if(x+vx>640||x+vx<0)vx=-vx;
    if(y+vy<0||y+vy>360)vy=-vy;
    x+=vx;
    y+=vy;
  }
  void display(){
    ellipse(x,y,r+r,r+r);
  }
}




//week15_04_circle_collision_cos_sin_vector_N_N2_M_M2_swap void setup(){ size(640,340); } Ball ball=new Ball(100,200,60); Ball ball2=new Ball(300,200,60); void draw(){ background(51); if(ball.checkCollision(ball2)) fill(#FFAAAA); else fill(255); ball.display(); ball.update(); ball2.display(); ball2.update(); }
class Ball{ float x,y,r; float vx,vy; Ball(int x0,int y0,int r0){ x=x0; y=y0; r=r0; vx=random(-10,+10); vy=random(-10,+10); } boolean checkCollision(Ball other){ if(dist(x+vx,y+vy,other.x+other.vx,other.y+other.vy)<= r+other.r){ PVector diff = new PVector(other.x-x,other.y-y); float len=diff.mag(); print(len); float a=diff.heading(); PVector N =diff.copy().normalize(); N.mult(N.dot(new PVector(vx,vy))); PVector N2=new PVector(-N.y,N.x).normalize(); N2.mult(N2.dot(new PVector(vx,vy))); PVector M=diff.copy().normalize(); M.mult(M.dot(new PVector(other.vx,other.vy))); PVector M2=new PVector(-M.y,M.x).normalize(); M2.mult(M2.dot(new PVector(other.vx,other.vy))); PVector newV1=PVector.add(M,N2); PVector newV2=PVector.add(N,M2); vx=newV1.x; vy=newV1.y; other.vx=newV2.x; other.vy=newV2.y; return true; } else return false; } void update(){ if(x+vx>640||x+vx<0)vx=-vx; if(y+vy<0||y+vy>360)vy=-vy; x+=vx; y+=vy; } void display(){ ellipse(x,y,r+r,r+r); } }

//week15_04_circle_collision_cos_sin_vector_N_N2_M_M2_swap
void setup(){
  size(640,340);
}
Ball ball=new Ball(100,200,60);
Ball ball2=new Ball(300,200,60);
void draw(){
  background(51);
  if(ball.checkCollision(ball2)) fill(#FFAAAA);
  else fill(255);
  ball.display();
  ball.update();
  ball2.display();
  ball2.update();
}
class Ball{
  float x,y,r;
  float vx,vy;
  Ball(int x0,int y0,int r0){
    x=x0;
    y=y0;
    r=r0;
    vx=random(-10,+10);
    vy=random(-10,+10);
  }
  boolean checkCollision(Ball other){
    if(dist(x+vx,y+vy,other.x+other.vx,other.y+other.vy)<= r+other.r){
      PVector diff = new PVector(other.x-x,other.y-y);
      float len=diff.mag();
      print(len);
      float a=diff.heading();
      PVector N =diff.copy().normalize();
      N.mult(N.dot(new PVector(vx,vy)));
      PVector N2=new PVector(-N.y,N.x).normalize();
      N2.mult(N2.dot(new PVector(vx,vy)));
      
      
      PVector M=diff.copy().normalize();
      M.mult(M.dot(new PVector(other.vx,other.vy)));
      PVector M2=new PVector(-M.y,M.x).normalize();
      M2.mult(M2.dot(new PVector(other.vx,other.vy)));
      PVector newV1=PVector.add(M,N2);
      PVector newV2=PVector.add(N,M2);
      vx=newV1.x;
      vy=newV1.y;
      other.vx=newV2.x;
      other.vy=newV2.y;
      return true;
    } else return false;
  }
  void update(){
    if(x+vx>640||x+vx<0)vx=-vx;
    if(y+vy<0||y+vy>360)vy=-vy;
    x+=vx;
    y+=vy;
  }
  void display(){
    ellipse(x,y,r+r,r+r);
  }
}


//week15_05_maze_DFS int [][] maze={ {1,1,1,1,1,1,1,1,1,1,}, {1,0,0,0,1,0,0,0,1,1,}, {1,1,0,1,1,0,1,0,1,1,}, {1,1,0,0,1,0,1,0,1,1,}, {1,1,1,0,0,0,1,0,1,1,}, {1,0,1,1,0,1,1,0,1,1,}, {1,0,0,0,0,1,1,0,1,1,}, {1,0,1,1,1,1,1,0,1,1,}, {1,0,0,0,0,0,1,0,0,1,}, {1,1,1,1,1,1,1,1,1,1,}, }; void setup(){ size(400,400); } void draw(){ for(int i=0;i<maze.length;i++){ for(int j=0;j<maze[0].length;j++){ if(maze[i][j]==1)fill(0); else fill(255); rect(j*40,i*40,40,40); } } }

//week15_05_maze_DFS
int [][] maze={
  {1,1,1,1,1,1,1,1,1,1,},
  {1,0,0,0,1,0,0,0,1,1,},
  {1,1,0,1,1,0,1,0,1,1,},
  {1,1,0,0,1,0,1,0,1,1,},
  {1,1,1,0,0,0,1,0,1,1,},
  {1,0,1,1,0,1,1,0,1,1,},
  {1,0,0,0,0,1,1,0,1,1,},
  {1,0,1,1,1,1,1,0,1,1,},
  {1,0,0,0,0,0,1,0,0,1,},
  {1,1,1,1,1,1,1,1,1,1,},
};
void setup(){
  size(400,400);
}
void draw(){
  for(int i=0;i<maze.length;i++){
    for(int j=0;j<maze[0].length;j++){
      if(maze[i][j]==1)fill(0);
      else fill(255);
      rect(j*40,i*40,40,40);
    }  
  }
}




//week15_06_maze_DFS_visited int [][] maze={ {1,1,1,1,1,1,1,1,1,1,}, {1,0,0,0,1,0,0,0,1,1,}, {1,1,0,1,1,0,1,0,1,1,}, {1,1,0,0,1,0,1,0,1,1,}, {1,1,1,0,0,0,1,0,1,1,}, {1,0,1,1,0,1,1,0,1,1,}, {1,0,0,0,0,1,1,0,1,1,}, {1,0,1,1,1,1,1,0,1,1,}, {1,0,0,0,0,0,1,0,0,1,}, {1,1,1,1,1,1,1,1,1,1,}, }; int [][] visited=new int[10][10]; int startI=1,startJ=1,I=1,J=1,step=1; void setup(){ size(400,400); visited[I][J]=step; } void draw(){ for(int i=0;i<maze.length;i++){ for(int j=0;j<maze[0].length;j++){ if(maze[i][j]==1)fill(0); else fill(255); rect(j*40,i*40,40,40); } } fill(255,0,0); rect(startJ*40,startI*40,40,40); }

//week15_06_maze_DFS_visited
int [][] maze={
  {1,1,1,1,1,1,1,1,1,1,},
  {1,0,0,0,1,0,0,0,1,1,},
  {1,1,0,1,1,0,1,0,1,1,},
  {1,1,0,0,1,0,1,0,1,1,},
  {1,1,1,0,0,0,1,0,1,1,},
  {1,0,1,1,0,1,1,0,1,1,},
  {1,0,0,0,0,1,1,0,1,1,},
  {1,0,1,1,1,1,1,0,1,1,},
  {1,0,0,0,0,0,1,0,0,1,},
  {1,1,1,1,1,1,1,1,1,1,},
};
int [][] visited=new int[10][10];
int startI=1,startJ=1,I=1,J=1,step=1;
void setup(){
  size(400,400);
  visited[I][J]=step;
}
void draw(){
  for(int i=0;i<maze.length;i++){
    for(int j=0;j<maze[0].length;j++){
      if(maze[i][j]==1)fill(0);
      else fill(255);
      rect(j*40,i*40,40,40);
    }  
  }
  fill(255,0,0);
  rect(startJ*40,startI*40,40,40);
}


//week15_07_maze_DFS_visited_step_mousrPressed_move int [][] maze={ {1,1,1,1,1,1,1,1,1,1,}, {1,0,0,0,1,0,0,0,1,1,}, {1,1,0,1,1,0,1,0,1,1,}, {1,1,0,0,1,0,1,0,1,1,}, {1,1,1,0,0,0,1,0,1,1,}, {1,0,1,1,0,1,1,0,1,1,}, {1,0,0,0,0,1,1,0,1,1,}, {1,0,1,1,1,1,1,0,1,1,}, {1,0,0,0,0,0,1,0,0,1,}, {1,1,1,1,1,1,1,1,1,1,}, }; int [][] visited=new int[10][10]; int startI=1,startJ=1,I=1,J=1,step=1; void setup(){ size(400,400); visited[I][J]=step; } void draw(){ for(int i=0;i<maze.length;i++){ for(int j=0;j<maze[0].length;j++){ if(maze[i][j]==1)fill(0); else fill(255); rect(j*40,i*40,40,40); } } fill(255,0,0); rect(startJ*40,startI*40,40,40); for(int i=0;i<maze.length;i++){ for(int j=0;j<maze[0].length;j++){ if(visited[i][j]>0){ fill(0,0,255); text(""+visited[i][j],j*40+20,i*40+20); } } } } void mousePressed(){ if(maze[I+1][J]==0){ I=I=1; visited[I][J]=++step; } else if(maze[I][J+1]==0){ J=J+1; visited[I][J]=++step; } }

//week15_07_maze_DFS_visited_step_mousrPressed_move
int [][] maze={
  {1,1,1,1,1,1,1,1,1,1,},
  {1,0,0,0,1,0,0,0,1,1,},
  {1,1,0,1,1,0,1,0,1,1,},
  {1,1,0,0,1,0,1,0,1,1,},
  {1,1,1,0,0,0,1,0,1,1,},
  {1,0,1,1,0,1,1,0,1,1,},
  {1,0,0,0,0,1,1,0,1,1,},
  {1,0,1,1,1,1,1,0,1,1,},
  {1,0,0,0,0,0,1,0,0,1,},
  {1,1,1,1,1,1,1,1,1,1,},
};
int [][] visited=new int[10][10];
int startI=1,startJ=1,I=1,J=1,step=1;
void setup(){
  size(400,400);
  visited[I][J]=step;
}
void draw(){
  for(int i=0;i<maze.length;i++){
    for(int j=0;j<maze[0].length;j++){
      if(maze[i][j]==1)fill(0);
      else fill(255);
      rect(j*40,i*40,40,40);
    }  
  }
  fill(255,0,0);
  rect(startJ*40,startI*40,40,40);
  for(int i=0;i<maze.length;i++){
    for(int j=0;j<maze[0].length;j++){
      if(visited[i][j]>0){
        fill(0,0,255);
        text(""+visited[i][j],j*40+20,i*40+20);
      }
    }
  }
}
void mousePressed(){
  if(maze[I+1][J]==0){
    I=I=1;
    visited[I][J]=++step;
  } else if(maze[I][J+1]==0){
    J=J+1;
    visited[I][J]=++step;
  }
}



//week15_08_maze_DFS_visited_step_mousrPressed_move int [][] maze={ {1,1,1,1,1,1,1,1,1,1,}, {1,0,0,0,1,0,0,0,1,1,}, {1,1,0,1,1,0,1,0,1,1,}, {1,1,0,0,1,0,1,0,1,1,}, {1,1,1,0,0,0,1,0,1,1,}, {1,0,1,1,0,1,1,0,1,1,}, {1,0,0,0,0,1,1,0,1,1,}, {1,0,1,1,1,1,1,0,1,1,}, {1,0,0,0,0,0,1,0,0,1,}, {1,1,1,1,1,1,1,1,1,1,}, }; int [][] visited=new int[10][10]; int startI=1,startJ=1,I=1,J=1,step=1; void setup(){ size(400,400); //visited[I][J]=step; } void draw(){ for(int i=0;i<maze.length;i++){ for(int j=0;j<maze[0].length;j++){ if(maze[i][j]==1)fill(0); else fill(255); rect(j*40,i*40,40,40); } } fill(255,0,0); rect(startJ*40,startI*40,40,40); for(int i=0;i<maze.length;i++){ for(int j=0;j<maze[0].length;j++){ if(visited[i][j]>0){ fill(0,0,255); text(""+visited[i][j],j*40+20,i*40+20); } } } } boolean DFS(int i,int j,int s){ if(i==8 &&j ==8) return true; if(maze[i][j]==1)return false; if(visited[i][j]>0)return false; visited[i][j]=s; if(DFS(i+1,j,s+1))return true; if(DFS(i-1,j,s+1))return true; if(DFS(i,j+1,s+1))return true; if(DFS(i,j-1,s+1))return true; visited[i][j]=0; return false; } void mousePressed(){ visited[1][1]=0; DFS(1,1,1); }

//week15_08_maze_DFS_visited_step_mousrPressed_move
int [][] maze={
  {1,1,1,1,1,1,1,1,1,1,},
  {1,0,0,0,1,0,0,0,1,1,},
  {1,1,0,1,1,0,1,0,1,1,},
  {1,1,0,0,1,0,1,0,1,1,},
  {1,1,1,0,0,0,1,0,1,1,},
  {1,0,1,1,0,1,1,0,1,1,},
  {1,0,0,0,0,1,1,0,1,1,},
  {1,0,1,1,1,1,1,0,1,1,},
  {1,0,0,0,0,0,1,0,0,1,},
  {1,1,1,1,1,1,1,1,1,1,},
};
int [][] visited=new int[10][10];
int startI=1,startJ=1,I=1,J=1,step=1;
void setup(){
  size(400,400);
  //visited[I][J]=step;
}
void draw(){
  for(int i=0;i<maze.length;i++){
    for(int j=0;j<maze[0].length;j++){
      if(maze[i][j]==1)fill(0);
      else fill(255);
      rect(j*40,i*40,40,40);
    }  
  }
  fill(255,0,0);
  rect(startJ*40,startI*40,40,40);
  for(int i=0;i<maze.length;i++){
    for(int j=0;j<maze[0].length;j++){
      if(visited[i][j]>0){
        fill(0,0,255);
        text(""+visited[i][j],j*40+20,i*40+20);
      }
    }
  }
}
boolean DFS(int i,int j,int s){
  if(i==8 &&j ==8) return true;
  if(maze[i][j]==1)return false;
  if(visited[i][j]>0)return false;
  visited[i][j]=s;
  if(DFS(i+1,j,s+1))return true;
  if(DFS(i-1,j,s+1))return true;
  if(DFS(i,j+1,s+1))return true;
  if(DFS(i,j-1,s+1))return true;
  visited[i][j]=0;
  return false;
}
void mousePressed(){
  visited[1][1]=0;
  DFS(1,1,1);
}












沒有留言:

張貼留言