boost::geometry::read_wkt备选方案
boost::geometry::read_wkt alternative?
我正在尝试检查一个点是否在多边形内。为此,我想使用boost库。我的问题是如何修改boost中的示例,以便从stdvector中读取点,而不是通过read_wkt读取点。
这是boost:的示例代码
#include <iostream>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
int main()
{
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon_type;
polygon_type poly;
boost::geometry::read_wkt(
"POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
"(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);
point_type p(4, 1);
std::cout << "within: " << (boost::geometry::within(p, poly) ? "yes" : "no") << std::endl;
return 0;
}
这是我的vecPoints:
struct PNTS{
int x;
int y;
}
std::vector<PNTS> vecPoints;
请注意:我的问题与多边形算法中的点无关。
如果您想在boost:中添加多边形内的向量成员
using boost::geometry::append;
using boost::geometry::make;
using boost::geometry::correct;
std::vector<PNTS>::iterator iter;
for(iter = vecPoints.begin(); iter != vecPoints.end(); iter++)
{
PNTS ver = *iter;
append( poly, make<boost2dPoint>(ver.x, ver.y) );
}
// you have to close polygon by inserting first element as the last again
PNTS last = vecPoints[vecPoints.size()-1];
append( poly, make<boost2dPoint>(last.x, last.y) );
// you can also correct the polygon orientation
correct(poly);
我对几何概念了解不多。然而,这看起来应该很接近,或者至少给你一些做事的灵感。
Update向WKT添加了序列化,这样我们就可以确认polyon是相同的。
在Coliru上直播
#include <iostream>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/io.hpp>
int main()
{
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon_type;
polygon_type poly;
poly.outer().assign({
point_type { 2, 1.3 },
point_type { 2.4, 1.7 },
point_type { 2.8, 1.8 },
point_type { 3.4, 1.2 },
point_type { 3.7, 1.6 },
point_type { 3.4, 2 },
point_type { 4.1, 3 },
point_type { 5.3, 2.6 },
point_type { 5.4, 1.2 },
point_type { 4.9, 0.8 },
point_type { 2.9, 0.7 },
point_type { 2, 1.3 },
});
poly.inners().emplace_back();
poly.inners().back().assign({
{ 4.0, 2.0 },
{ 4.2, 1.4 },
{ 4.8, 1.9 },
{ 4.4, 2.2 },
{ 4.0, 2.0 },
});
point_type p(4, 1);
std::cout << "within: " << (boost::geometry::within(p, poly) ? "yes" : "no") << std::endl;
std::cout << boost::geometry::wkt(poly) << "n";
}
打印
within: yes
POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3),(4 2,4.2 1.4,4.8 1.9,4.4 2.2,4 2))
如果像评论者所说的那样,你认为手动从矢量创建WKT更容易,这里有一种方法,在大约2行代码中完成,而不使用Boost Geometry:中的任何东西
using namespace boost::spirit::karma;
std::cout << format_delimited("POLYGON(" << *('(' << auto_%',' << ')') << ")n", ' ', rings);
完整演示Coliru
#include <iostream>
#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/spirit/include/karma.hpp>
int main()
{
using Ring = std::vector<boost::tuple<double,double>>;
std::vector<Ring> rings = {
{ { 2, 1.3 }, { 2.4, 1.7 }, { 2.8, 1.8 }, { 3.4, 1.2 },
{ 3.7, 1.6 }, { 3.4, 2 }, { 4.1, 3 }, { 5.3, 2.6 },
{ 5.4, 1.2 }, { 4.9, 0.8 }, { 2.9, 0.7 }, { 2, 1.3 },
},
{ { 4.0, 2.0 }, { 4.2, 1.4 }, { 4.8, 1.9 }, { 4.4, 2.2 }, { 4.0, 2.0 },
}
};
using namespace boost::spirit::karma;
std::cout << format_delimited("POLYGON(" << *('(' << auto_%',' << ')') << ")n", ' ', rings);
}
打印:
POLYGON( ( 2.0 1.3 , 2.4 1.7 , 2.8 1.8 , 3.4 1.2 , 3.7 1.6 , 3.4 2.0 , 4.1 3.0 , 5.3 2.6 , 5.4 1.2 , 4.9 0.8 , 2.9 0.7 , 2.0 1.3 ) ( 4.0 2.0 , 4.2 1.4 , 4.8 1.9 , 4.4 2.2 , 4.0 2.0 ) )
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- 计算每个节点的树高,帮助我解释这个代码解决方案
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- visual c++,如何获取解决方案目录中的代码
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- C++11 中不同类型的对象的 std::array 的替代方案
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 无法在问题解决方案中执行输出逻辑
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- 在一个解决方案中针对第三方静态库 (Creo) 的不同版本(版本)进行构建
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- N-queen问题:无法弄清楚为什么我的解决方案不起作用
- 从排序数组中删除重复项,具有不同代码方式的相同解决方案具有不同的输出