The Artima Developer Community
Sponsored Link

Java Answers Forum
CHANGE THE EDGE DEFINITION FOR THE GRAPH

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
belen rive

Posts: 1
Nickname: zooo79
Registered: Jan, 2003

CHANGE THE EDGE DEFINITION FOR THE GRAPH Posted: Jan 30, 2003 2:42 AM
Reply to this message Reply
Advertisement
i have a problem. first sorry for my english.
down are my declarations and definitions for a graph that can be directed and not.
The definition for the edges are now that you can create only an edge that can go directly from one vertex to the other. I don't know how to change the definition that i can make also an edge that

is not a streight line but it can have 90 degree corners(like a polyline - exsactly like in the picture under the Thank You)

If no one knows how to do this, pleas give me the edge definition that do what i need and i will change my program

i have forgotten to tel that the edge must be declared as it is one line with corners not made of two or three lines.



pleas help me
Thank You

----edge-------vertex
|
|
|edge
|
|
-----edge------>vertex


or

----edge------->vertex
|
|
|edge
|
|
vertex


not only

vertex------edge----->vertex



class Vertex //vertex definition
{
public int x;
public int y;
public Color color;
public int dist;
public int pred;
public boolean S;
public boolean show;
}
//////////////////////////////////////////////////////
class Edge //edge definition
{
public int v1;
public int v2;
public int midX;
public int midY;
public int left;
public Color color;
}
//////////////////////////////////////////////////////////////////

public void update(Graphics g) //draw graph
{
paint(g);
}

public void paint(Graphics g)
{
Dimension size = size();
Image bufferedImage=createImage(size.width, size.height);
Graphics bg = bufferedImage.getGraphics();
bg.setColor(Color.black);
Font f = new Font("SanSerif", Font.PLAIN, 12);
bg.setFont(f);

for(int i=0;i<nedges;i++)
drawEdge(bg, i);

for(int i=0;i<nvertices;i++)
drawVertex(bg, i);

if(drag)
{
bg.setColor(Color.black);
bg.drawLine(vertices[selectedVertex].x+DIA/2,
vertices[selectedVertex].y+DIA/2,
mouseX, mouseY);
}

g.drawImage(bufferedImage, 0, 0, null);
}

////////////////////////////////////////////////////////////////////////
public void drawVertex(Graphics g, int i) //draw vertex in graph
{
if(vertices.show)
{
g.setColor(vertices.color);
if(i==startVertex)
g.setColor(Color.green);
else if(i==targetVertex)
g.setColor(Color.red);

if(i<9)
{
g.fillOval(vertices.x, vertices.y, DIA, DIA);
g.setColor(Color.black);
g.drawOval(vertices.x, vertices.y, DIA, DIA);
g.drawString("V"+(i+1), vertices.x+DIA/2-6, vertices.y+DIA-5);
}
else
{
g.fillOval(vertices.x, vertices.y, DIA+8, DIA);
g.setColor(Color.black);
g.drawOval(vertices.x, vertices.y, DIA+8, DIA);
g.drawString("V"+(i+1), vertices.x+DIA/2-6, vertices.y+DIA-5);
}
}
}
////////////////////////////////////////////////////////////////////////

