boost库之geometry
环境:win732位旗舰版、VS2010旗舰版、boost 1.55.0版本、坐标系为MM_TEXT
Geometry是一个开源的几何计算库,包含了几何图形最基本的操作(也支持复杂的操作),下面我们看看怎么使用它。
#include <boost/assign.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/polygon.hpp>
namespace bg = boost::geometry;
typedef bg::model::d2::point_xy<double> DPoint;
typedef bg::model::segment<DPoint> DSegment;
typedef bg::model::linestring<DPoint> DLineString;
typedef bg::model::box<DPoint> DBox;
//这里的ring就是我们通常说的多边形闭合区域(内部不存在缕空),模板参数为true,表示顺时针存储点,为false,表示逆时针存储点,由于MM_TEXT坐标系与传统上的坐标系的Y轴方向是相反的,所以最后为false,将TopLeft、TopRight、BottomRight、BottomLeft、TopLeft以此存储到ring中,以便能正确计算
typedef bg::model::ring<DPoint, false> DRing;
//polygon模板参数false,也是由上面相同的原因得出来的
typedef bg::model::polygon<DPoint, false> DPolygon; int _tmain(int argc, _TCHAR* argv[])
{
DPoint pt0(100, 100);
DPoint pt1(200, 200);
DSegment sg0(pt0, pt1); double dDistance = 0; //1、点到点的距离
dDistance = bg::distance(pt0, pt1);
//2、点到线段的距离,如果点到直线的垂足不在线段上,所计算的距离为(点到直线的距离、线段到垂足延长的距离之和)
dDistance = bg::distance(DPoint(200, 100), sg0);
dDistance = bg::distance(DPoint(100, 0), sg0); //3、判断线段是否相交
DSegment sg1(DPoint(0, 100), DPoint(100, 0));
DSegment sg2(DPoint(100, 200), DPoint(200, 100));
bool bIntersect = false;
bIntersect = bg::intersects(sg0, sg1);
bIntersect = bg::intersects(sg0, sg2); //4、求线段与线段的交点
std::list<DPoint> lstPoints;
bg::intersection(sg0, sg1, lstPoints);
lstPoints.clear();
bg::intersection(sg0, sg2, lstPoints); DBox rc2(DPoint(0, 0), DPoint(0, 0)); //5、判断box是否相交
DBox rc(DPoint(0, 0), DPoint(200, 200));
DBox rc0(DPoint(250, 250), DPoint(450, 450));
DBox rc1(DPoint(100, 100), DPoint(300, 300)); bIntersect = bg::intersects(rc, rc0);
bIntersect = bg::intersects(rc, rc1);
//bg::intersection(rc, rc0, container);//error //6、判断box是否与LineString相交
DLineString line0; line0.push_back(DPoint(10, 250));
line0.push_back(DPoint(100, 100));
line0.push_back(DPoint(120, -10));
line0.push_back(DPoint(210, 200));
bIntersect = bg::intersects(rc, line0);
bIntersect = bg::intersects(rc0, line0); //7、求box与linestring的交点
std::list<DLineString> lstLines;
bg::intersection(rc, line0, lstLines); //8、点是否在box内
DBox rc7(DPoint(0, 0), DPoint(100, 100));
bool bInside = false;
bInside = bg::within(DPoint(50, 50), rc7);
bInside = bg::within(DPoint(0, 0), rc7); //9、判断LineString与LineString是否相交
DLineString line1, line2, line3; line1.push_back(DPoint(50, 50));
line1.push_back(DPoint(150, 50));
line1.push_back(DPoint(50, 200));
line1.push_back(DPoint(150, 200));
line2.push_back(DPoint(100, 0));
line2.push_back(DPoint(70, 100));
line2.push_back(DPoint(150, 210));
line3.push_back(DPoint(200, 0));
line3.push_back(DPoint(200, 200)); bIntersect = bg::intersects(line1, line2);
bIntersect = bg::intersects(line1, line3); //10、求LineString与LineString的交点
lstPoints.clear();
bg::intersection(line1, line2, lstPoints);
lstPoints.clear();
bg::intersection(line1, line3, lstPoints); //11、判断ring与ring是否相交
DPoint arDPoint0[6] = {DPoint(0, 0), DPoint(100, 0), DPoint(200, 100), DPoint(100, 200), DPoint(0, 200), DPoint(0, 0)};
DPoint arDPoint1[6] = {DPoint(100, 100), DPoint(200, 0), DPoint(300, 0), DPoint(300, 200), DPoint(200, 200), DPoint(100, 100)};
DRing r0(arDPoint0, arDPoint0 + 6);
DRing r1(arDPoint1, arDPoint1 + 6);
bIntersect = bg::intersects(r0, r1); //12、求ring与ring的交点
lstPoints.clear();
bg::intersection(r0, r1, lstPoints); DPolygon poly1;
DPolygon poly2;
DPolygon poly3; auto lstOf = boost::assign::list_of(DPoint(0, 0))(DPoint(200, 0))(DPoint(200, 200))(DPoint(0, 200))(DPoint(0, 0));
poly1.outer().assign(lstOf.begin(), lstOf.end());
lstOf = boost::assign::list_of(DPoint(50, 50))(DPoint(150, 50))(DPoint(150, 150))(DPoint(50, 150))(DPoint(50, 50));
poly1.inners().push_back(lstOf);
lstOf = boost::assign::list_of(DPoint(100, 0))(DPoint(120, 0))(DPoint(120, 200))(DPoint(100, 200))(DPoint(100, 0));
poly2.outer().assign(lstOf.begin(), lstOf.end());
lstOf = boost::assign::list_of(DPoint(100, 60))(DPoint(120, 60))(DPoint(120, 140))(DPoint(100, 140))(DPoint(100, 60));
poly3.outer().assign(lstOf.begin(), lstOf.end()); //13、判断polygon与polygon是否相交
bIntersect = bg::intersects(poly1, poly2);
bIntersect = bg::intersects(poly1, poly3); //14、求polygon与polygon相交的区域
std::list<DPolygon> lstPolygon; bg::intersection(poly1, poly2, lstPolygon);
lstPolygon.clear();
bg::intersection(poly1, poly3, lstPolygon); //15、判断点是否在polygon内
bInside = bg::within(DPoint(100, 100), poly1);
bInside = bg::within(DPoint(25, 25), poly1); return 0;
}
boost库之geometry的更多相关文章
- boost库之geometry<二>
#include <boost/assign.hpp> #include <boost/geometry/core/point_type.hpp> #include <b ...
- 【转】boost库之geometry
#include <boost/assign.hpp> #include <boost/geometry/geometry.hpp> #include <boost/ge ...
- Boost 库Program Options--第二篇
程式執行參數處理函式庫:Boost Program Options(2/N) 前一篇已經大致解釋了 Boost Program Options 基本上的使用方法.而這一篇,則來細講一下選項描述(opt ...
- (十二)boost库之多线程高级特性
(十二)boost库之多线程高级特性 很多时候,线程不仅仅是执行一些耗时操作,可能我们还需要得到线程的返回值,一般的处理方法就是定义一个全局状态变量,不断轮训状态,就如我目前维护的一个项目,全局变量定 ...
- (二)boost库之字符串格式化
(二)boost库之字符串格式化 程序中经常需要用到字符串格式化,就个人而言还是比较倾向于C格式的输出,如果只是打印日志,printf就够了,如果到生成字符串,获取你可以选择sprintf,但这些都是 ...
- C++ | boost库 类的序列化
是的,这是今年的情人节,一篇还在研究怎么用的文章,文结的时候应该就用成功了. 恩,要有信心 神奇的分割线 不知何时装过boost库的header-only库, 所以ratslam中的boost是可以编 ...
- boost库学习之regex
一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是, ...
- boost库学习之开篇
本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...
- boost库在windows下的编译和使用
因为跨平台的原因,现在要使用到boost库,boost库非常大,现在处于摸索阶段. 首先来说boost库在window下的安装和使用. 一.下载 首先从boost官方主页http://www.boos ...
随机推荐
- Introducing 'bind'
原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-bind/ 上一篇讨论了如何理解let作为一个能实现contin ...
- STORM在线业务实践-集群空闲CPU飙高问题排查
源:http://daiwa.ninja/index.php/2015/07/18/storm-cpu-overload/ 2015-07-18AUTHORDAIWA STORM在线业务实践-集群空闲 ...
- 为什么 dll 改名字之后无法使用
有人直接把dll名字改了,我的程序运行出错,说这是我程序的问题,难道真是这样吗? 总感觉直接改dll名字不对,但哪儿不对呢,带着这样的疑惑研究了一下,重新做了一下试验,结果程序抛出了错误: Could ...
- hitTest:withEvent:方法流程
此方法可实现点击穿透.点击下层视图功能. 一. hitTest:withEvent:调用过程 iOS系统检测到手指触摸(Touch)操作时会将其放入当前活动Application的事件队列,UIApp ...
- SharePoint 2013 列表启用搜索
转载自:http://www.cnblogs.com/jianyus/p/3470117.html SharePoint 2013列表搜索的设置,只是进行完全爬网,就可以使用.如果开启爬网不是很熟练可 ...
- MySql - JdbcType - Oracle类型映射
MySql - JdbcType - Oracle类型映射 MySQL数据类型 JDBC TYPE Oracle数据类型 BIGINT BIGINT NUMBER(20) TINYINT TINY ...
- 用开源软件建垂直搜索引擎 转载 http://news.cnblogs.com/n/60041/
用Solr.Nutch等开源软件来构建电子元器件垂直搜索引擎涉及很多实现细节,本文结合实际应用系统对数据采集.中文搜索.结果输出.分页处理.整合数据库等重点问题提出了切实可行的解决方法. 用开源软件建 ...
- 学习笔记——解释器模式Interpreter
解释器模式,其实就是编译原理中的语法解释器,如果用在项目中,可以用于实现动态脚本的解析,也就是说项目可以支持用户脚本扩展. 但实际上,这种运行时解释,效率很慢,如果不是很需要的话,不建议使用. 一种简 ...
- 关于C++中字符的转换
VS的工程属性中我们常会设置Character Set:
- android log 学习
一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...