2024年12月30日 星期一

week01.0313

 //week01_1_size

size(400, 400);











//week01_2_background

size(500,500);

background(255,255,0);//黃色














//week01_3_void_setup_void_draw_if_mousePressed

void setup() {

  size(500, 500);//大小

}

void draw() {

  if (mousePressed)background(255, 0, 0);//紅色

  else background(0, 255, 0);//綠色

}















//week01_4_if_mousePressed_line_mouseX_mouseY_pmouseX_pmouseY

void setup() {

  size(500, 500);

}

void draw() {

  if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY);

}//如果 mouse按下去,就畫線mouse的XY座標到previous mouse XY


















//week01_5_void_keyPressed_if_key_strokeWeight
void setup() {
  size(500, 500);
}
void draw() {
  if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY);
}//如果 mouse按下去,就畫線mouse的XY座標到previous mouse XY
void keyPressed() {
  if (key=='1') strokeWeight(1);
  if (key=='2') strokeWeight(5);
  if (key=='3')strokeWeight(10);
}//如果key按下去,看key的值,決定線的粗細。



















//week01_6_void_keyPressed_if_key_stroke
void setup() {
  size(500, 500);
}
void draw() {
  if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY);
}//如果 mouse按下去,就畫線mouse的XY座標到previous mouse XY
void keyPressed() {
  if (key=='1') strokeWeight(1);
  if (key=='2') strokeWeight(5);
  if (key=='3')strokeWeight(10);
  if (key=='4')stroke(255, 0, 0);
  if (key=='5')stroke(255, 255, 0);
  if (key=='6')stroke(0, 255, 0);
  if (key=='7')stroke(0, 0, 255);
  if (key=='8')stroke(255, 0, 255);
}//如果key按下去,看key的值,決定線的粗細。





















//week01_7_background_rect_fill_ellipse
size(600, 400);
background(#FFFFA2);//色彩的色碼 #RRGGBB(色彩選擇器,可調色)
rect (100, 100, 400, 200);
//在100,100的地方,畫寬度400,高度200的長方形
fill(#FFFFA2);//填充色
ellipse(300, 200, 100, 100);
















//week01_8_arc
size(400, 400);
fill(#FFDC0F);//填充黃色
stroke(255, 0, 0); //noStroke();//不要有邊緣的筆觸
//ellipse(50,50,100,100);//本來畫圓
arc(50, 50, 100, 100, 0+1, PI*2-1, PIE);//改變弧度
////X座標,Y,寬,高,開始,結束,蘋果派的方法畫















//week01_9_for_x_for_y_arc
size(400, 400);
fill(#FFDC0F);//填充黃色
for (int x=0; x<400; x+=100) {
  for (int y=0; y<400; y+=100) {
    arc(x+50, y+50, 100, 100, 0+1, PI*2-1, PIE);//改變弧度
  }
}




week17 fin.

 int[][][] maze = {

  {

    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 牆壁區域

    {1, 0, 0, 1, 1, 0, 0, 0, 1, 1}, // 可通行區域

    {1, 1, 0, 1, 1, 1, 1, 0, 1, 1}, // 可通行區域

    {1, 1, 0, 0, 0, 0, 1, 0, 1, 1}, // 可通行區域

    {1, 0, 1, 1, 0, 1, 1, 0, 1, 1}, // 可通行區域

    {1, 0, 1, 1, 0, 0, 0, 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}, // 牆壁區域

  },

  {

    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 牆壁區域

    {1, 0, 1, 1, 0, 0, 1, 0, 0, 1}, // 可通行區域

    {1, 0, 0, 0, 0, 1, 1, 0, 1, 1}, // 可通行區域

    {1, 0, 1, 1, 0, 0, 0, 0, 0, 1}, // 可通行區域

    {1, 0, 1, 1, 0, 1, 1, 0, 1, 1}, // 可通行區域

    {1, 0, 1, 1, 0, 1, 0, 0, 1, 1}, // 可通行區域

    {1, 0, 1, 1, 0, 0, 1, 0, 0, 1}, // 可通行區域

    {1, 0, 1, 1, 0, 1, 1, 1, 0, 1}, // 可通行區域

    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1}, // 可通行區域

    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 牆壁區域

  },

  {

    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 牆壁區域

    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1}, // 可通行區域

    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1}, // 可通行區域

    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1}, // 可通行區域

    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1}, // 可通行區域

    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1}, // 可通行區域

    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1}, // 可通行區域

    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1}, // 可通行區域

    {1, 1, 1, 1, 0, 0, 0, 1, 0, 1}, // 可通行區域

    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 牆壁區域

  },

  {

    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 牆壁區域

    {1, 0, 1, 0, 0, 0, 0, 0, 0, 1}, // 可通行區域

    {1, 0, 1, 0, 1, 1, 1, 1, 0, 1}, // 可通行區域

    {1, 0, 1, 0, 1, 0, 0, 0, 0, 1}, // 可通行區域

    {1, 0, 1, 0, 1, 0, 1, 1, 1, 1}, // 可通行區域

    {1, 0, 1, 0, 1, 0, 0, 0, 0, 1}, // 可通行區域

    {1, 0, 1, 0, 1, 1, 0, 1, 0, 1}, // 可通行區域

    {1, 0, 1, 0, 1, 0, 0, 0, 0, 1}, // 可通行區域

    {1, 0, 0, 0, 1, 1, 0, 1, 0, 1}, // 可通行區域

    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 牆壁區域

  }

};


