CGAL-绘制Power Diagram并显示

综述

这些代码用的挺频繁的,梳理一下便于日后查看。

环境

macos
clion编译器

基础

确保您基本掌握cgal与openGL
注意输入的是weight point您的权重应该确保足够大

效果

这里写图片描述

代码

//作者:SDU窦志扬
//日期:2018/8/10
//联络:sdudzy@163.com


#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Regular_triangulation_2.h>
#include <CGAL/Weighted_point_2.h>
#include <GLUT/glut.h>
#include <fstream>
#include<iostream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Regular_triangulation_2<K> Regular_triangulation;
typedef K::Point_2 Point;
typedef K::Weighted_point_2 Wp;
int window_size = 600;
using namespace std;

std::vector<Regular_triangulation::Weighted_point> wpoints;


const int n=100;
const GLfloat Pi=3.1415926536f;
#define MAX_CHAR        128
void MyDisplay(double x,double y ,double R)
{
    int i;
    glBegin(GL_POLYGON);
    glColor3f(1,0,0);
    R  =sqrt(R);
    for(int i=0;i<n;i++){
        glVertex2f(R*cos(2*Pi/n*i)+x,R*sin(2*Pi/n*i)+y);
    }
    glEnd();
}

void add_point(int x, int y, double w){
    //绘制圆形

    wpoints.push_back( Wp(Point(x,y), w) );
    MyDisplay(x,y,w);
    glPointSize(7);
    glBegin(GL_POINTS);
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex2f( x,y);
    glEnd();
}
void display(void)
{
    add_point(100, 400,10000); //左中
    add_point(200,200,15000);
    add_point(210, 560,20000); //
    add_point(340, 80, 12000);
    add_point(560,120, 10000);
    add_point(600, 500,12002);
    add_point(450, 550,8000);

    Regular_triangulation rt(wpoints.begin(), wpoints.end());
    rt.is_valid();
    Regular_triangulation::Edge_iterator eit;
    Regular_triangulation::Point_iterator pit;
    //遍历Delaunay的所有边,绘制Delaunay图的对偶图,即Voronoi图
    glEnable( GL_LINE_STIPPLE );//使用点画模式,即使用虚线来绘制Voronoi图
    glLineStipple( 1, 0x3333 );
    glColor3f( 0.0, 1.0,1.0 );

    cout << "========" << endl;
    for (auto vit = rt.vertices_begin(); vit != rt.vertices_end(); ++vit){
        cout << vit->point() << endl;

    }
    cout << "========" << endl;

    for( eit = rt.edges_begin(); eit != rt.edges_end(); eit ++)
    {
        CGAL::Object o = rt.dual(eit);
        //在其对偶图中所对应的边
        if (CGAL::object_cast<K::Segment_2>(&o)) //如果这条边是线段,则绘制线段
        {
            glBegin(GL_LINES);
            glVertex2i( CGAL::object_cast<K::Segment_2>(&o)->source().hx(), CGAL::object_cast<K::Segment_2>(&o)->source().hy() );
            glVertex2i( CGAL::object_cast<K::Segment_2>(&o)->target().hx(), CGAL::object_cast<K::Segment_2>(&o)->target().hy() );
            glEnd();
        }
        else if (CGAL::object_cast<K::Ray_2>(&o))//如果这条边是射线,则绘制射线
        {
            glBegin(GL_LINES);
            glVertex2i( CGAL::object_cast<K::Ray_2>(&o)->source().hx(), CGAL::object_cast<K::Ray_2>(&o)->source().hy() );
            glVertex2i( CGAL::object_cast<K::Ray_2>(&o)->point(1).hx(), CGAL::object_cast<K::Ray_2>(&o)->point(1).hy() );
            glEnd();
        }
    }
    glDisable( GL_LINE_STIPPLE );//关闭点画模式
}
void InitEnvironment()
{
    //一些初始化操作
    glClearColor(0.0,0.0,0.0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(7);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluOrtho2D(0,window_size,0,window_size);
}
void init_Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.98f, 0.625f, 0.12f);
    display();
    glFlush();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);   //初始化GLUT
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(300, 100);
    glutInitWindowSize(window_size, window_size);
    glutCreateWindow("RT");
    InitEnvironment();   //初始化
    glutDisplayFunc(&init_Display);   //回调函数
    glutMainLoop();    //持续显示,当窗口改变会重新绘制图形
    return 0;
}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值