WEEK 07
1.寫出可自由畫線且會淨空畫面的程式
2.做出一個鬼會靠近玩家(正中心)的程式,不過鬼會留下黑色軌跡
程式碼:
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;
}
3.改變繪畫軌跡,讓鬼往玩家移動所殘留的黑影清掉,但會影響到滑鼠的繪畫,從線條變成了小圓圈
4.改善繪畫線條時不會消失的狀況,滑鼠放開後那些小圈圈會清除
程式碼:
void mouseReleased()
{
for(int i=pt.size()-1;i>=0;i--)
{
pt.remove(i);
}
}
4-1.改善滑鼠繪畫出小圓圈的狀況,將繪畫黑色線條改回來
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);
//ellipse(p.x,p.y,3,3);
}
5.將線條全加粗,但是線條在x軸拉到一定長度時,線條會變紅
程式碼:
void draw()
{
background(255);
ellipse(200,200,15,15);
ellipse(ghostX,ghostY,15,15);
strokeWeight(4);
float dpx=0,dpy=0;
//if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);
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);
dpy +=abs(p2.y-p.y);
//line(p.x,p.y,p2.x,p2.y);
//ellipse(p.x,p.y,3,3);
}
if(dpx>100&&dpy<40)stroke(255,0,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;
}
6.新增線條顏色反饋,當x或y軸到一定長度會讓線條變色,表示線條畫在x或y軸條件內
程式碼:
if(dpx<100&&dpy>40)stroke(0,255,0);
else if(dpx>40&&dpy<100)stroke(255,0,0);
7.新增一個傾斜到一定長度後會特別變色的偵測,即V字形
程式碼:
int rightdown=0;
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);
dpy +=abs(p2.y-p.y);
if(p2.x-p.x>0&&p2.y-p.y>0)rightdown++;
}
else if (rightdown>30)stroke(0,255,0);
8.優化傾斜字線條的偵測判別
if(p2.x-p.x>0&&p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy);
9.優化傾斜V字型偵測判別,現在只畫單邊斜線不會直接變色,要在畫出另一個朝上的線材會變色
float rightdown=0,rightup=0;
boolean green_partl = false,green_ok=false;
if(p2.x-p.x>0&&p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy);
if(p2.x-p.x>0&&p2.y-p.y<0) rightup+=sqrt(dpx*dpx+dpy*dpy);
if(green_partl==false&&rightdown>150)green_partl=true;
else if(green_partl==true&&rightup>150)green_ok=true;
println(rightdown);
else if(green_ok)stroke(0,255,0);
9-A.多加一個倒傾斜V字形判別偵測
相關程式碼:
if(green_partl && p2.x-p.x>0&&p2.y-p.y<0) green_rightup+=sqrt(dpx*dpx+dpy*dpy);
else if(green_partl==true&&green_rightup>150)green_ok=true;
else if(green_partl)stroke(0,128,0);
10-B.優化倒V傾斜字形顏色判別式,多加一個判別式,在偵測到倒V字形時會特別變色
完整程式碼:
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);
strokeWeight(4);
float dpx=0,dpy=0;
float rightdown=0,green_rightup=0;
float rightup=0,yellow_rightdown=0;
boolean green_partl = false,green_ok=false;
boolean yellow_partl = 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);
dpy +=abs(p2.y-p.y);
if(p2.x-p.x>0&&p2.y-p.y>0) rightdown+=sqrt(dpx*dpx+dpy*dpy);
if(green_partl && 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_partl && p2.x-p.x>0 && p2.y-p.y>0)yellow_rightdown+=sqrt(dpx*dpx+dpy*dpy);;
//ellipse(p.x,p.y,10,10);
if(green_partl==false&&rightdown>150)green_partl=true;
else if(green_partl==true&&green_rightup>150)green_ok=true;
if(yellow_partl==false&&rightup>150)yellow_partl=true;
else if(yellow_partl==true&&yellow_rightdown>150)yellow_ok=true;
}
println(rightdown);
if(dpx<100&&dpy>40)stroke(0,0,255);
else if(dpx>40&&dpy<100)stroke(255,0,0);
else if(green_ok)stroke(0,255,0);
else if(yellow_ok)stroke(255,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);
}
}
沒有留言:
張貼留言