int startI = 1, startJ = 1, playerI = 1, playerJ = 1, goalI = 8, goalJ = 8;  // 起點、終點及玩家初始位置

boolean gameOver = false;  // 遊戲是否結束

int next=0;


void setup() {

  size(400, 400);  // 設定畫面大小

  noStroke();  // 不使用邊框

  frameRate(15);  // 設定更新幀數

}


void draw() {

  background(255);  // 背景顏色為白色


  // 繪製迷宮

  for (int i = 0; i < maze[next].length; i++) {

    for (int j = 0; j < maze[next][0].length; j++) {

      if (maze[next][i][j] == 1) fill(0); // 牆壁使用黑色

      else fill(255); // 通道使用白色

      rect(j * 40, i * 40, 40, 40);  // 每個格子的大小為40x40

    }

  }

  // 繪製起點 (紅色方塊)

  fill(255, 0, 0);

  rect(startJ * 40, startI * 40, 40, 40);


  // 繪製終點 (綠色方塊)

  fill(0, 255, 0);

  rect(goalJ * 40, goalI * 40, 40, 40);


  // 繪製玩家位置 (藍色方塊)

  if (!gameOver) {

    fill(0, 0, 255);

    rect(playerJ * 40, playerI * 40, 40, 40);

  }


  // 檢查玩家是否到達終點

  if (playerI == goalI && playerJ == goalJ && !gameOver) {

    gameOver = true;  // 設定遊戲結束

  }


  // 如果遊戲結束,顯示通關畫面

  if (gameOver==true) {

    displayWinMessage();  // 顯示通關訊息

  }

}


// 使用鍵盤按鍵來移動玩家

void keyPressed() {

  if (gameOver==true) return;  // 如果遊戲結束,不允許移動


  if (key == 'w' || key == 'W') movePlayer(-1, 0); // 上移

  if (key == 's' || key == 'S') movePlayer(1, 0);  // 下移

  if (key == 'a' || key == 'A') movePlayer(0, -1); // 左移

  if (key == 'd' || key == 'D') movePlayer(0, 1);  // 右移

}


// 移動玩家

void movePlayer(int di, int dj) {

  int newI = playerI + di;

  int newJ = playerJ + dj;


  // 檢查新位置是否在迷宮範圍內,並且是通道

  if (newI >= 0 && newI < maze[next].length && newJ >= 0 && newJ < maze[next][0].length && maze[next][newI][newJ] == 0) {

    playerI = newI;  // 更新玩家的垂直位置

    playerJ = newJ;  // 更新玩家的水平位置

  }

}


// 顯示通關訊息

void displayWinMessage() {

  fill(255, 0, 0);  // 設定文字顏色為黑色

  textSize(32);  // 設定文字大小d

  text("You Win!", 180, 180);  // 顯示通關字樣


  // 繪製重製按鈕

  fill(0, 255, 0);  // 設定按鈕顏色為綠色

  rect(150, 220, 100, 50);  // 按鈕的區域

  fill(255);  // 設定文字顏色為白色

  textSize(16);  // 設定文字大小

  textAlign(CENTER, CENTER);  // 設定文字對齊方式

  text("Restart", 200, 245);  // 顯示重製文字


  fill(255, 0, 0);

  rect(150, 270, 100, 50);  // 按鈕的區域

  fill(255);  // 設定文字顏色為白色

  textSize(16);  // 設定文字大小

  textAlign(CENTER, CENTER);  // 設定文字對齊方式

  text("Next", 200, 295);

}


// 當玩家點擊滑鼠時,檢查是否點擊到重製按鈕

