前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用CGAL轻松检索两条相交多边形的相交线

如何使用CGAL轻松检索两条相交多边形的相交线

作者头像
用户3519280
发布2023-07-06 14:05:33
2990
发布2023-07-06 14:05:33
举报
文章被收录于专栏:c++ 学习分享c++ 学习分享

如何使用CGAL轻松检索两条相交多边形的相交线(从第一个交点到最后一个交点)。看到图像的澄清,绿线是我想要的。使用CGAL获取多边形相交线

Two intersecting polygons with intersection line

目前我使用下面的算法,在那里我得到的交集多边形,然后发现这是两个多边形的边界点,这应该是交叉点。这里是代码:

代码语言:javascript
复制
Polygon_2 P,Q; 
Pwh_list_2     intR; 
Pwh_list_2::const_iterator it; 
CGAL::intersection(P, Q, std::back_inserter(intR)); 

//Loop through intersection polygons 
for (it = intR.begin(); it != intR.end(); ++it) { 
    boost::numeric::ublas::vector<double> firstIntersectPoint(3), lastIntersectPoint(3); 
    Polygon_2 Overlap = it->outer_boundary(); 
    typename CGAL::Polygon_2<Kernel>::Vertex_iterator vit; 
    int pointNr = 1; 

    //Loop through points of intersection polygon to find first and last intersection point. 
    for (vit = Overlap.vertices_begin(); vit != Overlap.vertices_end(); ++vit) { 
     CGAL::Bounded_side bsideThis = P.bounded_side(*vit); 
     CGAL::Bounded_side bsideArg = Q.bounded_side(*vit); 
     if (bsideThis == CGAL::ON_BOUNDARY && bsideArg == CGAL::ON_BOUNDARY && pointNr == 1) { 
      firstIntersectPoint <<= 0, CGAL::to_double(vit->x()), CGAL::to_double(vit->y()); 
      pointNr = 2; 
     } 
     else if (bsideThis == CGAL::ON_BOUNDARY && bsideArg == CGAL::ON_BOUNDARY && pointNr == 2) { 
      lastIntersectPoint <<= 0, CGAL::to_double(vit->x()), CGAL::to_double(vit->y()); 
      pointNr = 2; 
     } 
    } 

    //RESULT 
    std::cout << firstIntersectPoint << std::endl; 
    std::cout << lastIntersectPoint << std::endl; 
} 

虽然这个作品,我不认为这是正确的方式去。有人可以告诉我这是否是正确的方法,或者指出如何更好地做到这一点。

来源

2017-08-02 D.J. Klomp A 回答 2 将两个多边形的线段插入到2D排列中。然后找到具有度4的顶点。注意,在一般情况下可能有2个以上;有可能是没有,有可能是1

代码语言:javascript
复制
std::list<X_monotone_curve_2> segments; 
for (const auto& pgn : polygons) { 
    for (auto it = pgn.curves_begin(); it != pgn.curves_end(); ++it) 
    segments.push_back(*it); 
} 
Arrangement_2 arr; 
insert(arr, segments.begin(), segments.end()); 
for (auto it = arr.begin_vertices(); it != arr.end_vertices(); ++it) { 
    if (4 == it->degree()) 
    ... 
} 

可以避开“段”名单的建设,而是直接将多边形细分成使用迭代器适配器的安排。 (这是纯粹的通用编程,与CGAL无关。)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档