/* * Edge.java * * Created on December 3, 2003, 12:49 AM */ /** * * @author linyuan */ public class Edge extends Selectable { Vertex u; Vertex v; /** Creates a new instance of Edge */ public Edge() { } public Edge(Vertex u, Vertex v){ this.u=u; this.v=v; } double distance(int x, int y) { double a, b, c; a=(u.x-x)*(u.x-x)+(u.y-y)*(u.y-y)+0.0; b=(v.x-x)*(v.x-x)+(v.y-y)*(v.y-y)+0.0; c=(u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y)+0.0; if(a>=b+c) return Math.sqrt((double) b); if(b>=a+c) return Math.sqrt((double) a); return Math.sqrt(b-(c+b-a)*(c+b-a)/4.0/c); } public void draw(java.awt.Graphics g){ if(isSelected()) g.setColor(selectedColor); else g.setColor(color); g.drawLine(u.x,u.y,v.x,v.y); //draw end arrow int r = 3; //thickness of vertex int s = 5; //thickness of line double ax = u.x+0.5; double ay = u.y+0.5; double length=Math.sqrt((v.x-u.x)*(v.x-u.x)+(v.y-u.y)*(v.y-u.y)); if (length==0) return; double sin = (v.y-u.y)/length; double cos = (v.x-u.x)/length ; double px = length-(r+1); double py = 0; double ly = s; double lx = length-(r+s+s); double rx = lx; double ry = -ly; double mx = length-(r+s+s/2); double my = 0; double tx; double ty; tx = px*cos-py*sin; ty = px*sin+py*cos; px = tx; py = ty; tx = lx*cos-ly*sin; ty = lx*sin+ly*cos; lx = tx; ly = ty; tx = rx*cos-ry*sin; ty = rx*sin+ry*cos; rx = tx; ry = ty; tx = mx*cos-my*sin; ty = mx*sin+my*cos; mx = tx; my = ty; px+=ax; py+=ay; mx+=ax; my+=ay; lx+=ax; ly+=ay; rx+=ax; ry+=ay; int[] xvals=new int[4]; int[] yvals=new int[4]; xvals[0]=(int)px; xvals[1]=(int)lx; xvals[2]=(int)mx; xvals[3]=(int)rx; yvals[0]=(int)py; yvals[1]=(int)ly; yvals[2]=(int)my; yvals[3]=(int)ry; g.fillPolygon(xvals,yvals,4); } }