public void drawEdge(Graphics g, int i) //draw edge
{ int x1=vertices[edges.v1].x+DIA/2;
int y1=vertices[edges.v1].y+DIA/2;
int x2=vertices[edges.v2].x+DIA/2;
int y2=vertices[edges.v2].y+DIA/2;

edges.midX = x1+(x2-x1)/2;
edges.midY = y1+(y2-y1)/2;

g.setColor(edges.color);
if(digraph)
drawArrow(g,i,x1,y1,x2,y2);
else
{
g.drawLine(x1,y1,x2,y2);
g.setColor(Color.white);
g.drawString(""+Adj[edges.v1][edges.v2],
edges.midX, edges.midY);
g.drawString("- "+lev[edges.v1][edges.v2],
edges.midX+15, edges.midY);
}
}
///////////////////////////////////////////////////////////////////////////// ////
//this is the arrow for the directed edge
public void drawArrow(Graphics g,int i, int x1, int y1, int x2, int y2)
{
int[] coordsX = new int[3];
int[] coordsY = new int[3];
//int dve;
double distX = x2-x1;
double distY = y2-y1;
double edgeLength = Math.sqrt(Math.pow(distX,2)+Math.pow(distY,2));
double slideX = distX/edgeLength;
double slideY = distY/edgeLength;
double splitX = slideY;
double splitY = slideX;

if(Adj[edges.v2][edges.v1]>=0)
{
double splitX1=x1-5*splitX+0.5;
double splitY1=y1+5*splitY+0.5;
double splitX2=x2-5*splitX+0.5;
double splitY2=y2+5*splitY+0.5;
double intPtx=x2-30*slideX-5*splitX+0.5;
double intPty=y2-30*slideY+5*splitY+0.5;
coordsX[0]=(int)(x2-20*slideX-5*splitX+0.5);
coordsY[0]=(int)(y2-20*slideY+5*splitY+0.5);
coordsX[1]=(int)(intPtx-5*splitX+0.5);
coordsY[1]=(int)(intPty+5*splitY+0.5);
coordsX[2]=(int)(intPtx+5*splitX+0.5);
coordsY[2]=(int)(intPty-5*splitY+0.5);
g.drawLine((int)splitX1,(int)splitY1,(int)splitX2,(int)splitY2);
g.fillPolygon(coordsX, coordsY, 3);
edges.midX=(int)(splitX1+(splitX2-splitX1)/2);
edges.midY=(int)(splitY1+(splitY2-splitY1)/2);

//if (dve==1){
g.setColor(Color.white);
g.drawString(""+Adj[edges.v1][edges.v2],
edges.midX, edges.midY);
g.drawString(""+lev[edges.v1][edges.v2],
edges.midX+4, edges.midY+5);
}
else
{
double intPtx = x2-30*slideX;
double intPty = y2-30*slideY;
coordsX[0]=(int)(x2-20*slideX+0.5);
coordsY[0]=(int)(y2-20*slideY+0.5);
coordsX[1]=(int)(intPtx-5*slideY+0.5);
coordsY[1]=(int)(intPty+5*slideX+0.5);
coordsX[2]=(int)(intPtx+5*slideY+0.5);
coordsY[2]=(int)(intPty-5*slideX+0.5);
g.drawLine(x1, y1, x2, y2);
g.fillPolygon(coordsX, coordsY, 3);

g.setColor(Color.white);
g.drawString(""+Adj[edges.v1][edges.v2],
edges.midX, edges.midY);
g.drawString("- "+lev[edges.v1][edges.v2],
edges.midX+13, edges.midY);

}
/*g.setColor(Color.white);
g.drawString(""+Adj[edges.v1][edges.v2],
edges.midX, edges.midY);
g.drawString(""+edges.left,
edges.midX, edges.midY);*/
}
////////////////////////////////////////////////////////////////////////
public void addVertex(int x, int y) //add vertex in graph
{
if (nvertices<MAXVERTICES)
{
Vertex v = new Vertex();
v.x = x-DIA/2;
v.y = y-DIA/2;
v.color = new Color(250, 200, 100);
v.dist = 0;
v.pred = -1;
v.S=false;
v.show=true;
vertices[nvertices++] = v;
}
repaint();
}
///////////////////////////////////////////////////////////////////////////
public void moveVertex(int i, int x, int y) //move vertex in graph
{
vertices.x=x-DIA/2;
vertices.y=y-DIA/2;
repaint();
}
//////////////////////////////////////////////////////////////////////////
public void selectVertex(int i) //select vertex in graph
{
if(startVertex==i)
{
reset();
startVertex=-1;
}
else if(targetVertex==i)
{
reset();
targetVertex=-1;
}
else if(startVertex<0)
startVertex=i;
else if (targetVertex<0)
targetVertex=i;
else
{
reset();
startVertex=i;
targetVertex=-1;
}

repaint();
}

