2020年5月7日 星期四

泡泡排序法/圈圈


今天小葉老師一開始上課的時候先教我們了解泡泡排序法
以下是上圖的程式碼:
int []a=new int[10];//這是java宣告陣列的方式
void setup(){
  size(500,800);
  for(int i=0;i<10;i++){
    a[i]=int(random(10));
  }
  textSize(36);
  textAlign(LEFT,TOP);
  showArray(y);
}
int y=0;
void showArray(int y){
  for(int i=0;i<10;i++){
    fill(255);  rect(i*50, y, 50,50);
    fill(0);         text(a[i], i*50, y);
  }
}
void draw(){

}
void mousePressed(){
  for(int i=0; i<10-1;i++){
    if(a[i] > a[i+1]){
      int temp=a[i];
      a[i]=a[i+1];
      a[i+1]=temp;
      fill(255,0,0,128); rect(i*50, y, 100,50);
    }//   紅,綠,藍,半透明
  }
  y+=50;
  showArray(y);
}

最後一節課老師帶我們繼續做上禮拜沒做完的processing
一開始先複習怎麼把圈圈畫出來

上圖程式碼:
void setup(){
   size(300,500);
   colorMode(HSB);
}
int ansX=2, ansY=3;
int N=5;
int H=255, S=255, B=255;
void draw(){
  background(0);
  int R=300/N, w=R/2;
  for(int x=0;x<N;x++){
     for(int y=0;y<N;y++){
       if(x==ansX&&y==ansY) fill(H+10,S-35,B-45);
       else fill(H,S,B);
        ellipse(0+w+x*R, 200+w+y*R,R,R);
     }
  }
}
void mousePressed(){
   ansX =int(random(N));
   ansY =int(random(N));
   H+=16;
   if(H>255)H=0;
}
今天教了HSB的觀念,讓我們的圈可以變顏色。

沒有留言:

張貼留言