-
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.
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 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,13 @@ | ||
class Edge { | ||
Vertex v1,v2; | ||
Edge(Vertex _v1, Vertex _v2){ | ||
v1=_v1;v2=_v2; | ||
v1.E.add(this); | ||
v2.E.add(this); | ||
} | ||
void draw(){ | ||
strokeWeight(2); | ||
stroke(0,0,255); | ||
line(v1.x,v1.y,v2.x,v2.y); | ||
} | ||
} |
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,12 @@ | ||
class Graph{ | ||
ArrayList<Vertex> V=new ArrayList(); | ||
ArrayList<Edge> E=new ArrayList(); | ||
boolean ValidNewEdge(Vertex v1,Vertex v2){ | ||
if(v1==v2)return false; | ||
for(Edge i:v1.E){ | ||
if((i.v1==v1)&&(i.v2==v2))return false; | ||
if((i.v2==v1)&&(i.v1==v2))return false; | ||
} | ||
return true; | ||
} | ||
} |
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,29 @@ | ||
class Vertex{ | ||
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){ | ||
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; | ||
} | ||
} | ||
} |
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,65 @@ | ||
final int DRAW_NORMAL=1; | ||
final int DRAW_DRAG=2; | ||
final int Vertex_Rad=8; | ||
Vertex drag_start; | ||
Vertex vertex_under_mouse; | ||
void setup() { | ||
size(640, 360); | ||
noStroke(); | ||
} | ||
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(); | ||
} | ||
//Draw Edges | ||
strokeWeight(2); | ||
for(Edge i:G.E){ | ||
i.draw(); | ||
} | ||
if(draw_state==DRAW_DRAG){ | ||
strokeWeight(2); | ||
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; | ||
} | ||
} | ||
void mouseMoved(){ | ||
vertex_under_mouse=null; | ||
for(Vertex i:G.V){ | ||
i.checkmouseover(); | ||
} | ||
} | ||
void mouseDragged(){ | ||
mouseMoved(); | ||
if(draw_state!=DRAW_DRAG){//start dragging | ||
drag_start=vertex_under_mouse; | ||
draw_state=DRAW_DRAG; | ||
} | ||
|
||
} | ||
void mousePressed(){ | ||
if(vertex_under_mouse==null){ | ||
G.V.add(new Vertex(mouseX, mouseY,true)); | ||
} | ||
} | ||
|
||
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)); | ||
println("Valid"); | ||
}else{ | ||
println("Invalid"); | ||
} | ||
} | ||
} | ||
} |
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,2 @@ | ||
mode.id=processing.mode.java.JavaMode | ||
mode=Java |