2016年3月10日 星期四

02160615_成庭萱_week03

第一節課

期中作品:視力檢查

1.先到Leap Motion網站安裝軟體 Processing下載LeapMotion for Processing的library
2.複習上星期教的程式

















import de.voidplus.leapmotion.*;  //LeapMotion標頭檔
LeapMotion leap;
PVector [][] pt = new PVector[5][50];
//PVector pt1,pt2,pt3,pt4,pt5,pt6,pt7,pt8,pt9,pt10;
void setup() {
  size(640, 480, P3D);
  colorMode(HSB, 100);
  for (int f=0; f<5; f++)
    for (int i=0; i<50; i++)
      pt[f][i] = new PVector(0, 0, 0);
  leap = new LeapMotion(this);  //LeapMotion起始值
}
void draw() {
  background(0);
  //stroke(48,191,153);
  stroke(#30BF99);
  for (int f=0; f<5; f++)
    for (int i=49; i>0; i--) {
      pt[f][i].x=pt[f][i-1].x;
      pt[f][i].y=pt[f][i-1].y;
    }
  for (Hand hand : leap.getHands ()) {  //抓取手
    for (Finger finger : hand.getFingers ()) {  //抓取手指
      int f=finger.getType();
      PVector now = finger.getPosition();
      pt[f][0].x = now.x;
      pt[f][0].y = now.y;
    }
  }
  for (int f=0; f<5; f++) {
    stroke(f*20, 100, 100);
    for (int i=1; i<10; i++)
      line(pt[f][i].x, pt[f][i].y, pt[f][i].z, pt[f][i-1].x, pt[f][i-1].y, pt[f][i-1].z);
  }

}

3.把上週程式改寫成可3D畫線
   且對y軸做旋轉

















import de.voidplus.leapmotion.*;  //LeapMotion標頭檔
LeapMotion leap;
PVector [][] pt = new PVector[5][50];
void setup() {
  size(640, 480, P3D);
  colorMode(HSB, 100);
  for (int f=0; f<5; f++)
    for (int i=0; i<50; i++)
      pt[f][i] = new PVector(0, 0, 0);
  leap = new LeapMotion(this);  //LeapMotion起始值
}
void draw() {
  background(0);
  stroke(#30BF99);
  for (int f=0; f<5; f++)
    for (int i=49; i>0; i--) {
      pt[f][i].x=pt[f][i-1].x;
      pt[f][i].y=pt[f][i-1].y;
      pt[f][i].z=pt[f][i-1].z;
    }
  for (Hand hand : leap.getHands ()) {  //抓取手
    for (Finger finger : hand.getFingers ()) {  //抓取手指
      int f=finger.getType();
      PVector now = finger.getPosition();
      pt[f][0].x = now.x;
      pt[f][0].y = now.y;
      if (f==1)
        println(now);
    }
  }
  translate(width/2, height/2);
  rotateY(radians(frameCount));
  translate(-width/2, -height/2);
  for (int f=0; f<5; f++) {
    stroke(f*20, 100, 100);
    for (int i=1; i<10; i++)
      line(pt[f][i].x, pt[f][i].y, pt[f][i].z, pt[f][i-1].x, pt[f][i-1].y, pt[f][i-1].z);
  }
}


第二節課

1.把剛剛的程式寫得更完整
   且加上格子





























import de.voidplus.leapmotion.*;  //LeapMotion標頭檔
LeapMotion leap;
PVector [][] pt = new PVector[5][50];
void setup() {
  size(640, 480, P3D);
  colorMode(HSB, 100);
  for (int f=0; f<5; f++)
    for (int i=0; i<50; i++)
      pt[f][i] = new PVector(0, 0, 0);
  leap = new LeapMotion(this);  //LeapMotion起始值
}
void draw() {
  background(0);
  stroke(#30BF99);
  for (int f=0; f<5; f++)
    for (int i=49; i>0; i--) {
      pt[f][i].x=pt[f][i-1].x;
      pt[f][i].y=pt[f][i-1].y;
      pt[f][i].z=pt[f][i-1].z;
    }
  for (Hand hand : leap.getHands ()) {  //抓取手
    for (Finger finger : hand.getFingers ()) {  //抓取手指
      int f=finger.getType();
      PVector now = finger.getPosition();
      pt[f][0].x = now.x;
      pt[f][0].y = now.y;
      pt[f][0].z = now.z*2-90;  //要自己試很多值
      if (f==1)
        println(now);
    }
  }
  translate(width/2, height/2, 0);
  rotateY(radians(frameCount/4.0));  //除4會轉慢一點
  translate(-width/2, -height/2, 0);
  for (int f=0; f<5; f++) {
    for (int i=1; i<50; i++) {
      stroke(f*20, 100, 100);
      line(pt[f][i].x, pt[f][i].y, pt[f][i].z, pt[f][i-1].x, pt[f][i-1].y, pt[f][i-1].z);
    }
  }
  stroke(60, 100, 100);
  for (int i=0; i<640; i+=20)  //畫格子
    for (int j=0; j<480; j+=20) {
      noFill();
      rect(i, j, 20, 20);
    }
}


2.兩隻手圖案不一樣















左手是橢圓  右手是長方形

import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup() {
  size(640, 480, P3D);
  leap = new LeapMotion(this);
}
void draw() {
  background(255);
  for (Hand hand : leap.getHands ()) {
    PVector pos = hand.getPosition();
    if (hand.isLeft()) {
      ellipse(pos.x, pos.y, 200, 100);
    } else if (hand.isRight()) {
      rect(pos.x, pos.y, 10, 100);
    }
  }
}


第三節課

1.左手是調色盤  右手是長方形















import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup() {
  size(640, 480, P3D);
  leap = new LeapMotion(this);
}
color nowColor=color(255, 0, 0);
PVector leftPos;
void draw() {
  background(255);
  for (Hand hand : leap.getHands ()) {
    PVector pos = hand.getPosition();
    if (hand.isLeft()) {
      noFill(); ellipse(pos.x, pos.y, 200, 100);
      fill(255, 0, 0); ellipse(pos.x+150, pos.y, 60, 50);
      fill(255, 255, 0); ellipse(pos.x+120, pos.y-40, 60, 50);
      fill(0, 255, 0); ellipse(pos.x+90, pos.y-60, 60, 50);
      fill(0, 0, 255); ellipse(pos.x+30, pos.y-60, 60, 50);
      fill(255, 0, 255); ellipse(pos.x-20, pos.y-60, 60, 50);
      leftPos=pos;
    } else if (hand.isRight()) {
      fill(nowColor); rect(pos.x, pos.y, 10, 100);
      println(leftPos);
      println(pos);
      if (dist(pos.x, pos.y, leftPos.x+120, leftPos.y-40)<30) {
        nowColor=color(255, 255, 0);
      }
    }
  }
}

沒有留言:

張貼留言