我正在尝试创建一个显示简单图形的应用程序,并且由于我使用 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/