多边形合并、多边形融合算法实现 [C++]

多边形合并、多边形融合算法实现。[C++]

任务如下图所示:

输入为蓝色五个多边形,输出为红色三个多边形,调用boost库实现。
多边形融合实现

#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometry.hpp>

using BoostPoint = boost::geometry::model::d2::point_xy<double>;
using BoostPolygon = boost::geometry::model::polygon<BoostPoint>;
using BoostBox = boost::geometry::model::box<BoostPoint>;

// An highlighted block
std::vector<BoostPolygon> mergeBoxes2PointArrayVec(const std::vector<BoostPolygon> &polygons) {
    std::vector<BoostPolygon> polygonsRes;
    std::vector<bool> merged(polygons.size(), false);  // mark polygon.

    for (std::size_t i = 0; i < polygons.size(); ++i) {
        if (merged[i]) continue;  // skip polygon which has been merged.

        BoostPolygon currentPolygon = polygons[i];
        bool hasMerged = false;

        for (std::size_t j = i + 1; j < polygons.size(); ++j) {
            if (merged[j]) continue;

            // check polygons intersects
            if (boost::geometry::intersects(currentPolygon, polygons[j]) ||
                boost::geometry::touches(currentPolygon, polygons[j])) {
                std::vector<BoostPolygon> unionResult;
                boost::geometry::union_(currentPolygon, polygons[j], unionResult);

                if (!unionResult.empty()) {
                    currentPolygon = unionResult[0];  // return one polygon.
                }
                merged[j] = true;  // mark this polygon.
                hasMerged = true;
            }
        }

        polygonsRes.push_back(currentPolygon);
        merged[i] = hasMerged;
    }
    return polygonsRes;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值