C++ 上不正确的工作 boost::geometry::union_
Posted
技术标签:
【中文标题】C++ 上不正确的工作 boost::geometry::union_【英文标题】:Incorrect work boost::geometry::union_ on C++ 【发布时间】:2021-04-24 07:10:17 【问题描述】:我想在图片上使用带有多边形的 boost::geometry::union_,但它看不到点 E(例如,多边形 ABCDE 的面积为 ABCD)。我该如何解决?
【问题讨论】:
您需要显示代码。图片不反映您如何表示多边形。 【参考方案1】:我会这样写你的练习。我设计了一个类似的场景(这些点的名称可能不同,但这不是 [原文如此] 点):
注意:我已经将坐标四舍五入到最接近的整数(然后缩放 x10)
Live On Wandbox
#include <boost/geometry.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <iostream>
#include <fstream>
namespace bg = boost::geometry;
namespace bgm = bg::model;
using Point = bgm::d2::point_xy<int>;
using Polygon = bgm::polygon<Point>;
using Multi = bgm::multi_polygon<Polygon>;
int main()
auto prepare = [](auto name, auto& geo)
std::string r;
if (!bg::is_valid(geo, r))
std::cout << "Correcting " << name << ": " << bg::wkt(geo) << " (" << r << ")\n";
bg::correct(geo);
std::cout << name << ": " << bg::wkt(geo) << "\n";
;
Point A-120, 50, B-60, 70, C-70, 50, D-40, 00, E-130, 10;
Point F-20, -20, G40, -10, H80, 60, I50, 80, J-20, 50, K30, 30;
Polygon ABCDE(A, B, C, D, E, A);
Polygon DFGHIJK(D, F, G, H, I, J, K, D);
prepare("ABCDE", ABCDE);
prepare("DFGHIJK", DFGHIJK);
Multi out;
bg::union_(ABCDE, DFGHIJK, out);
prepare("union", out);
namespace bs = bg::strategy::buffer;
const double buffer_distance = 3;
bs::distance_symmetric<double> distance(buffer_distance);
bs::join_miter join;
bs::end_flat end;
bs::point_circle circle(6);
bs::side_straight side;
Multi outline;
bg::buffer(out, outline, distance, side, join, end, circle);
std::ofstream svg("output.svg");
boost::geometry::svg_mapper<Point> mapper(svg, 400, 400);
mapper.add(ABCDE);
mapper.add(DFGHIJK);
mapper.add(outline);
mapper.map(ABCDE,
"fill-opacity:0.3;fill:rgb(153,0,0);stroke:rgb(153,0,0);"
"stroke-width:1;stroke-dasharray:1 2");
mapper.map(DFGHIJK,
"fill-opacity:0.3;fill:rgb(0,0,153);stroke:rgb(0,0,153);"
"stroke-width:1;stroke-dasharray:1 2 2 1");
mapper.map(outline,
"fill-opacity:0.1;fill:rgb(153,204,0);stroke:rgb(153,204,0);"
"stroke-width:1");
std::cout << "Areas: " << bg::area(ABCDE) << " + " << bg::area(DFGHIJK)
<< " = " << bg::area(out) << "\n";
打印预期的:
ABCDE: POLYGON((-120 50,-60 70,-70 50,-40 0,-130 10,-120 50))
Correcting DFGHIJK: POLYGON((-40 0,-20 -20,40 -10,80 60,50 80,-20 50,30 30,-40 0)) (Geometry has wrong orie
ntation)
DFGHIJK: POLYGON((-40 0,30 30,-20 50,50 80,80 60,40 -10,-20 -20,-40 0))
union: MULTIPOLYGON(((-40 0,-130 10,-120 50,-60 70,-70 50,-40 0)),((-40 0,30 30,-20 50,50 80,80 60,40 -10,-
20 -20,-40 0)))
Areas: 3600 + 5800 = 9400
它还会生成以下 SVG:
我希望我处理它的方式,特别是诊断代码对您有所帮助。
【讨论】:
以上是关于C++ 上不正确的工作 boost::geometry::union_的主要内容,如果未能解决你的问题,请参考以下文章
Embarcadero C++ builder 10.4.2-附加到进程在 64 位上不起作用