-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gui_element Add Animation
- Loading branch information
Showing
6 changed files
with
203 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Button extends Gui_element{ | ||
ButtonEvent e; | ||
int x,y,w,h; | ||
color button_color; | ||
boolean disable=false; | ||
String str; | ||
Button(int _x, int _y, int _w, int _h, color _c, String _s, ButtonEvent _e){ | ||
x=_x;y=_y;w=_w;h=_h;button_color=_c;str=_s;e=_e; | ||
} | ||
void draw(){ | ||
if(disable){ | ||
fill(200); | ||
}else if(mouse_over){ | ||
fill(255,0,0); | ||
}else { | ||
fill(button_color); | ||
} | ||
noStroke(); | ||
rect(x,y,w,h); | ||
textSize(10); | ||
fill(0, 0, 0); | ||
textAlign(CENTER,CENTER); | ||
text(str,x+w/2,y+h/2); | ||
} | ||
void mouseMoved(){ | ||
mouse_over=(mouseX>=x&&mouseX<=x+w&&mouseY>=y&&mouseY<=(y+h)); | ||
if(mouse_over)gui_current=this; | ||
} | ||
void mousePressed(){ | ||
if(e!=null&&!disable) | ||
e.onclick(); | ||
} | ||
} | ||
interface ButtonEvent{ | ||
void onclick(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class Edge { | ||
class Edge extends Gui_element{ | ||
Vertex v1,v2; | ||
Edge(Vertex _v1, Vertex _v2){ | ||
v1=_v1;v2=_v2; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
abstract class Gui_element{ | ||
boolean mouse_over=false; | ||
void draw(){}; | ||
void mouseMoved(){}; | ||
void mousePressed(){}; | ||
void mouseDragged(){}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,20 @@ | ||
class Vertex{ | ||
class Vertex extends Gui_element{ | ||
int x,y; | ||
boolean mouseover=false; | ||
ArrayList<Edge> E=new ArrayList(); | ||
Vertex(int _x,int _y){ | ||
x=_x;y=_y; | ||
} | ||
Vertex(int _x,int _y,boolean _mo){ | ||
x=_x;y=_y;mouseover=_mo; | ||
if(_mo)vertex_under_mouse=this; | ||
} | ||
void draw(){ | ||
noStroke(); | ||
if(mouseover){ | ||
if(mouse_over){ | ||
fill(0,255,0); | ||
}else{ | ||
fill(255,0,0); | ||
} | ||
ellipse(x,y,Vertex_Rad*2,Vertex_Rad*2); | ||
} | ||
void checkmouseover(){ | ||
if(sqrt((mouseX-x)*(mouseX-x)+(mouseY-y)*(mouseY-y))<=Vertex_Rad*2){ | ||
mouseover=true; | ||
vertex_under_mouse=this; | ||
}else{ | ||
mouseover=false; | ||
} | ||
void mouseMoved(){ | ||
mouse_over=(sqrt((mouseX-x)*(mouseX-x)+(mouseY-y)*(mouseY-y))<=Vertex_Rad*2); | ||
if(mouse_over)gui_current=this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,191 @@ | ||
import papaya.*; | ||
|
||
|
||
final int DRAW_NORMAL=1; | ||
final int DRAW_DRAG=2; | ||
final int DRAW_ANIMATION=3; | ||
final int Vertex_Rad=8; | ||
// define parameters for rectangular button | ||
int rectX1=500, rectY1=500; | ||
int rectX2=500, rectY2=550; | ||
int rectwidth=80; | ||
int rectheight=40; | ||
color rectColor; | ||
color rectHighlight; | ||
boolean rectover=false; | ||
|
||
ArrayList<Gui_element> gui=new ArrayList(); | ||
Gui_element gui_current=null; | ||
Vertex drag_start; | ||
Vertex vertex_under_mouse; | ||
Graph G=new Graph(); | ||
Button draw_button=new Button(500, 450, 80, 40, color(150), "Tutte Draw", new TutteDrawEvent()); | ||
float[][] orig,targ; | ||
int animation_start_time=0; | ||
final int animation_duration=1000; | ||
void setup() { | ||
size(600, 600); | ||
background(255); | ||
rectColor=(150); | ||
rectHighlight=color(255,0,0); | ||
rectX1=500; | ||
rectY1=500; | ||
noStroke(); | ||
draw_button.disable=true; | ||
gui.add(draw_button); | ||
gui.add(new Button(500, 500, 80, 40, color(150), "Initial Graph", new InitialGraphEvent())); | ||
gui.add(new Button(500, 550, 80, 40, color(150), "Reset", new ResetEvent())); | ||
} | ||
Graph G=new Graph(); | ||
int draw_state=DRAW_NORMAL; | ||
|
||
void draw() { | ||
background(255); | ||
//Draw everything else | ||
//Draw Vertices | ||
for(Vertex i:G.V){ | ||
i.draw(); | ||
if(draw_state==DRAW_ANIMATION){ | ||
if(millis()-animation_start_time>animation_duration){ | ||
for(int i=0;i<G.V.size();++i){ | ||
G.V.get(i).x=(int)targ[i][0]; | ||
G.V.get(i).y=(int)targ[i][1]; | ||
} | ||
draw_state=DRAW_NORMAL; | ||
}else{ | ||
for(int i=0;i<G.V.size();++i){ | ||
G.V.get(i).x=(int)((targ[i][0]-orig[i][0])*(millis()-animation_start_time)/animation_duration+orig[i][0]); | ||
G.V.get(i).y=(int)((targ[i][1]-orig[i][1])*(millis()-animation_start_time)/animation_duration+orig[i][1]); | ||
} | ||
} | ||
} | ||
|
||
mouse_over_BUTTON(); | ||
|
||
//Draw Edges | ||
strokeWeight(2); | ||
for(Edge i:G.E){ | ||
for (Gui_element i : gui) { | ||
i.draw(); | ||
} | ||
if(draw_state==DRAW_DRAG){ | ||
if (draw_state==DRAW_DRAG) { | ||
strokeWeight(2); | ||
stroke(0,0,255); | ||
line(drag_start.x,drag_start.y,mouseX,mouseY); | ||
stroke(0, 0, 255); | ||
line(drag_start.x, drag_start.y, mouseX, mouseY); | ||
|
||
//println("DRAW_DRAG:",drag_start.x,",",drag_start.y,",",mouseX,",",mouseY); | ||
//Draw the dragged line; | ||
} | ||
} | ||
|
||
boolean check_mouse_over_button(int x, int y,int _height,int _width){ | ||
if(mouseY>=y && (mouseY-y<_height) &&(mouseX-x<_width)&& mouseX>=x) | ||
rectover=true; | ||
else rectover=false; | ||
|
||
return rectover; | ||
|
||
} | ||
void mouseMoved(){ | ||
vertex_under_mouse=null; | ||
for(Vertex i:G.V){ | ||
i.checkmouseover(); | ||
void mouseMoved() { | ||
gui_current=null; | ||
for (Gui_element i : gui) { | ||
i.mouseMoved(); | ||
} | ||
} | ||
void mouseDragged(){ | ||
void mouseDragged() { | ||
mouseMoved(); | ||
if(draw_state!=DRAW_DRAG){//start dragging | ||
drag_start=vertex_under_mouse; | ||
draw_state=DRAW_DRAG; | ||
if (draw_state!=DRAW_DRAG) {//start dragging | ||
if (gui_current!=null&&gui_current instanceof Vertex) { | ||
drag_start=(Vertex)gui_current; | ||
draw_state=DRAW_DRAG; | ||
} | ||
} | ||
|
||
} | ||
void mousePressed(){ | ||
if(vertex_under_mouse==null && !check_mouse_over_button(rectX1,rectY1,rectheight,rectwidth) &&!check_mouse_over_button(rectX2,rectY2,rectheight,rectwidth)){ | ||
G.V.add(new Vertex(mouseX, mouseY,true)); | ||
void mousePressed() { | ||
if (gui_current==null) { | ||
Vertex v=new Vertex(mouseX, mouseY); | ||
G.V.add(v); | ||
gui.add(v); | ||
draw_button.disable=!G.ValidGraph(); | ||
} else { | ||
gui_current.mousePressed(); | ||
} | ||
|
||
} | ||
|
||
void mouseReleased(){ | ||
if(draw_state==DRAW_DRAG){ | ||
void mouseReleased() { | ||
if (draw_state==DRAW_DRAG) { | ||
draw_state=DRAW_NORMAL; | ||
if(vertex_under_mouse!=null){ | ||
if(G.ValidNewEdge(drag_start,vertex_under_mouse)){ | ||
G.E.add(new Edge(drag_start,vertex_under_mouse)); | ||
if (gui_current!=null&&(gui_current instanceof Vertex)) { | ||
if (G.ValidNewEdge(drag_start, (Vertex)gui_current)) { | ||
Edge e=new Edge(drag_start, (Vertex)gui_current); | ||
G.E.add(e); | ||
gui.add(e); | ||
draw_button.disable=!G.ValidGraph(); | ||
println("Valid"); | ||
}else{ | ||
} else { | ||
println("Invalid"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
void mouse_over_BUTTON(){ | ||
|
||
if(check_mouse_over_button(rectX1,rectY1,rectheight,rectwidth)){ | ||
fill(255,0,0); | ||
}else { | ||
fill(rectColor); | ||
class InitialGraphEvent implements ButtonEvent { | ||
void onclick() { | ||
if (G.V.size()<=3) throw new RuntimeException("Requires at least 4 points"); | ||
for (int i=0; i<G.V.size (); i++) { | ||
do { | ||
stroke(0, 0, 255); | ||
strokeWeight(2); | ||
int l= int(random(G.V.size())); | ||
println(l); | ||
//add the drawn edge to the graph | ||
if (G.ValidNewEdge(G.V.get(i), G.V.get(l))) { //have to check the whether the randomly selected vertex has three edges???? | ||
Edge e=new Edge(G.V.get(i), G.V.get(l)); | ||
G.E.add(e); | ||
gui.add(e); | ||
} | ||
}while (G.check3edges_per_vertex (G.V.get (i))); | ||
draw_button.disable=!G.ValidGraph(); | ||
} | ||
} | ||
} | ||
class ResetEvent implements ButtonEvent { | ||
void onclick() { | ||
G.E.clear(); | ||
G.V.clear(); | ||
gui.clear(); | ||
setup(); | ||
} | ||
noStroke(); | ||
rect(rectX1,rectY1,rectwidth,rectheight); | ||
textSize(10); | ||
fill(0, 0, 0); | ||
text("Initial Graph",rectX1+rectwidth/8,rectY1+rectheight/2); | ||
|
||
if(check_mouse_over_button(rectX2,rectY2,rectheight,rectwidth)) | ||
fill(255,0,0); | ||
else | ||
fill(rectColor); | ||
|
||
rect(rectX2,rectY2,rectwidth,rectheight); | ||
textSize(10); | ||
fill(0, 0, 0); | ||
text("Reset",rectX2+rectwidth/8,rectY2+rectheight/2); | ||
} | ||
|
||
void mouseClicked(){ | ||
if(check_mouse_over_button(rectX1,rectY1,rectheight,rectwidth)){ | ||
//draw edges should be implemented by pressing button. | ||
if (G.V.size()<=3) throw new RuntimeException("Requires at least 4 points"); | ||
for(int i=0;i<G.V.size();i++){ | ||
do{ | ||
stroke(0,0,255); | ||
strokeWeight(2); | ||
int l= int(random(G.V.size())); | ||
println(l); | ||
//add the drawn edge to the graph | ||
if(G.ValidNewEdge(G.V.get(i),G.V.get(l))){ //have to check the whether the randomly selected vertex has three edges???? | ||
G.E.add(new Edge(G.V.get(i),G.V.get(l))); | ||
line(G.V.get(i).x,G.V.get(i).y,G.V.get(l).x,G.V.get(l).y); | ||
} | ||
}while(G.check3edges_per_vertex(G.V.get(i))); | ||
} | ||
} | ||
if(check_mouse_over_button(rectX2,rectY2,rectheight,rectwidth)){ | ||
G.E.clear(); | ||
G.V.clear(); | ||
class TutteDrawEvent implements ButtonEvent { | ||
void onclick() { | ||
int l=G.V.size(); | ||
|
||
//Matrix L | ||
float[][] L=new float[l][l]; | ||
for(int i=0;i<l;++i){ | ||
for(int j=0;j<l;++j){//L[i][j]=... | ||
if(i==j){ | ||
L[i][j]=G.V.get(i).E.size(); | ||
}else{ | ||
L[i][j]=0; | ||
for(Edge k:G.V.get(i).E){ | ||
if(k.v1==G.V.get(j)||k.v2==G.V.get(j)){ | ||
L[i][j]=-1;break; | ||
} | ||
} | ||
} | ||
//print(" ",L[i][j]); | ||
} | ||
//println(); | ||
} | ||
|
||
float[][] L1=new float[3][3]; | ||
float[][] L2=new float[l-3][l-3]; | ||
float[][] B=new float[l-3][3]; | ||
for(int i=0;i<3;++i){ | ||
for(int j=0;j<3;++j){ | ||
L1[i][j]=L[i][j]; | ||
} | ||
} | ||
for(int i=0;i<l-3;++i){ | ||
for(int j=0;j<l-3;++j){ | ||
L2[i][j]=L[i+3][j+3]; | ||
} | ||
} | ||
for(int i=0;i<l-3;++i){ | ||
for(int j=0;j<3;++j){ | ||
B[i][j]=L[i+3][j]; | ||
} | ||
} | ||
|
||
float[][] P1=new float[][]{{10,10},{590,10},{300,590}}; | ||
float[][] invL2= Mat.inverse(L2); | ||
float[][] nagP2=Mat.multiply(Mat.multiply(invL2,B),P1); | ||
orig=new float[l][2]; | ||
targ=new float[l][2]; | ||
for(int i=0;i<3;++i){ | ||
orig[i][0]=G.V.get(i).x; | ||
orig[i][1]=G.V.get(i).y; | ||
targ[i][0]=(int)P1[i][0]; | ||
targ[i][1]=(int)P1[i][1]; | ||
} | ||
for(int i=3;i<l;++i){ | ||
orig[i][0]=G.V.get(i).x; | ||
orig[i][1]=G.V.get(i).y; | ||
targ[i][0]=(int)-nagP2[i-3][0]; | ||
targ[i][1]=(int)-nagP2[i-3][1]; | ||
} | ||
animation_start_time=millis(); | ||
draw_state=DRAW_ANIMATION; | ||
} | ||
} | ||
|
||
|