void mousePressed() {

  if (gameOver==true && mouseX > 150 && mouseX < 250 && mouseY > 270 && mouseY < 320) {

    nextGame();  // 如果點擊到重製按鈕,則重設遊戲

  }

  if (gameOver==true && mouseX > 150 && mouseX < 250 && mouseY > 220 && mouseY < 270) {

    resetGame();  // 如果點擊到重製按鈕,則重設遊戲

  }

}


// 重設遊戲

void resetGame() {

  playerI = startI;  // 重設玩家位置為起點

  playerJ = startJ;  // 重設玩家位置為起點

  gameOver = false;  // 設定遊戲狀態為未結束

}

void nextGame() {

  next++;

  playerI = startI;  // 重設玩家位置為起點

  playerJ = startJ;  // 重設玩家位置為起點

  gameOver = false;  // 設定遊戲狀態為未結束

  if (next>3) {

    next = 0;

  }

}


week1717171717

小賴 week17

 

☺HOMEWORK (1) 

2024年12月29日 星期日

比奇堡佳齁勝 10

透過這次的體驗,我知道了關於馬克杯印製的過程和細節。選擇圖案,設計馬克杯,我能夠將個性化的作品展現出來,並且讓人感受到獨特的價值。

最讓我驚喜的是,透過專業工具的進行熱轉印,讓原本簡單的馬克杯變得不再普通,不論是用來送禮還是作為日常使用,謝謝小葉老師帶我們參訪創客基地,讓我們體驗到這些


2024年12月16日 星期一

我好想回家

 //week15_01_circle_collision_class_Ball_new_Ball_display

void setup()

{

  size(640,360);

}

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,360);

}

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

void draw()

{

  ball.update();

  ball.display();

}

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,360);
}
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.update();
  ball.display();
  ball2.update();
  ball2.display();
}
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,360);
}
Ball ball = new Ball(100,200,60);
Ball ball2 = new Ball(300,200,60);
void draw()
{
  background(51);
  fill(255,255,0);
  ball.update();
  ball.display();
  fill(255);
  ball2.update();
  ball2.display();
  if(ball.checkCollision(ball2))fill(#FFAAAA);
  else fill(255);
}
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();
      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)));
      
      println(N.mag());
      
      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.x;
      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,1,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,0,1,1,0,1,1,0,1,1},
  {1,0,0,1,1,0,0,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.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,1,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,0,1,1,0,1,1,0,1,1},
  {1,0,0,1,1,0,0,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.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 mousePressed move
int[][]maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,1,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,0,1,1,0,1,1,0,1,1},
  {1,0,0,1,1,0,0,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_boolean
int[][]maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,1,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,0,1,1,0,1,1,0,1,1},
  {1,0,0,1,1,0,0,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);
}





SY-BlingBangBang week15 點播4

   [TODO]

上課內容:

Collision1

// week15_1_circle_collision_class_Ball_new_Ball_display
// https://processing.org/examples/circlecollision.html
void setup(){
  size(640,360);
}
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);
  }
}



Collision2

[主頁]

// week15_2_circle_collision_class_Ball_update
// https://processing.org/examples/circlecollision.html
void setup(){
  size(640,360);
}
Ball ball = new Ball(100, 200, 20);
void draw(){
  ball.update();//加這行
  ball.display();
}
//把class Ball 移到右邊的新分頁(名字也叫Ball)

[分頁]

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);
  }
}



Collision3

主頁

// week15_3_circle_collision_detection_two_ball
void setup(){
  size(640,360);
}
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.update();
  ball.display();
  ball2.update();//加這行
  ball2.display();//加這行
}
//把class Ball 移到右邊的新分頁(名字也叫Ball)

分頁

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);
  }
}


Collision4

主頁

// week15_4_circle_collision_cos_sin_vector_N_N2_M_M2_swap
void setup(){
  size(640,360);
}
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.update();
  ball.display();
  ball2.update();//加這行
  ball2.display();//加這行
}
//把class Ball 移到右邊的新分頁(名字也叫Ball)

分頁
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(); //maganitude 量值、長度的意思
      float a = diff.heading(); //向量的方向,對應某個角度
      //大家熟的 cos(a), sin(a)
      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 = newV2.y;
      other.vx = newV2.x;
      other.vy = newV2.y;
      return true;
    }
    else return false;
  }
  void update(){
    if(x+vx > 640-r || x+vx < 0+r) vx = -vx;
    if(y+vy < 0+r || y+vy > 360-r) vy = -vy;
    x += vx;
    y += vy;
  }
  void display(){
    ellipse(x, y, r+r, r+r);
  }
}


