2014年4月21日 星期一

第10週 期中作品展示:用leap motion 和 processing 做視力測量

用leap motion 和 processing 做視力測量
視力表:




心得:
每一層隨機選一個圖像顯示,當leap motion抓取手移動的方向正確時,才會移動到下一層,
方向錯誤的話,就會顯示上一層所測的視力。

怕有bug出現,所以有設一個 key 去控制是否讀取手所在的位置,以免產生重複讀取的bug

然後也設一個範圍,手必須先回到中心,此時才會記錄移動的方向。

當方向都沒錯的話,最後的結果會顯示 1.0。



程式執行實況:





import de.voidplus.leapmotion.*;
LeapMotion leap;
import ddf.minim.*;
Minim minim;
AudioPlayer stop;
PVector handPos;
PImage [] img = new PImage[1];
PImage [] imgw = new PImage[1];
PImage [] eye = new PImage[1];
int test=0,act=0,ref=0,ex=2,right=0,fault=2;
void setup(){
  size(300,300,P3D);
 img[0]=loadImage("data/01.jpg");
 imgw[0]=loadImage("data/02.jpg");
 eye[0]=loadImage("data/eyes.jpg");
 leap = new LeapMotion(this);
 minim = new Minim(this);
  stop = minim.loadFile("data/stop.mp3");
}
void draw(){
  background(255,255,255);

  for(Hand hand:leap.getHands())
  {
   hand.draw();
   handPos = hand.getPosition();

   println("X:"+handPos.x+", Y:"+handPos.y+" , ex= "+ex);
   if(handPos.x>250 && right==1)
   {
     ref=1;
   }
   if(handPos.x<70 && right==1)
   {
     ref=3;
   }
   if(handPos.y>250 && right==1)
   {
     ref=2;
   }
   if(handPos.y<70 && right==1)
   {
    ref=4;
   }
 
   if(handPos.x>130 && handPos.x<170 && handPos.y>130 && handPos.y<170)              //return 0
   {
    right=1;
    ref=0;
   }
 
 
  }


  if(fault==2)
  {
    image(eye[0],0,0,300,300);
    ArrayList<Finger>fingers = leap.getFingers();
    if(fingers.size()>3)
    {
      fault=0;
    }
  }


  if(fault==1)          // wrong direction result
  {
          if(test==1)
            {
         
            image(img[0],100,-250,740,1720);
            image(imgw[0],0,0,300,100);
         
         
                 
            }
          else if(test==2)
            {
           
            image(img[0],100,-450,740,1720);
            image(imgw[0],0,200,300,300);
           
                     
            }
          else if(test==3)
            {
           
            image(img[0],100,-600,740,1720);
            image(imgw[0],0,0,300,100);
            image(imgw[0],0,200,300,300);
            image(imgw[0],200,0,300,300);
                     
            }
          else if(test==4)
            {
           
            image(img[0],100,-750,740,1720);
            image(imgw[0],0,0,300,100);
            image(imgw[0],0,200,300,300);
            image(imgw[0],200,0,300,300);        
         
            }
          else if(test==5)
            {
           
            image(img[0],100,-900,740,1720);
            image(imgw[0],0,0,300,100);
            image(imgw[0],0,200,300,300);
            image(imgw[0],200,0,300,300);
         
            }
          else if(test==6)
            {
            image(img[0],100,-1000,740,1720);
            image(imgw[0],0,0,300,100);
            image(imgw[0],0,200,300,300);
            image(imgw[0],200,0,300,300);

            }
          else
          {
            image(img[0],100,-1100,740,1720);
            image(imgw[0],0,0,300,100);
            image(imgw[0],0,180,300,300);
            image(imgw[0],200,0,300,300);
         
          }
 
  }



  if(fault==0)
  {
          if(test==0)
          {
            act=1;
         
            image(img[0],0,-50,370,860);
            image(imgw[0],0,200,300,100);
            image(imgw[0],0,0,300,100);
            if(ref==act && right==1)                     //use right control next step
            {
              test++;
              right=0;
           
            }
         
             if(ref==2 && right==1)              ///wrong direction
            {
              fault=1;
              stop.play();
            }
             if(ref==3 && right==1)              ///wrong direction
            {
             fault=1;
             stop.play();
            }
             if(ref==4 && right==1)              ///wrong direction
            {
              fault=1;
              stop.play();
            }
         
          }
          /////////////////////////////////////////
          if(test==1)
          {
            if(ex==2)
            {
              ex=int(random(2));
            }
            if(ex==0)
            {
              act=2;
              image(img[0],0,-150,370,860);
              image(imgw[0],0,200,300,100);
              image(imgw[0],0,0,300,100);
              image(imgw[0],200,0,300,300);
           
              if(ref==act && right==1)
                {
                  test++;
                  ex=2;
                  right=0;
                }
             
              if(ref==1 && right==1)              ///wrong direction
                {
                  fault=1;
                }
              if(ref==3 && right==1)              ///wrong direction
                {
                 fault=1;
                }
              if(ref==4 && right==1)              ///wrong direction
                {
                  fault=1;
                }
           
            }
            else if(ex==1)
            {
              act=3;
              image(img[0],0,-150,370,860);
              image(imgw[0],0,200,300,100);
              image(imgw[0],0,0,300,100);
              image(imgw[0],50,0,130,300);
           
              if(ref==act && right==1)
                {
                  test++;
                  ex=2;
                  right=0;
                }
             
              if(ref==2 && right==1)              ///wrong direction
                {
                  fault=1;
                }
              if(ref==1 && right==1)              ///wrong direction
                {
                 fault=1;
                }
              if(ref==4 && right==1)              ///wrong direction
                {
                  fault=1;
                }
               
             
            }
          }
       
          /////////////////////////////////////////
          if(test==2)
          {
             if(ex==2)
            {
              ex=int(random(2));
            }
         
            if(ex==0)
            {
              act=1;
              image(img[0],0,-240,370,860);
              image(imgw[0],0,180,300,200);
              image(imgw[0],0,0,300,100);
           
              image(imgw[0],50,0,150,300);
              if(ref ==act && right==1)
                {
                   test++;
                   ex=2;
                   right=0;
                }
             
              if(ref==2 && right==1)              ///wrong direction
                {
                  fault=1;
                }
              if(ref==3 && right==1)              ///wrong direction
                {
                 fault=1;
                }
              if(ref==4 && right==1)              ///wrong direction
                {
                  fault=1;
                }
               
             
            }
            else if(ex==1)
            {
               act=4;
               image(img[0],0,-240,370,860);
               image(imgw[0],0,180,300,200);
               image(imgw[0],0,0,300,100);
               image(imgw[0],150,0,300,300);
               if(ref ==act && right==1)
                {
                   test++;
                   ex=2;
                   right=0;
                }
              if(ref==2 && right==1)              ///wrong direction
                {
                  fault=1;
                }
              if(ref==3 && right==1)              ///wrong direction
                {
                 fault=1;
                }
              if(ref==1 && right==1)              ///wrong direction
                {
                  fault=1;
                }
     
            }
         
          }
       
          //////////////////////////////////////////
          if(test==3)
          {
             if(ex==2)
            {
              ex=int(random(2));
            }
            if(ex==0)
            {
                act=2;
                image(img[0],0,-310,370,860);
                image(imgw[0],0,180,300,200);
                image(imgw[0],0,0,300,100);
             
                image(imgw[0],50,0,200,300);
                if(ref==act && right==1)
                  {
                     test++;
                     ex=2;
                     right=0;
                  }
               
              if(ref==1 && right==1)              ///wrong direction
                {
                  fault=1;
                }
              if(ref==3 && right==1)              ///wrong direction
                {
                 fault=1;
                }
              if(ref==4 && right==1)              ///wrong direction
                {
                  fault=1;
                }
                 
            }
         
            else if(ex==1)
            {
                act=3;
                image(img[0],0,-310,370,860);
                image(imgw[0],0,180,300,200);
                image(imgw[0],0,0,300,100);
                image(imgw[0],250,0,200,300);
                image(imgw[0],50,0,150,300);
             
                 if(ref==act && right==1)
                  {
                     test++;
                     ex=2;
                     right=0;
                  }
               
                if(ref==2 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
                if(ref==1 && right==1)              ///wrong direction
                  {
                   fault=1;
                  }
                if(ref==4 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
             
            }
          }
       
          ///////////////////////////////////////////
          if(test==4)
          {
             if(ex==2)
            {
              ex=int(random(2));
            }
            if(ex==0)
            {
                act=1;
                image(img[0],0,-380,370,860);
                image(imgw[0],0,180,300,200);
                image(imgw[0],0,0,300,100);
                image(imgw[0],150,0,300,300);
                image(imgw[0],50,0,50,300);
             
                if(ref==act && right==1)
                   {
                     test++;
                     ex=2;
                     right=0;
                   }
                if(ref==2 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
                if(ref==3 && right==1)              ///wrong direction
                  {
                   fault=1;
                  }
                if(ref==4 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
                   
            }
            else if(ex==1)
            {
                act=4;
                image(img[0],0,-380,370,860);
                image(imgw[0],0,180,300,200);
                image(imgw[0],0,0,300,100);
                image(imgw[0],50,0,200,300);
             
                 if(ref==act && right==1)
                  {
                     test++;
                     ex=2;
                     right=0;
                  }
                if(ref==2 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
                if(ref==3 && right==1)              ///wrong direction
                  {
                   fault=1;
                  }
                if(ref==1 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
             
            }
          }
       
          //////////////////////////////////////////////
          if(test==5)
          {
             if(ex==2)
            {
              ex=int(random(2));
            }
            if(ex==0)
            {
                act=2;
                image(img[0],0,-440,370,860);
                image(imgw[0],0,160,300,200);
                image(imgw[0],0,0,300,100);
                image(imgw[0],280,0,300,300);
                image(imgw[0],60,0,160,300);
                 
                if(ref==act && right==1)
                    {
                       test++;
                       ex=2;
                       right=0;
                    }
                if(ref==1 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
                if(ref==3 && right==1)              ///wrong direction
                  {
                   fault=1;
                  }
                if(ref==4 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
                   
            }
         
            else if(ex==1)
            {
                act=3;
                image(img[0],0,-440,370,860);
                image(imgw[0],0,160,300,200);
                image(imgw[0],0,0,300,100);
                image(imgw[0],0,0,300,100);
                image(imgw[0],200,0,300,300);
                image(imgw[0],50,0,100,300);
             
                 if(ref==act && right==1)
                    {
                       test++;
                       ex=2;
                       right=0;
                    }
                  if(ref==2 && right==1)              ///wrong direction
                    {
                      fault=1;
                    }
                  if(ref==1 && right==1)              ///wrong direction
                    {
                     fault=1;
                    }
                  if(ref==4 && right==1)              ///wrong direction
                    {
                      fault=1;
                    }
                   
            }
         
          }
       
          ///////////////////////////////////////////////
           if(test==6)
           {
              if(ex==2)
            {
              ex=int(random(2));
            }
            if(ex==0)
            {
                act=4;
                image(img[0],0,-480,370,860);
                image(imgw[0],0,160,300,200);
                image(imgw[0],0,0,300,100);
                image(imgw[0],170,0,300,300);
                image(imgw[0],50,0,70,300);
             
                if(ref==act && right==1)
                   {
                     test++;
                     ex=2;
                     right=0;
                     fault=1;
                   }
                if(ref==2 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
                if(ref==3 && right==1)              ///wrong direction
                  {
                   fault=1;
                  }
                if(ref==1 && right==1)              ///wrong direction
                  {
                    fault=1;
                  }
                   
            }
            else if(ex==1)
            {
                 act=3;
                 image(img[0],0,-480,370,860);
                 image(imgw[0],0,160,300,200);
                 image(imgw[0],0,0,300,100);
                 image(imgw[0],50,0,200,300);
                 image(imgw[0],280,0,200,300);
               
                 if(ref==act && right==1)
                    {
                       test++;
                       ex=2;
                       right=0;
                       fault=1;
                    }
                  if(ref==2 && right==1)              ///wrong direction
                    {
                      fault=1;
                    }
                  if(ref==1 && right==1)              ///wrong direction
                    {
                     fault=1;
                    }
                  if(ref==4 && right==1)              ///wrong direction
                    {
                      fault=1;
                    }
                   
            }
          }
  }




}

void keyPressed(){
 if(keyCode == UP)
  {
    test+=1;
  }


}

沒有留言:

張貼留言