📄 linearedge.java
字号:
package edu.odu.cs.zeil.AlgAE.Client.DataViewer.DataGraph;import edu.odu.cs.zeil.AlgAE.Debug;import edu.odu.cs.zeil.AlgAE.Direction;import edu.odu.cs.zeil.AlgAE.Client.DataViewer.DataGraph.Edge;import edu.odu.cs.zeil.AlgAE.Client.DataViewer.DataGraph.Edgeable;import edu.odu.cs.zeil.AlgAE.Client.GraphicsInfo;import java.awt.Color;import java.awt.Graphics;import java.awt.Point;import java.awt.Polygon; /** * A linear edge is an edge that extends from one node to another, * different node. * * It is portrayed as a striaght line tipped with an arrow. * * @see NullEdge * @see SelfEdge */public class LinearEdge extends Edge{ LinearEdge(Edgeable src, Edgeable dest, int direction, Color colour, String edgeLabel) { //pre: src != null && dest != null && src != dest super (src, dest, direction, colour, edgeLabel); } Point entryPoint() { int dir1 = destination.directionTo(exitPoint()); Debug.show (Debug.entryPoint, "dir1: ", dir1); int dir2 = (dir1 + 1) % 16; Debug.show (Debug.entryPoint, "dir2: ", dir2); Point p1 = destination.exitPoint(dir1); Point p2 = destination.exitPoint(dir2); p1.x = (p1.x + p2.x) / 2; p1.y = (p1.y + p2.y) / 2; return p1; } void draw(GraphicsInfo gi) { Graphics g = gi.g; if ((source.position() != null) && (destination.position() != null)) { Point sourcePt = exitPoint(); Point destPt = entryPoint(); Debug.show (Debug.graphDraw, "drawing edge from", sourcePt); Debug.show (Debug.graphDraw, "drawing edge to", destPt); // Draw a small circle at the exit point. drawExitPoint (gi, sourcePt); // Draw the line between the two nodes g.setColor (color); g.drawLine(sourcePt.x, sourcePt.y, destPt.x, destPt.y); // Draw the arrow head drawArrowHead (destPt, sourcePt, gi); // Draw the label if (label != null && label.length() != 0) { // Labels are positioned 2/3 of the way down the edge. // Not halfway, because that looks confusing when nodes have // parallel edges going in opposite directions (e.g., doubly // linked lists). Point frac = new Point(sourcePt); int dx = (destPt.x - sourcePt.x); int dy = (destPt.y - sourcePt.y); frac.x += 0.67 * dx; frac.y += 0.67 * dy; if (sourcePt.x == destPt.x) frac.x += (1 + gi.getTextOffset().width) / 2; else if (sourcePt.y == destPt.y) frac.y -= (1 + gi.getTextOffset().height) / 2; else { int approxLen = Math.abs(dx) + Math.abs(dy); frac.x += dy * gi.getTextOffset().width / approxLen; frac.y -= dx * gi.getTextOffset().height / approxLen; } gi.g.drawString (label, frac.x, frac.y); } } } double length() { Point entry = entryPoint(); Point exit = exitPoint(); double x = entry.x - exit.x; double y = entry.y - exit.y; return Math.sqrt(x*x + y*y); } double positionScore(double targetLength) { Point d = new Point(); Point entry = entryPoint(); Point exit = exitPoint(); Debug.show (Debug.edgePositionScore, "positionScore//source ", source.ID()); Debug.show (Debug.edgePositionScore, "positionScore//dest ", destination.ID()); Debug.show (Debug.edgePositionScore, "positionScore//entry ", entry); Debug.show (Debug.edgePositionScore, "positionScore//exit ", exit); d.x = entry.x - exit.x; d.y = entry.y - exit.y; double dlensq = d.x*d.x + d.y*d.y; double dlen = Math.sqrt(dlensq); // directionalScore penalizes positions that bend an edge away from // its preferred direction. double directionalScore; if (dir == Direction.ANYDIR) { directionalScore = 0.0; } else { Debug.show (Debug.edgePositionScore, " d: ", d); Debug.show (Debug.edgePositionScore, " xnorm: ", xNorm[dir]); Debug.show (Debug.edgePositionScore, " ynorm: ", yNorm[dir]); directionalScore = 1.0 - (d.x * xNorm[dir] + d.y * yNorm[dir]) / dlen; Debug.show (Debug.edgePositionScore, " directionalScore: ", directionalScore); } // distanceScore penalizes positions that are too close or too // far away. double distanceScore = Math.abs(dlen - targetLength) / targetLength; return 5.0 * directionalScore + distanceScore; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -