点线段的距离函数
2016-05-24 13:28
323 查看
点线段的距离函数
#include <iostream>
#include <cmath>
struct POINT
{
double x;
double y;
};
typedef POINT VECTOR;
struct SEGMENT
{
POINT * pStart;
POINT * pEnd;
};
class Segment
{
public:
POINT * pStart;
POINT * pEnd;
Segment() : pStart(NULL), pEnd(NULL) {}
Segment(POINT * p1, POINT * p2) : pStart(p1), pEnd(p2) {}
double GetLength() const;
double GetDist(const POINT * point) const;
double GetDist(const POINT & point) const;
};
double Segment::GetLength() const
{
if(pStart == NULL || pEnd == NULL)
return 0.0;
return sqrt(pow(pStart->x - pEnd->x, 2) + pow(pStart->y - pEnd->y,2));
}
double Segment::GetDist(const POINT * point) const
{
VECTOR sp = {point->x - pStart->x, point->y - pStart->y};
VECTOR se = {pEnd->x - pStart->x, pEnd->y - pStart->y};
VECTOR es = {-se.x, -se.y};
VECTOR ep = {point->x - pEnd->x, point->y - pEnd->y};
if(sp.x * se.x + sp.y * se.y <= 0)
return sqrt(pow(pStart->x - point->x,2) + pow(pStart->y - point->y,2));
else if(ep.x * es.x + ep.y * es.y <= 0)
return sqrt(pow(pEnd->x - point->x, 2) + pow(pEnd->y - point->y, 2));
else
{
double abs_sp = sqrt(sp.x * sp.x + sp.y * sp.y);
double abs_se = sqrt(se.x * se.x + se.y * se.y);
double cos = (sp.x * es.x + sp.y * es.y)/(abs_sp * abs_se);
return abs_sp * sqrt(1 - cos * cos);
}
}
double Segment::GetDist(const POINT & point) const
{
return GetDist(&point);
}
int main()
{
POINT p1 = {1.0, 2.0};
POINT p2 = {2.0, 2.0};
POINT p3 = {1.5, 4};
Segment pn = Segment(&p1, &p2);
std::cout<<pn.GetDist(p3)<<std::endl;
return 0;
}
#include <iostream>
#include <cmath>
struct POINT
{
double x;
double y;
};
typedef POINT VECTOR;
struct SEGMENT
{
POINT * pStart;
POINT * pEnd;
};
class Segment
{
public:
POINT * pStart;
POINT * pEnd;
Segment() : pStart(NULL), pEnd(NULL) {}
Segment(POINT * p1, POINT * p2) : pStart(p1), pEnd(p2) {}
double GetLength() const;
double GetDist(const POINT * point) const;
double GetDist(const POINT & point) const;
};
double Segment::GetLength() const
{
if(pStart == NULL || pEnd == NULL)
return 0.0;
return sqrt(pow(pStart->x - pEnd->x, 2) + pow(pStart->y - pEnd->y,2));
}
double Segment::GetDist(const POINT * point) const
{
VECTOR sp = {point->x - pStart->x, point->y - pStart->y};
VECTOR se = {pEnd->x - pStart->x, pEnd->y - pStart->y};
VECTOR es = {-se.x, -se.y};
VECTOR ep = {point->x - pEnd->x, point->y - pEnd->y};
if(sp.x * se.x + sp.y * se.y <= 0)
return sqrt(pow(pStart->x - point->x,2) + pow(pStart->y - point->y,2));
else if(ep.x * es.x + ep.y * es.y <= 0)
return sqrt(pow(pEnd->x - point->x, 2) + pow(pEnd->y - point->y, 2));
else
{
double abs_sp = sqrt(sp.x * sp.x + sp.y * sp.y);
double abs_se = sqrt(se.x * se.x + se.y * se.y);
double cos = (sp.x * es.x + sp.y * es.y)/(abs_sp * abs_se);
return abs_sp * sqrt(1 - cos * cos);
}
}
double Segment::GetDist(const POINT & point) const
{
return GetDist(&point);
}
int main()
{
POINT p1 = {1.0, 2.0};
POINT p2 = {2.0, 2.0};
POINT p3 = {1.5, 4};
Segment pn = Segment(&p1, &p2);
std::cout<<pn.GetDist(p3)<<std::endl;
return 0;
}
相关文章推荐
- Ubantu安装cuda驱动教程
- Java堆栈简单介绍
- 设计模式之-单例实现
- Android 面试题总结之Android 基础(五)
- iOS打包发布过程
- 分配问题[网络流24题之18]
- IOS9适配技巧(转)
- 数据结构之------深度优先搜索(DFS)
- iOS - Masonry布局下的属性动画实现 ,UIVIew动态旋转
- Android浏览图片,点击放大至全屏效果
- 样本不均衡(2)
- java求多个数的最大公约数,最小公倍数。
- 【转载转载转载】【项目管理和构建】十分钟教程,eclipse配置maven + 创建maven项目(三)
- SDcard 篇
- SDAU练习三1017
- QString类
- 第11周-课后实践(2)
- C++实验6-字符串操作
- PHP常用魔术方法(__call魔术方法:)
- 面向对象的JavaScript-002