迷宮1
// week15_5_maze_DFS
int [][] maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,1,1,1,1},
  {1,1,0,1,0,0,1,1,1,1},
  {1,1,0,1,0,0,0,0,1,1},
  {1,1,0,1,0,0,1,1,1,1},
  {1,1,0,0,0,0,1,1,1,1},
  {1,1,0,1,1,1,1,1,1,1},
  {1,1,0,1,1,1,1,1,1,1},
  {1,1,0,0,0,0,0,0,1,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);
    }
  }
}



迷宮2
// week15_6_maze_DFS_visited
int [][] maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,1,1,1,1},
  {1,1,0,1,0,0,1,1,1,1},
  {1,1,0,1,0,0,0,0,1,1},
  {1,1,0,1,0,0,1,1,1,1},
  {1,1,0,0,0,0,1,1,1,1},
  {1,1,0,1,1,1,1,1,1,1},
  {1,1,0,1,1,1,1,1,1,1},
  {1,1,0,0,0,0,0,0,1,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);
}



迷宮3
// week15_7_maze_DFS_visited_step_mousePressed_move
int [][] maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,1,1,1,1},
  {1,1,0,1,0,0,1,1,1,1},
  {1,1,0,1,0,0,0,0,1,1},
  {1,1,0,1,0,0,1,1,1,1},
  {1,1,0,0,0,0,1,1,1,1},
  {1,1,0,1,1,1,1,1,1,1},
  {1,1,0,1,1,1,1,1,1,1},
  {1,1,0,0,0,0,0,0,1,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(){ //按下mouse會往右走 or往下走
  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;
  }
}



迷宮4
// week15_8_maze_DFS_boolean_true_false_ok
int [][] maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,1,1,1,1},
  {1,1,0,1,0,0,1,1,1,1},
  {1,1,0,1,0,0,0,0,1,1},
  {1,1,0,1,0,0,1,1,1,1},
  {1,1,0,0,0,0,1,1,1,1},
  {1,1,0,1,1,1,1,1,1,1},
  {1,1,0,1,1,1,1,1,1,1},
  {1,1,0,0,0,0,0,0,1,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(){ //按下mouse會往右走 or往下走
  visited[1][1] = 0;
  DFS(1,1,1);
}


Week15 還剩三周放假

WEEK15
15-1(生成球)

void setup(){
  size(640,360);
}
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);
   }
}

15-2(生成球)

///main
//week15_02_circle_class_update
void setup(){
  size(640,360);
}
Ball ball = new Ball(100,200,20);
void draw(){
  ball.display();
  ball.update();
}
///Ball
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>360 || y+vy<0) vy = -vy;
    x += vx;
    y += vy;
  }
   void display(){
     ellipse(x,y,r+r,r+r);
   }
}



15-3(2球)

///main
//week15_03_two_ball 
void setup(){
  size(640,360);
}
Ball ball = new Ball(100,200,60);
Ball ball2 =new Ball(300,200,60);
void draw(){
  background(51);
  ball.display();
  ball.update();
  ball2.display();
  ball2.update();
}
///Ball
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>360 || y+vy<0) vy = -vy;
    x += vx;
    y += vy;
  }
   void display(){
     ellipse(x,y,r+r,r+r);
   }
}

15-4(2球碰撞)







15-5(生成迷宮)

//week15_05_maze_2D
int [][] maze = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 1, 1, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 1, 0, 0, 0, 0, 1},
  {1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
  {1, 0, 0, 0, 1, 1, 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.length; j++) {
      if (maze[i][j]==1) fill(0);
      else fill(255);
      rect(j*40, i*40, 40, 40);
    }
  }

}

15-6(走迷宮)

//week15_06_maze_visited
int [][] maze = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 1, 1, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 1, 0, 0, 0, 0, 1},
  {1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
  {1, 0, 0, 0, 1, 1, 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.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);
}

15-7(右下走迷宮)

