将图形转换为2D图 [英] Converting a graph to a 2D diagram

查看:240
本文介绍了将图形转换为2D图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个如下的2D图,

I want to create a 2D diagram like the following,

上面的图片是使用MATLAB中的Graph创建的( ref )

The above image has been created using Graph in MATLAB (ref)

s = [1 1 1 1 2 2 3 4 4 5 6];
t = [2 3 4 5 3 6 6 5 7 7 7];
weights = [50 10 20 80 90 90 30 20 100 40 60];
G = graph(s,t,weights)
plot(G,'EdgeLabel',G.Edges.Weight)

信息存储为graph的节点,边,边权重. 我想使用此信息来创建2D CAD图.可以使用边缘权重指定线的长度.但是,我不确定如何从图形中检索角度.据我了解,边缘的方向会根据创建图形对象所选择的布局而变化. 我想创建一个[x,y]坐标文件并导入到Autocad中.

The information is stored as nodes, edges, edge weight of a graph. I'd like to create a 2D CAD drawing using this information. The length of lines can be specified using edge weights. However, I am not sure how the angles can be retrieved from the graph. From what I understand, the orientation of the edges vary depending on the layout that is selected for creating a graph object. I want to create a [x,y] coordinate file and import into Autocad.

从下面解释的答案中,我知道将边缘权重指定为长度并不是一件容易的事.作为一种替代方法,我想从图像中获取节点的坐标,计算节点之间的距离,并将该距离指定为边缘权重(忽略上面提供的权重). 使用坐标集,节点到节点的连接以及节点到节点的距离,我想以编程方式生成一维CAD图.

From the answer explained below, I understand it is not straightforward to assign the edge weights as lengths. As an alternative, I want to obtain the coordinates of nodes from the image, compute the distance between nodes and assign the distance as edge weights (ignoring the weights provided above). With the set of coordinates, node-node connection and node-node distance I'd like to generate a 1D CAD digram programmatically.

由于无法直接从MATLAB输出中获取节点的坐标,并且无法将边缘权重(在原始输入中)分配为边缘长度,因此,我想尝试一种替代方法. 例如,如果这些是节点((75 25) (115 45) (90 60) (10 5) (45 0) (45 55) (0 25))的坐标,我想计算之间的欧式距离 坐标并将距离指定为边缘权重. 据我了解,AutoCAD中的dimension选项卡计算欧几里德距离.但是,我不确定如何将此输出分配为边缘权重.

Since the coordinates of the nodes cannot be directly obtained from the MATLAB output and the edge weights(in the original input) cannot be assigned as edge lengths, I'd like to try an alternate approach. For instance, if these are the coordinates of the nodes ((75 25) (115 45) (90 60) (10 5) (45 0) (45 55) (0 25)), I'd like to compute the euclidean distance between the coordinates and assign the distances as edge weights. From what I understand, the dimension tab in AutoCAD computes the euclidean distance. However, I am not sure how to assign this output as edge weights.

任何有关进行方法的建议将不胜感激.

Any suggestions on how to proceed will be really appreciated.

推荐答案

首先,对于您的特定示例,将无法生成边缘权重为线长的图形.

Firstly, for your particular example it would not be possible to generate a graph for which the edge weights are the line lengths.

例如,如果节点123之间的距离是根据您的阵列进行的:

For example, if the distances between nodes 1, 2 and 3 are per your arrays:

  • 1 → 2 = 50
  • 1 → 3 = 10
  • 1 → 2 = 50
  • 1 → 3 = 10

然后,距离2 → 3必须在40&之间. 60,否则三角形不存在.而您的数组将此距离指定为90.

Then the distance 2 → 3 must be between 40 & 60, else the triangle does not exist. Whereas your array specifies this distance as 90.

为直观地说明这一点,如果要绘制一条跨越结点1&的长度为50的线, 2,如果您在此线的任意一端构造半径等于节点1 → 32 → 3之间距离的圆,则此类圆必须相交才能存在三角形.

To demonstrate this visually, if you were to picture a line of length 50 spanning nodes 1 & 2, if you construct circles at either end of this line with radii equal to the distances between nodes 1 → 3 and 2 → 3, then such circles must intersect for the triangle to exist.

当前,在您指定的权重下,没有这样的交集:

Currently, with the weights you have specified, there is no such intersection:

