如何得到一些点的Voronoi图在QT中显示
How to get the Voronoi diagram of some points to display in QT?
我在QT中的QGraphicsScene上绘制了一些点,并将它们封装到一个点类中。我想计算并在场景中显示这些点的Voronoi图。最好的方法是什么?
我正在考虑使用CGAL,但我找不到一个好的方法来做到这一点。
您需要显示的是限制为矩形(您的显示窗口)的Voronoi图。这里有一个简单的方法。
相关文档页到处都有。
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <iterator>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef K::Iso_rectangle_2 Iso_rectangle_2;
typedef K::Segment_2 Segment_2;
typedef K::Ray_2 Ray_2;
typedef K::Line_2 Line_2;
typedef CGAL::Delaunay_triangulation_2<K> Delaunay_triangulation_2;
//A class to recover Voronoi diagram from stream.
//Rays, lines and segments are cropped to a rectangle
//so that only segments are stored
struct Cropped_voronoi_from_delaunay{
std::list<Segment_2> m_cropped_vd;
Iso_rectangle_2 m_bbox;
Cropped_voronoi_from_delaunay(const Iso_rectangle_2& bbox):m_bbox(bbox){}
template <class RSL>
void crop_and_extract_segment(const RSL& rsl){
CGAL::Object obj = CGAL::intersection(rsl,m_bbox);
const Segment_2* s=CGAL::object_cast<Segment_2>(&obj);
if (s) m_cropped_vd.push_back(*s);
}
void operator<<(const Ray_2& ray) { crop_and_extract_segment(ray); }
void operator<<(const Line_2& line) { crop_and_extract_segment(line); }
void operator<<(const Segment_2& seg){ crop_and_extract_segment(seg); }
};
int main(){
//consider some points
std::vector<Point_2> points;
points.push_back(Point_2(0,0));
points.push_back(Point_2(1,1));
points.push_back(Point_2(0,1));
Delaunay_triangulation_2 dt2;
//insert points into the triangulation
dt2.insert(points.begin(),points.end());
//construct a rectangle
Iso_rectangle_2 bbox(-1,-1,2,2);
Cropped_voronoi_from_delaunay vor(bbox);
//extract the cropped Voronoi diagram
dt2.draw_dual(vor);
//print the cropped Voronoi diagram as segments
std::copy(vor.m_cropped_vd.begin(),vor.m_cropped_vd.end(),
std::ostream_iterator<Segment_2>(std::cout,"n"));
}
如果您考虑使用CGAL库,您是否阅读了其中关于Voronoi图的部分?在软件设计小节中,他们解释了所涉及的数据结构。从我读到的,你可以得到代表狄利克雷细胞的多边形,从那里你可以告诉Qt用不同的颜色绘制和填充它们(一些世界地图的着色算法可能在这里有用)。
相关文章:
- 在无向图中打印循环
- 有没有办法在一行中填充矢量图
- Winapi:屏幕截图未显示在窗口中
- 在 UML 类图中为C++类添加构造函数和析构函数
- 如何在快板的屏幕中显示子位图的绘制?
- 为什么 std::next 在以空地图开头馈送时卡住
- 更改高度贴图,使其在 4x4 网格上显示 16 个 hieghtmap
- C++在乘法图中放置随机值(嵌套循环)
- 在自定义类型图中重用 SWIG 映射
- 优化在网格图中查找哈密尔循环的函数?
- 在 c++ 或 python 中生成一个体面的视差图以在 Raspberry Pi 上实现的最佳方法(算法或函数)是什么
- 动图在闪烁
- 使用Vertex_descriptor boost图在顶点上打印或迭代
- 使用栅格数据和1D纹理图在opengl中创建等高线图
- 如何得到一些点的Voronoi图在QT中显示
- 在c++中可视化图形和voronoi图的最佳方法
- 简单的方法捕获截图在windows操作系统?c++
- 如何输出一个有向图在graphml格式
- 如何在多边形内构造voronoi图
- 模板匹配截图在断言时失败