//week15_07_maze_visited_moved
int [][] maze = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 1, 1, 1, 1, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 1, 0, 0, 0, 0, 1},
  {1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
  {1, 0, 0, 0, 1, 1, 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.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.length; j++) {
      if (visited[i][j]>0) {
      fill(0,150,150);
      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;
  }
}

15-8(一次走完迷宮)

//week15_08_maze_gotoend
int [][] maze = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
  {1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 1, 1},
  {1, 0, 0, 0, 0, 0, 1, 0, 1, 1},
  {1, 0, 1, 0, 1, 0, 1, 0, 0, 1},
  {1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
  {1, 0, 0, 0, 1, 1, 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.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.length; j++) {
      if (visited[i][j]>0) {
      fill(0,150,150);
      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(visited[i][j] > 0)return false;
  if(maze [i][j]==1)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);
}




w151515151515 weeeeek000015

001.

 void setup() {

  size(640, 360);

}

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

Ball ball2 = new Ball(300, 200, 50);


void draw() {

  background(51);

  if( ball.checkCollision(ball2)) fill(#FFAAAA);

  else fill(255);

  ball.update();

  ball.display();

  ball2.update();

  ball2.display();

}

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 > 360 || y+vy < 0) vy = -vy;

    x += vx;

    y += vy;

 

  }

  void display() {

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

  }

}

-----------------------------------------------------------------------------------

002.
void setup() {
  size(640, 360);
}
Ball ball = new Ball(100, 200, 50);
Ball ball2 = new Ball(300, 200, 50);

void draw() {
  background(51);
  if ( ball.checkCollision(ball2)) fill(#FFAAAA);
  else fill(255);
  ball.update();
  ball.display();
  ball2.update();
  ball2.display();
}

--
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) {
      PVector diff = new PVector( other.x-x, other.y-y);
      float len = diff.mag();
      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 > 360 || y+vy < 0) vy = -vy;
    x += vx;
    y += vy;
  }
  void display() {
    ellipse(x, y, r+r, r+r);
  }
}
---------------------------------------------------------
003.
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) {
  println(i, j, 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[I][J] = 0;
  DFS(1, 1, 1);
}

HSY-week15

 //week15-1

void setup(){

  size(640,360);

}

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-2

void setup(){

  size(640,360);

}

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

void draw(){

  ball.update();

  ball.display();

}


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-3

void setup(){

  size(640,360);

}

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

Ball ball2 = new Ball(300,200,60);

void draw(){

  background(51);

  if(ball.checkCollision(ball2) ) fill(#FFAAAA);

  else fill(255);

  ball.update();

  ball.display();

  ball2.update();

  ball.display();

}


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-4

void setup(){

  size(640,360);

}

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

Ball ball2 = new Ball(300,200,60);

void draw(){

  background(51);

  if(ball.checkCollision(ball2) ) fill(#FFAAAA);

  else fill(255);

  ball.update();

  ball.display();

  ball2.update();

  ball2.display();

}


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) {

      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-5&6
int [][] maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,1,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);
}
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-7
int [][] maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,1,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-8
int [][] maze = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,1,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);
  
}

12/16

(1) //week15_01_circle_collision_class_Ball_new_Ball_display

void setup() {
  size(640, 360); 
}
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_upda
void setup() {
  size(640, 360); 
}
Ball ball=new Ball(100, 200, 20);
void draw() {
  ball.update();
  ball.display();
}

**Ball
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, 360); 
}
Ball ball=new Ball(100, 200, 20);
Ball ball2=new Ball(300, 200, 60);
void draw() {
  background(51);
  if(ball.checkCollision(ball2))fill(#FFAAAA);
  else fill(255);
  ball.update();
  ball.display();
  ball2.update();
  ball2.display();
}

**Ball
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, 360); 
}
Ball ball=new Ball(100, 100, 60);
Ball ball2=new Ball(100, 300, 60);
void draw() {
  background(51);
  if(ball.checkCollision(ball2))fill(#FFAAAA);
  else fill(255);
  ball.update();
  ball.display();
  ball2.update();
  ball2.display();
}

**Ball
class Ball {
  float x, y, r;
  float vx, vy;
  Ball(int x0, int y0, int r0) {
    x=x0;
    y=y0;
    r=r0;
    vx=random(-6, +6);
    vy=random(-6, +6);
  }
  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();
      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();
      N.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(vx, 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_2D_array_draw
int [][]maze={
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,1,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,0,1,0,0,0,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,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,1,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,0,1,0,0,0,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,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=I;
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_step_mousePressed_move
int [][]maze={
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,1,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,0,1,0,0,0,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,1,0,1,1},
  {1,1,1,1,1,1,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=I;
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_boolean_true_false_ok
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,0,0,0,1,0,1,0,1,1},
  {1,0,1,1,0,1,1,0,1,1},
  {1,0,1,1,0,0,1,0,1,1},
  {1,0,0,0,0,1,1,0,1,1},
  {1,1,1,1,0,1,1,0,1,1},
  {1,0,0,0,0,0,0,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=I;
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){
  println(i,j,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);
}