2023年10月12日 星期四

week05

 

WEEK 05-1



1.使用最新版processing,並應用其3D功能 (P3D)畫出一個可被滑鼠移動的立體方塊

void setup()

{

   size(400,400,P3D); 

}

void draw()

{

   background(#FFFFF2);

   pushMatrix();

   translate(mouseX,mouseY);

   box(100);

   popMatrix();

}



2.利用rotate的機制來改變方塊移動時的旋轉方向,並透過fill來為方塊進行填色,並在多加一個僅外框架的無色方塊跟著顏色方塊移動
void draw()
{
   background(#FFFFF2);
   pushMatrix();
   translate(mouseX,mouseY);
   rotateY(radians(mouseX));
   fill(0,255,0);
   box(100);
   noFill();
   scale(2);
   box(100);
   popMatrix();
}



3.利用vertex座標節點進行圖形連接,創作出一個三角形並用fill在3個不同vertex座標進行填色

size(410,410,P3D);
beginShape();
fill(255,0,0);
vertex(200,0);
fill(0,255,0);
vertex(0,400);
fill(0,0,255);
vertex(400,400);
endShape();





WEEK 05-2


4.學習新語法ArrayList<PVector>【大的資料結構】,利用滑鼠點擊來產生圓圈在畫面上

ArrayList<PVector> pt;

void setup()

{

   size(400,400,P3D); 

   pt =new ArrayList<PVector>();

}

void draw()

{

   background(#FFFFF2);

   for(PVector p:pt)

   {

      ellipse( p.x , p.y ,10 ,10 ); 

   }

}

void mousePressed()

{

   pt.add(new PVector(mouseX,mouseY)); 

}


5.透過vertex座標節點來進行繪圖,結合上個程式,在點擊後會來下圓圈表示點擊處座標,再點擊第三個以上的座標後會開始填充圖形。

ArrayList<PVector> pt;

void setup()

{

   size(400,400,P3D); 

   pt =new ArrayList<PVector>();

}

void draw()

{

   background(#FFFFF2);

   for(PVector p:pt)

   {

      ellipse(p.x,p.y,10,10) ;

   }

   beginShape();

   for(PVector p:pt)

   {

      vertex( p.x , p.y ); 

   }

   endShape();

}

void mousePressed()

{

   pt.add(new PVector(mouseX,mouseY)); 

}



6.多加一個可以旋轉展示的功能,透過translate、rotate來實現,在按住右鍵時會讓圖形原地旋轉。

pushMatrix();

   if(mousePressed&&mouseButton==RIGHT)

   {

      translate(200,200);

      rotateY(radians(frameCount));

      translate(-200,-200);

   }

   for(PVector p:pt)

   {

      ellipse(p.x,p.y,10,10) ;

   }

   beginShape();

   for(PVector p:pt)

   {

      vertex( p.x , p.y ); 

   }

   endShape();

   popMatrix();



WEEK 05-3


7.在按下滑鼠左中右鍵時改變背景顏色並在命令列提醒按下哪一個按鍵



8.產生一列圓圈,並在點擊指定圓圈後會特別標示那個圓圈,即是在點擊圓圈的外圍再添一個外框圓圈

ArrayList<PVector>pt;

void setup()

{

   size(400,400,P3D);

   pt=new ArrayList<PVector>();

   for(int i=0;i<20;i++)

   {

      pt.add(new PVector(i*20,20)); 

   }

}

void draw()

{

   background(255);

   for(PVector p:pt)

   {

      ellipse(p.x,p.y,10,10); 

   }

   if(ans!=null)ellipse(ans.x,ans.y,15,15);

}

PVector ans = null;

void mousePressed()

{

   for(PVector p:pt)

   {

      if(dist(p.x,p.y,mouseX,mouseY)<5)

      {

          ans=p;

      }

   }

}


9.隨機產生任意位置的圓圈,並對圓圈按下滑鼠中鍵時會標記點擊的圓圈,若是按住中鍵則可以拖曳圓圈移動

ArrayList<PVector>pt;

void setup()

{

   size(400,400,P3D);

   pt=new ArrayList<PVector>();

   for(int i=0;i<20;i++)

   {

      pt.add(new PVector(random(400),random(400))); 

   }

}

void draw()

{

   background(255);

   for(PVector p:pt)

   {

      ellipse(p.x,p.y,10,10); 

   }

   if(ans!=null)ellipse(ans.x,ans.y,15,15);

}

PVector ans = null;

void mouseDragged()

{

    if(mouseButton==CENTER && ans!=null)

    {

       ans.x=mouseX;

       ans.y=mouseY;

    }

}

void mousePressed()

{

   if(mouseButton==LEFT)pt.add(new PVector(mouseX,mouseY));

   else if(mouseButton==CENTER)

   {

     for(PVector p:pt)

     {

        if(dist(p.x,p.y,mouseX,mouseY)<5)

        {

            ans=p;

        }

     }

   }

}



10.添加刪除功能,對圓圈按下右鍵時會刪除點擊的圓圈

else if(mouseButton==RIGHT)

   {

       for(int i=0;i<pt.size();i++)

       {

        PVector p =pt.get(i);

        if(dist(p.x,p.y,mouseX,mouseY)<5)

        {

           pt.remove(i); 

        }

       }

   }


完整程式碼:

ArrayList<PVector>pt;

void setup()

{

   size(400,400,P3D);

   pt=new ArrayList<PVector>();

   for(int i=0;i<20;i++)

   {

      pt.add(new PVector(random(400),random(400))); 

   }

}

void draw()

{

   background(255);

   for(PVector p:pt)

   {

      ellipse(p.x,p.y,10,10); 

   }

   if(ans!=null)ellipse(ans.x,ans.y,15,15);

}

PVector ans = null;

void mouseDragged()

{

    if(mouseButton==CENTER && ans!=null)

    {

       ans.x=mouseX;

       ans.y=mouseY;

    }

}

void mousePressed()

{

   if(mouseButton==LEFT)pt.add(new PVector(mouseX,mouseY));

   else if(mouseButton==CENTER)

   {

      for(PVector p:pt)

      {

        if(dist(p.x,p.y,mouseX,mouseY)<5)

        {

            ans=p;

        }

      }

   }

   else if(mouseButton==RIGHT)

   {

       for(int i=0;i<pt.size();i++)

       {

        PVector p =pt.get(i);

        if(dist(p.x,p.y,mouseX,mouseY)<5)

        {

           pt.remove(i); 

        }

       }

   }

}















沒有留言:

張貼留言