简体   繁体   English

在两个节点(圆)之间绘制一条边(线)

[英]Drawing an Edge (Line) Between Two Nodes ( Circles)

I am working on a dependancy graph that consists of multiple nodes and multiple directed edges from one node to another in the graph. 我正在研究一个依赖关系图,该关系图由多个节点和图中一个节点到另一个节点的多个有向边组成。

I am trying to draw a visualation of the graph by adding n number of nodes as circles and edges between those nodes as a line. 我试图通过将n个节点添加为圆形并将这些节点之间的边缘添加为一条线来绘制图形的外观。

I a using Graphics library of Java along with JPanel and Jframe. 我与JPanel和Jframe一起使用Java图形库。

This is currently the code I have made: 这是我现在编写的代码:

public class LoopUnrolling extends JPanel{


static int length = 5;
static String graph[][] = new String[length][length];


@Override
public void paintComponent(Graphics g){

    super.paintComponent(g);

    Random random = new Random();

    int x1 = random.nextInt(500);
    int y1 = random.nextInt(100);

    int x2 = random.nextInt(500);
    int y2 = random.nextInt(100);

    g.setColor(Color.red);
    g.drawOval(x1,y1,30,40);
    g.drawOval(x2,y2,30,40);
    g.drawLine(x1, y1, x2, y2);



}
public static void main(String[] args) {

         LoopUnrolling paintObject = new LoopUnrolling();
         JFrame jf = new JFrame();
         jf.setTitle("Dependancy Graph");
         jf.setSize(600,400);
         jf.setVisible(true);
         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         jf.add(paintObject);
        }


 }

I was able to draw two circles and a line but the problem I am getting is connecting those two circles with an edge. 我能够绘制两个圆和一条线,但是我遇到的问题是用边缘连接这两个圆。

I have drawn each node at a random place on the canvas and want to add a Line between those two nodes. 我在画布上的随机位置绘制了每个节点,并希望在这两个节点之间添加一条线。 The line has Point1(x1,y1) and Point2(x2,y2). 该行具有Point1(x1,y1)和Point2(x2,y2)。 These points should be the points on two different Node's(Circles) outline 这些点应该是两个不同的节点(圆)轮廓上的点

You have two ellipses with centers 您有两个带中心的椭圆

 cx1 = x1 + w1/2, cy1 = y1 + h1/2 
 and 
 cx2 = x2 + w2/2, cy2 = y2 + h2/2 

where wxx and hxx are width and height of ellipse (third and fourth parameters of drawOval ) 其中wxx和hxx是椭圆的宽度和高度( drawOval第三个和第四个参数)

Get difference vector 获取差异向量

 dx = cx2 - cx1
 dy = cy2 - cy1

Normalize it 规范化

 len  = sqrt(dx*dx + dy*dy)
 dx = dx / len
 dy = dy / len

Now calculate points at circumference 现在计算圆周上的点

 r1 = 0.5 * w1 * h1 / sqrt(w1*w1*dy*dy+h1*h1*dx*dx)
 px1 = cx1 + r1 * dx
 py1 = cy1 + r1 * dy

 r2 = 0.5 * w2 * h2 / sqrt(w2*w2*dy*dy+h2*h2*dx*dx)
 px2 = cx2 - r2 * dx
 py2 = cy2 - r2 * dy

And draw line segment (px1,py1)-(px2,py2) 并绘制线段(px1,py1)-(px2,py2)

Sample Delphi implementation and result: 示例Delphi的实现和结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM