2023年10月26日 星期四

UU_week07

2023/10/26 

Happy Halloween

Part1 按下滑鼠可畫線, 放開滑鼠就會清空畫面

void setup() { size(400, 400); background(255); } void draw() { if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); } void mouseReleased() { background(255); }

Part2 創建人物與鬼魂, 鬼魂會往人物移動

鬼在移動的時候會有殘影, 但是移除後就無法完整畫圖, 需要修改

void setup() { size(400, 400); background(255); } float ghostX=400, ghostY=20; void draw() { ellipse(200, 200, 15, 15); if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); ellipse(ghostX, ghostY, 15, 15); float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX+=dx/len/3; ghostY+=dy/len/3; } void mouseReleased() { background(255); }

Part3 新增ArrayList來標點並移除鬼魂的殘影

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup() { size(400, 400); background(255); } float ghostX=400, ghostY=20; void mouseDragged(){ pt.add(new PVector(mouseX, mouseY)); } void draw() { background(255); ellipse(200, 200, 15, 15); // if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); for (int i=0; i<pt.size(); i++){ PVector p = pt.get(i); ellipse(p.x, p.y, 3, 3); } ellipse(ghostX, ghostY, 15, 15); float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX+=dx/len/3; ghostY+=dy/len/3; } void mouseReleased() { background(255); }

Part4

 

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup() { size(400, 400); background(255); } float ghostX=400, ghostY=20; void mouseDragged(){ pt.add(new PVector(mouseX, mouseY)); } void draw() { background(255); ellipse(200, 200, 15, 15); // if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); } ellipse(ghostX, ghostY, 15, 15); float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX+=dx/len/3; ghostY+=dy/len/3; } void mouseReleased() { // background(255); for (int i=pt.size()-1; i>=0; i--){ pt.remove(i); } }

Part5

 

新增兩個變數float dpx=0, dpy=0來計算x, y的移動量, 如果在條件內就變紅色(水平線)

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup() { size(400, 400); background(255); } float ghostX=400, ghostY=20; void mouseDragged(){ pt.add(new PVector(mouseX, mouseY)); } void draw() { background(255); ellipse(200, 200, 15, 15); // if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); strokeWeight(4); float dpx=0, dpy=0; // 要看p.x p2.x的差距, p.y p2.y 的差距 for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); dpx += abs(p2.x - p.x); // 累積x的移動量 dpy += abs(p2.y - p.y); // 累積y的移動量 } if (dpx>100 && dpy<40) stroke(255, 0, 0); // horizonal line (red) else stroke(0); // (black) for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); } ellipse(ghostX, ghostY, 15, 15); float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX+=dx/len/3; ghostY+=dy/len/3; } void mouseReleased() { // background(255); for (int i=pt.size()-1; i>=0; i--){ pt.remove(i); } }

Part6

新增垂直的藍線, 並將主角及鬼魂另外維持黑色 

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup() { size(400, 400); background(255); } float ghostX=400, ghostY=20; void mouseDragged(){ pt.add(new PVector(mouseX, mouseY)); } void draw() { background(255); strokeWeight(1); stroke(0); ellipse(200, 200, 15, 15); stroke(0); ellipse(ghostX, ghostY, 15, 15); // 搬上來才不會影響 // if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); strokeWeight(4); float dpx=0, dpy=0; // 要看p.x p2.x的差距, p.y p2.y 的差距 for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); dpx += abs(p2.x - p.x); // 累積x的移動量 dpy += abs(p2.y - p.y); // 累積y的移動量 } if (dpx>100 && dpy<40) stroke(255, 0, 0); // horizonal line (red) else if (dpx<40 && dpy>100) stroke(0, 0, 255); // vertical line (blue) else stroke(0); // (black) for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); } float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX+=dx/len/3; ghostY+=dy/len/3; } void mouseReleased() { // background(255); for (int i=pt.size()-1; i>=0; i--){ pt.remove(i); } }

Part7

新增綠色^線段

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup() { size(400, 400); background(255); } float ghostX=400, ghostY=20; void mouseDragged(){ pt.add(new PVector(mouseX, mouseY)); } void draw() { background(255); strokeWeight(1); stroke(0); ellipse(200, 200, 15, 15); stroke(0); ellipse(ghostX, ghostY, 15, 15); // 搬上來才不會影響 // if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); strokeWeight(4); float dpx=0, dpy=0; // 要看p.x p2.x的差距, p.y p2.y 的差距 int botton_right=0; // 往右下 for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); dpx += abs(p2.x - p.x); // 累積x的移動量 dpy += abs(p2.y - p.y); // 累積y的移動量 if (p2.x - p.x > 0 && p2.y - p.y > 0) botton_right++; } if (dpx>100 && dpy<40) stroke(255, 0, 0); // horizonal line (red) else if (dpx<40 && dpy>100) stroke(0, 0, 255); // vertical line (blue) else if (botton_right>30) stroke(0, 255, 0); // botton_right (green) else stroke(0); // (black) for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); } float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX+=dx/len/3; ghostY+=dy/len/3; } void mouseReleased() { // background(255); for (int i=pt.size()-1; i>=0; i--){ pt.remove(i); } }

Part8

修改part7

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup() { size(400, 400); background(255); } float ghostX=400, ghostY=20; void mouseDragged(){ pt.add(new PVector(mouseX, mouseY)); } void draw() { background(255); strokeWeight(1); stroke(0); ellipse(200, 200, 15, 15); stroke(0); ellipse(ghostX, ghostY, 15, 15); // 搬上來才不會影響 // if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); strokeWeight(4); float dpx=0, dpy=0; // 要看p.x p2.x的差距, p.y p2.y 的差距 float botton_right=0; // 往右下 for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); dpx += abs(p2.x - p.x); // 累積x的移動量 dpy += abs(p2.y - p.y); // 累積y的移動量 if (p2.x - p.x > 0 && p2.y - p.y > 0) botton_right+=sqrt(dpx*dpx+dpy+dpy); } if (dpx>100 && dpy<40) stroke(255, 0, 0); // horizonal line (red) else if (dpx<40 && dpy>100) stroke(0, 0, 255); // vertical line (blue) else if (botton_right>150) stroke(0, 255, 0); // botton_right (green) else stroke(0); // (black) for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); } float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX+=dx/len/3; ghostY+=dy/len/3; } void mouseReleased() { // background(255); for (int i=pt.size()-1; i>=0; i--){ pt.remove(i); } }

Part9

新增 green_okgreen_part1來確認^的順序是否正確(未完)

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup() { size(400, 400); background(255); } float ghostX=400, ghostY=20; void mouseDragged(){ pt.add(new PVector(mouseX, mouseY)); } void draw() { background(255); strokeWeight(1); stroke(0); ellipse(200, 200, 15, 15); stroke(0); ellipse(ghostX, ghostY, 15, 15); // 搬上來才不會影響 // if (mousePressed) line(mouseX, mouseY, pmouseX, pmouseY); strokeWeight(4); float dpx=0, dpy=0; // 要看p.x p2.x的差距, p.y p2.y 的差距 float botton_right=0, top_right=0; // 往右下, 右上 boolean green_part1 = false, green_ok = false; for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); dpx += abs(p2.x - p.x); // 累積x的移動量 dpy += abs(p2.y - p.y); // 累積y的移動量 if (p2.x - p.x > 0 && p2.y - p.y > 0) botton_right+=sqrt(dpx*dpx+dpy+dpy); if (p2.x - p.x > 0 && p2.y - p.y < 0) top_right+=sqrt(dpx*dpx+dpy+dpy);; ellipse(p.x, p.y, 10, 10); if (green_part1==false && top_right>150) green_part1 = true; else if (green_part1==true && top_right>150) green_ok =true; } if (dpx>100 && dpy<40) stroke(255, 41, 98); // horizonal line (red) else if (dpx<40 && dpy>100) stroke(41, 171, 255); // vertical line (blue) else if (green_ok) stroke(44, 255, 41); // botton_right (green) else stroke(0); // (black) for (int i=0; i<pt.size()-1; i++){ // -1下面要加回來 PVector p = pt.get(i), p2 = pt.get(i+1); // +1對消 line(p.x, p.y, p2.x, p2.y); // ellipse(p.x, p.y, 3, 3); } float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX+=dx/len/3; ghostY+=dy/len/3; } void mouseReleased() { // background(255); for (int i=pt.size()-1; i>=0; i--){ pt.remove(i); } }

Part10

完成修改green line

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup(){ size(400,400); background(255); } float ghostX=40, ghostY=20; void mouseDragged() pt.add(new PVector(mouseX,mouseY)); } void draw(){ background(255); strokeWeight(1); stroke(0);ellipse(200,200,15,15); stroke(0);ellipse(ghostX,ghostY,15,15); strokeWeight(4); float dpx=0, dpy=0; //要看p.x p2.x的差距, p.y p2.y的差距 float rightdown = 0, green_rightup =0; //往右下,往右上 boolean green_part1 = false,green_ok=false; for (int i=0; i<pt.size()-1; i++){ //累積移動量 PVector p=pt.get(i) , p2 = pt.get(i+1); dpx += abs(p2.x - p.x); //累積x的移動量 dpy += abs(p2.y - p.y); //累積y的移動量 if(p2.x - p.x>0 && p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy); if(green_part1 && p2.x - p.x>0 && p2.y-p.y<0) green_rightup+=sqrt(dpx*dpx+dpy*dpy); if(green_part1==false && rightdown>150) green_part1 = true; else if(green_part1==true && green_rightup>150) green_ok=true; } if( dpx>100 && dpy<40) stroke(255,0,0); else if( dpx<40 && dpy>100) stroke(0,0,255); else if(green_ok) stroke(0,255,0); else stroke(0); for (int i=0; i<pt.size()-1; i++){ PVector p=pt.get(i) , p2 = pt.get(i+1); line( p.x, p.y, p2.x, p2.y); } float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX += dx/ len/ 3; ghostY += dy/ len/ 3; } void mouseReleased(){ for(int i=pt.size()-1;i>=0;i--){ pt.remove(i); } }

Part11

依樣畫葫蘆畫出yellow line

ArrayList<PVector> pt = new ArrayList<PVector>(); void setup(){ size(400,400); background(255); } float ghostX=40, ghostY=20; void mouseDragged(){ pt.add(new PVector(mouseX,mouseY)); } void draw(){ background(255); strokeWeight(1); stroke(0);ellipse(200,200,15,15); stroke(0);ellipse(ghostX,ghostY,15,15); strokeWeight(4); float dpx=0, dpy=0; //要看p.x p2.x的差距, p.y p2.y的差距 float rightdown = 0, green_rightup =0; //往右下,往右上 float rightup = 0, yellow_rightdown =0; boolean green_part1 = false,green_ok=false; boolean yellow_part1 = false,yellow_ok=false; for (int i=0; i<pt.size()-1; i++){ //累積移動量 PVector p=pt.get(i) , p2 = pt.get(i +1); dpx += abs(p2.x - p.x); //累積x的移動量 dpy += abs(p2.y - p.y); //累積y的移動量 if(p2.x - p.x>0 && p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy); if(green_part1 && p2.x - p.x>0 && p2.y-p.y<0) green_rightup+=sqrt(dpx*dpx+dpy*dpy); if(p2.x - p.x>0 && p2.y-p.y<0) rightup+=sqrt(dpx*dpx+dpy*dpy); if(yellow_part1 && p2.x - p.x>0 && p2.y-p.y>0) yellow_rightdown+=sqrt(dpx*dpx+dpy*dpy); if(green_part1==false && rightdown>150) green_part1 = true; else if(green_part1==true && green_rightup>150) green_ok=true; if(yellow_part1==false && rightup>150) yellow_part1 = true; else if(yellow_part1==true && yellow_rightdown>150) yellow_ok=true; } if( dpx>100 && dpy<40) stroke(255,0,0); //red horizonal line else if( dpx<40 && dpy>100) stroke(0,0,255); // blue vertical line else if(green_ok) stroke(0,255,0); // green line else if(yellow_ok)stroke(255,255,0);/// yellow line else stroke(0);// black for (int i=0; i<pt.size()-1; i++){ //畫線 PVector p=pt.get(i) , p2 = pt.get(i+1); line( p.x, p.y, p2.x, p2.y); } float dx = 200-ghostX, dy = 200-ghostY, len=sqrt(dx*dx+dy*dy); ghostX += dx/ len/ 3; ghostY += dy/ len/ 3; } void mouseReleased(){ for(int i=pt.size()-1;i>=0;i--){ pt.remove(i); } }

沒有留言:

張貼留言