Skip to content

Commit

Permalink
Basic drawing, Vertex and Edge.
Browse files Browse the repository at this point in the history
  • Loading branch information
tao committed Nov 17, 2014
1 parent 2b9d06c commit 5759676
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
13 changes: 13 additions & 0 deletions drawing/Edge.pde
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);
}
}
12 changes: 12 additions & 0 deletions drawing/Graph.pde
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;
}
}
29 changes: 29 additions & 0 deletions drawing/Vertex.pde
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;
}
}
}
65 changes: 65 additions & 0 deletions drawing/drawing.pde
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");
}
}
}
}
2 changes: 2 additions & 0 deletions drawing/sketch.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mode.id=processing.mode.java.JavaMode
mode=Java

0 comments on commit 5759676

Please sign in to comment.