public int findVertex(int x, int y)
{
for(int i=0;i<nvertices;i++)
{
if((x>vertices.x-DIA/2)&&(x<vertices.x+DIA) &&
(y>vertices.y-DIA/2)&&(y<vertices.y+DIA))
return i;
}
return -1;
}
///////////////////////////////////////////////////////////////////////////// //
public void deleteVertex(int breakPoint) //delete vertex from graph
{
vertices[breakPoint].show=false;
for(int i=0;i<nvertices;i++)
{
for(int j=0;j<nedges;j++)
if((edges[j].v1==breakPoint&&edges[j].v2==i)||
(edges[j].v1==i&&edges[j].v2==breakPoint))
deleteEdge(j);

if(digraph&&Adj[breakPoint]>0)
{
for(int j=0;j<nedges;j++)
if(edges[j].v1==i&&edges[j].v2==breakPoint)
deleteEdge(j);
}
}
repaint();
}
///////////////////////////////////////////////////////////////////////////// ///
//add edge in graph
public void addEdge(int V1, int V2, int weightIn, int levin)
{
if(digraph) // if the edge is in both ways with the same waight
{
Adj[V1][V2]=weightIn;
lev[V1][V2]=levin;
}
else // if it is a directed edge
{
Adj[V1][V2]=weightIn;
Adj[V2][V1]=weightIn;
lev[V1][V2]=levin;
lev[V2][V1]=levin;
}

Edge e1 = new Edge();
e1.v1 = V1;
e1.v2 = V2;
e1.color = Color.black;
e1.midX = 0;
e1.midY = 0;
edges[nedges++]=e1;

if(!digraph) //the edge is not directed but is in both
// ways, from and to the vertex
{
Edge e2 = new Edge();
e2.v1=V2;
e2.v2=V1;
e2.color = Color.black;
e1.midX=0;
e1.midY=0;
edges[nedges++]=e2;
}
repaint();
}
///////////////////////////////////////////////////////////////////////////// //////
public void deleteEdge(int breakPoint)
{
if(!digraph)
{
Adj[edges[breakPoint].v1][edges[breakPoint].v2]=-1;
Adj[edges[breakPoint].v2][edges[breakPoint].v1]=-1;
lev[edges[breakPoint].v1][edges[breakPoint].v2]=-1;
lev[edges[breakPoint].v2][edges[breakPoint].v1]=-1;
}
else
Adj[edges[breakPoint].v1][edges[breakPoint].v2]=-1;
lev[edges[breakPoint].v1][edges[breakPoint].v2]=-1;

for(int i=breakPoint;i<nedges-1;i++)
{
edges=edges[i+1];
}
nedges--;
repaint();
}

public void changeWeight(int i, int newWeight)
{
Adj[edges.v1][edges.v2]=newWeight;
repaint();
}

public void changeLeft(int i, int newLeft)
{
lev[edges.v1][edges.v2]=newLeft;
repaint();
}

public int findEdge(int x, int y)
{
for(int i=0;i<nedges;i++)
{
if((x>edges.midX-8)&&(x<edges.midX+8)
&&(y>edges.midY-8)&&(y<edges.midY+8))
return i;
}

return -1;
}

public int getMin()
{ int minV;
int min;
int breakPoint;

if(nreached==0)
return -1;
else
{
min=vertices[reached[0]].dist;
minV=reached[0];
breakPoint=0;

for(int i=1;i<nreached;i++)
{
if(vertices[reached].dist<min)
{
min=vertices[reached].dist;
minV=reached;
breakPoint=i;
}
}

for(int i=breakPoint;i<nreached-1;i++)
reached=reached[i+1];

nreached--;
}

return minV;
}

public boolean inReached(int v)
{ boolean found=false;

for(int i=0;i<nreached;i++)
{
if(reached==v)
found=true;
}
return found;
}

Topic: Protocols Previous Topic   Next Topic Topic: print applet in htm

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use