这样,假设每个节点的任意位置,并以节点坐标作为函数的参数提供,则可以使用诸如以下AutoLISP示例的函数来构造所需的图形:

As such, assuming arbitrary positions for each of the nodes, with node coordinates supplied as an argument to the function, you could construct the desired graph using a function such as the following AutoLISP example:

(defun graph ( pts sls tls wgt )
    (   (lambda ( l )
            (foreach x l (text (cdr x) (itoa (car x)) 0.0 1))
            (mapcar
               '(lambda ( a b c / p q r )
                    (setq p (cdr (assoc a l))
                          q (cdr (assoc b l))
                          r (angle p q)
                    )
                    (entmake (list '(0 . "LINE") (cons 10 p) (cons 11 q) '(62 . 8)))
                    (text
                        (mapcar '(lambda ( x y ) (/ (+ x y) 2.0)) p q)
                        (itoa c)
                        (if (and (< (* pi 0.5) r) (<= r (* pi 1.5))) (+ r pi) r)
                        2
                    )
                )
                sls tls wgt
            )
        )
        (mapcar 'cons (vl-sort (append sls tls) '<) pts)
    )
)
(defun text ( p s a c )
    (entmake
        (list
           '(0 . "TEXT")
            (cons 10 p)
            (cons 11 p)
            (cons 50 a)
            (cons 01 s)
            (cons 62 c)
           '(40 . 2)
           '(72 . 1)
           '(73 . 2)
        )
    )
)

使用以下参数评估上述函数时(其中第一个参数表示七个节点的坐标):

When the above function is evaluated with the following arguments (where the first argument indicates the coordinates for the seven nodes):

(graph
   '((75 25) (115 45) (90 60) (10 5) (45 0) (45 55) (0 25))
   '( 1  1  1  1  2  2  3  4   4  5  6)
   '( 2  3  4  5  3  6  6  5   7  7  7)
   '(50 10 20 80 90 90 30 20 100 40 60)
)

它将在AutoCAD中产生以下结果:

It will produce the following result in AutoCAD:

但是,如果您希望权重由每个提供的节点坐标之间的2D距离确定,则可能需要考虑以下AutoLISP函数:

If however, you want the weights to be determined by the 2D distance between each of the supplied node coordinates, could might want to consider the following AutoLISP function:

(defun graph ( pts sls tls )
    (   (lambda ( l )
            (foreach x l (text (cdr x) (itoa (car x)) 0.0 1))
            (mapcar
               '(lambda ( a b / p q r )
                    (setq p (cdr (assoc a l))
                          q (cdr (assoc b l))
                          r (angle p q)
                    )
                    (entmake (list '(0 . "LINE") (cons 10 p) (cons 11 q) '(62 . 8)))
                    (text
                        (mapcar '(lambda ( x y ) (/ (+ x y) 2.0)) p q)
                        (rtos (distance p q) 2)
                        (if (and (< (* pi 0.5) r) (<= r (* pi 1.5))) (+ r pi) r)
                        2
                    )
                )
                sls tls
            )
        )
        (mapcar 'cons (vl-sort (append sls tls) '<) pts)
    )
)
(defun text ( p s a c )
    (entmake
        (list
           '(0 . "TEXT")
            (cons 10 p)
            (cons 11 p)
            (cons 50 a)
            (cons 01 s)
            (cons 62 c)
           '(40 . 2)
           '(72 . 1)
           '(73 . 2)
        )
    )
)

提供了一个节点坐标列表和两个已连接节点列表:

Supplied with a list of node coordinates and two lists of connected nodes:

(graph
   '((75 25) (115 45) (90 60) (10 5) (45 0) (45 55) (0 25))
   '(1 1 1 1 2 2 3 4 4 5 6)
   '(2 3 4 5 3 6 6 5 7 7 7)
)

此函数将产生以下结果:

This function will produce the following result:

在这里,权重的准确性将由AutoCAD中LUPREC系统变量的值(在上面的示例中设置为4)确定.您也可以通过在我的代码中为rtos函数提供precision参数来覆盖此参数,例如对于3小数位的精度,表达式为:

Here, the accuracy of the weights will be determined by the value of the LUPREC system variable in AutoCAD (which was set to 4 in the above example). You can alternatively override this by supplying the precision argument to the rtos function in my code, e.g. for a precision of 3 decimal places, the expression would be:

(rtos (distance p q) 2 3)

这篇关于将图形转换为2D图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