【发布时间】:2020-07-16 20:14:02
【问题描述】:
使用从这个question 转换为python Radial Positions algorithm 的C,我已经成功地创建了一个基于根节点的径向图,一直到每个节点的最后一个子节点。现在我有一个新问题,我有多个根节点,需要它们以找到的第一个根为中心,或者至少是一个中心点。
我找到的最接近的例子是这张图:
到目前为止,我所能想到的只是对于找到的每个根节点,将其乘以它的索引,然后将 x 位置与 y 半径相加。到目前为止,它的效果并不好,因为我的子节点不遵循它。这几天我一直被这个问题难住了。
def RadialPositions(node, id):
children_in_node = len(timelinedatta.neighbors(id, mode="out"))
def rotate_node(x, y, nangle):
nx = x * math.cos(nangle) - y * math.sin(nangle)
ny = x * math.sin(nangle) + y * math.cos(nangle)
return nx, ny
def get_depth(id):
count = 0
for v in timelinedatta.bfsiter(id, mode="in", advanced=True):
count = count + 1
return count - 1
if len(timelinedatta.neighbors(id, mode="out")) > 0 and len(timelinedatta.neighbors(id, mode="in")) == 0:
node["positions"] = (0, 0)
node["depth"] = get_depth(id)
node_children_list = []
for child in timelinedatta.neighbors(id, mode="out"):
node_children_list.append((child, timelinedatta.vs[child]))
for idx, (child_node_id, child_node) in enumerate(node_children_list, start=0):
centerAdjust = 0
if timelinedatta.neighbors(id, mode="in"):
centerAdjust = (-node["angleRange"] + node["angleRange"] / children_in_node) / 2
child_node["depth"] = get_depth(child_node_id)
child_depth = child_node["depth"]
child_node["nodeAngle"] = node["nodeAngle"] + node["angleRange"] / children_in_node * idx + centerAdjust
child_node["angleRange"] = node["angleRange"] / children_in_node
nx = rotate_node(40 * child_depth, 0, child_node["nodeAngle"])[0]
ny = rotate_node(40 * child_depth, 0, child_node["nodeAngle"])[1]
child_node["positions"] = [2 * nx, 2 * ny]
RadialPositions(child_node, child_node_id)
我的图表 here 在 pastebin 上也有一个示例
【问题讨论】:
-
您有任何想要表示的图表示例吗?
-
@OneLyner 是的,我已经用有向图更新了我的答案。 pastebin.com/51NH7phr
-
不确定您的具体问题,但不是重新发明,而是检查graphviz.readthedocs.io/en/stable 吗?这顺利地使用了实际的en.wikipedia.org/wiki/Graphviz,在 C 中实现。
-
@thoku 我很想使用 Graphviz,但对于我的实现,我需要在不同的应用程序中显示所有图形信息。虽然我尝试过查看 Graphviz 的算法,但我对 C 不太了解
标签: python math tree nodes igraph