c++ - 如何访问 boost 图拓扑布局中的坐标?

标签 c++ boost graph

我正在尝试创建一个显示简单图形的应用程序,并且由于我使用 boost::graph 作为底层数据结构,因此我也想使用库中提供的布局算法。

此处给出的答案解释了如何使用 boost 库中的布局算法来布局图的顶点: How does the attractive force of Fruchterman Reingold work with Boost Graph Library

但遗憾的是,它没有解释在计算布局后如何实际访问顶点的坐标。尽管我们得到了位置 vector (或者更确切地说是点),但 float 组件是私有(private)的,所以这没有帮助。 boost::graph 文档也没有讨论这个主题。

那么应用布局算法后如何检索每个顶点的简单 (X,Y) 坐标呢?

最佳答案

在查看了 boost graph 源代码后,发现这毕竟并不难。 我们可以使用属性映射来迭代 PositionsMap 和 [] 运算符来访问坐标:

template<typename Graph, typename Positions>
void print_positions(const Graph &g, const Positions &positions) {
    auto index_map = boost::get(boost::vertex_index, graph);

    using PropertyMap = boost::iterator_property_map<Positions::iterator, decltype(index_map)>;
    PropertyMap position_map(positions.begin(), index_map);
    BGL_FORALL_VERTICES(v, graph, Graph) {
        Position pos = position_map[v];
        cout << v << ": " << pos[0] << "|" << pos[1] << endl;
    }
}

关于c++ - 如何访问 boost 图拓扑布局中的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39057826/

相关文章:

c++ - 在 Q_OBJECT 的子类中提供 Q_PROPERTY 会导致错误 1

c++ - 使用来自 boost::math 的 Gauss-Kronrod 求积积分复杂函数

Unix 上的 C++ : Differences in threading libraries?

javascript - nvd3 "line plus bar chart"示例未显示

c++ - 运算符重载 C++; << 操作的参数太多

c++ - 使用 Qt 创建目录和 Windows' "CreateDirectory"返回 -1,错误

javascript - amCharts 4 (v4) 在饼图中显示值而不是百分比

algorithm - 如何检测有向图是否唯一连通?

c++ - 为什么不能在全特化中引入新的模板参数?

C++ : How to include boost library header in VC++6?