基于boost实现点到线段的投影
2016-05-31 16:06
465 查看
该算法实现于boos中t的geomertry中,在distance算法函数中,其中的求点到线段的距离中用到了投影计算
其核心思想是:三角形的相似性,通过向量点乘,得到两个相似三角形的边的比值
/*
*brief: project one point onto a segment
*parma1: the point to be projected
*param2: the start point of segment
*param3: the end point of segment
*parma4: the projected point on the segment
*/
bool point_project_on_segment(point_t const & p, point_t const & p1, point_t const & p2, point_t & p_prj)
{
/*
Algorithm [p: (px,py), p1: (x1,y1), p2: (x2,y2)]
VECTOR v(x2 - x1, y2 - y1)
VECTOR w(px - x1, py - y1)
c1 = w . v
c2 = v . v
b = c1 / c2
RETURN POINT(x1 + b * vx, y1 + b * vy)
*/
point_t w, v, projected;
bg::convert(p2, v);
bg::convert(p, w);
bg::convert(p1, projected);
bg::subtract_point(v, projected);
bg::subtract_point(w, projected);
double const zero = 0;
double c1 = bg::dot_product(w, v);
if (c1 <= zero) {
return false;
}
double c2 = bg::dot_product(v, v);
if (c2 < c1) {
return false;
}
double b = c1 / c2;
bg::multiply_value(v, b);
bg::add_point(projected, v);
p_prj = projected;
return true;
}
其核心思想是:三角形的相似性,通过向量点乘,得到两个相似三角形的边的比值
/*
*brief: project one point onto a segment
*parma1: the point to be projected
*param2: the start point of segment
*param3: the end point of segment
*parma4: the projected point on the segment
*/
bool point_project_on_segment(point_t const & p, point_t const & p1, point_t const & p2, point_t & p_prj)
{
/*
Algorithm [p: (px,py), p1: (x1,y1), p2: (x2,y2)]
VECTOR v(x2 - x1, y2 - y1)
VECTOR w(px - x1, py - y1)
c1 = w . v
c2 = v . v
b = c1 / c2
RETURN POINT(x1 + b * vx, y1 + b * vy)
*/
point_t w, v, projected;
bg::convert(p2, v);
bg::convert(p, w);
bg::convert(p1, projected);
bg::subtract_point(v, projected);
bg::subtract_point(w, projected);
double const zero = 0;
double c1 = bg::dot_product(w, v);
if (c1 <= zero) {
return false;
}
double c2 = bg::dot_product(v, v);
if (c2 < c1) {
return false;
}
double b = c1 / c2;
bg::multiply_value(v, b);
bg::add_point(projected, v);
p_prj = projected;
return true;
}
相关文章推荐
- ibatis实现Iterate的使用
- Middle-题目45:215. Kth Largest Element in an Array
- 【BZOJ3522】[Poi2014]Hotel【DFS】
- window.onload
- 第13周项目1:分数类中的运算符重载(2)
- 第12周项目3-数组类运算的实现
- zabbix客户端安装
- Middle-题目44:334. Increasing Triplet Subsequence
- Tomcat配置JNDI数据源
- apache开启vhost后,适应ThinkPHP的rewrite模式
- 内网服务器配置DDNS实现外网固定域名访问
- JavaScript学习--Item29 DOM基础详解
- 用Kibana和logstash快速搭建实时日志查询、收集与分析系统
- orcale使用row_number序号递增,遇到不同的序号重新递增
- 我的第一个安卓工程:云计算环境下基于安卓的任务执行系统 (1)系统概述
- AFNetworking3.1 POST 请求
- win7系统连接蓝牙鼠标或键盘后无线网络总是掉线的原因及解决办法
- 数据库连接池
- Middle-题目42/43:274. H-Index && 275. H-Index II
- Android 对话框封装(确认/取消 )