C++ boost几何体相交会产生奇怪的结果
我想使用boost geometry中的求交函数来处理直线和多边形。我希望交点是位于多边形内的直线的一部分 不幸的是,boost geometry返回位于多边形外部的直线部分。这是boost geometry中的错误还是我的代码有问题C++ boost几何体相交会产生奇怪的结果,c++,boost,boost-geometry,C++,Boost,Boost Geometry,我想使用boost geometry中的求交函数来处理直线和多边形。我希望交点是位于多边形内的直线的一部分 不幸的是,boost geometry返回位于多边形外部的直线部分。这是boost geometry中的错误还是我的代码有问题 #include <boost/geometry/core/cs.hpp> #include <boost/geometry/geometries/point.hpp> #include <boost/geometry/geometr
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/multi/geometries/multi_point.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/algorithms/intersection.hpp>
namespace bg = boost::geometry;
using value_type = double ;
using cs_type = bg::cs::cartesian;
using point_type = bg::model::point< value_type , 2 , cs_type >;
using polygon_type = bg::model::ring< point_type > ;
using line_string_type = bg::model::linestring< point_type >;
using multi_line_type = bg::model::multi_linestring< line_string_type >;
int main( int argc , char *argv[] )
{
line_string_type line;
line.push_back( point_type { 13.37688020921095 , 53.66231710654281 } );
line.push_back( point_type { 13.3857295713429 , 53.6636835518369 } );
line.push_back( point_type { 13.39213495232734 , 53.66501934623722 } );
line.push_back( point_type { 13.39719615524716 , 53.66546436809296 } );
line.push_back( point_type { 13.40724694386097 , 53.66240690770171 } );
polygon_type polygon;
polygon.push_back( point_type { 13.35 , 53.64 } );
polygon.push_back( point_type { 13.39 , 53.64 } );
polygon.push_back( point_type { 13.39 , 53.68 } );
polygon.push_back( point_type { 13.35 , 53.68 } );
polygon.push_back( point_type { 13.35 , 53.64 } );
multi_line_type intersection;
bg::intersection( line , polygon , intersection );
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间bg=boost::geometry;
使用值_type=double;
使用cs_type=bg::cs::cartesian;
使用point_type=bg::model::point;
使用多边形类型=bg::model::ring;
使用line_string_type=bg::model::linestring;
使用multi_line_type=bg::model::multi_linestring;
int main(int argc,char*argv[])
{
行\字符串\类型行;
向后推(点类型{13.37688020921095,53.66231710654281});
向后推(点类型{13.3857295713429,53.6636835518369});
线。推回(点类型{13.39213495232734,53.66501934623722});
向后推(点类型{13.39719615524716,53.66546436809296});
向后推(点类型{13.40724694386097,53.6624069077017});
多边形\ U型多边形;
多边形。推回(点类型{13.35,53.64});
多边形。推回(点类型{13.39,53.64});
多边形。推回(点类型{13.39,53.68});
多边形。推回(点类型{13.35,53.68});
多边形。推回(点类型{13.35,53.64});
多线式交叉;
bg::交点(直线、多边形、交点);
返回0;
}
确保您的输入几何体满足记录的前提条件
您可以使用bg::correct
修复大多数问题(例如多边形中点的正确CCW顺序、关闭未闭合多边形等):
#include <boost/geometry/core/cs.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/multi/geometries/multi_point.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/algorithms/intersection.hpp>
namespace bg = boost::geometry;
using value_type = double;
using cs_type = bg::cs::cartesian;
using point_type = bg::model::point<value_type, 2, cs_type>;
using polygon_type = bg::model::ring<point_type>;
using line_string_type = bg::model::linestring<point_type>;
using multi_line_type = bg::model::multi_linestring<line_string_type>;
int main()
{
line_string_type line;
line.push_back(point_type{13.37688020921095, 53.66231710654281});
line.push_back(point_type{13.3857295713429, 53.6636835518369});
line.push_back(point_type{13.39213495232734, 53.66501934623722});
line.push_back(point_type{13.39719615524716, 53.66546436809296});
line.push_back(point_type{13.40724694386097, 53.66240690770171});
bg::correct(line);
polygon_type polygon;
polygon.push_back(point_type{13.35, 53.64});
polygon.push_back(point_type{13.39, 53.64});
polygon.push_back(point_type{13.39, 53.68});
polygon.push_back(point_type{13.35, 53.68});
polygon.push_back(point_type{13.35, 53.64});
bg::correct(polygon);
multi_line_type intersection;
bg::intersection(line, polygon, intersection);
std::cout << bg::wkt(intersection);
}
如果输入不正确,它将打印以下内容:
MULTILINESTRING((13.39 53.6646,13.3921 53.665,13.3972 53.6655,13.4072 53.6624))
增加了可视化功能。有关如何制作漂亮的SVG图形的信息:很好的答案!现在一切正常。谢谢@seheCheers!我忘了提到(这需要增压1.57+AFAIR)。它们是输入验证的基本工具。罚点球是可以的,所以小心点!
MULTILINESTRING((13.39 53.6646,13.3921 53.665,13.3972 53.6655,13.4072 53.6624))