点到线段的最短距离
2011-06-21 13:59
323 查看
private double pointToLine(int x1, int y1, int x2, int y2, int x0,
int y0) {
double space = 0;
double a, b, c;
a = lineSpace(x1, y1, x2, y2);// 线段的长度
b = lineSpace(x1, y1, x0, y0);// (x1,y1)到点的距离
c = lineSpace(x2, y2, x0, y0);// (x2,y2)到点的距离
if (c <= 0.000001 || b <= 0.000001) {
space = 0;
return space;
}
if (a <= 0.000001) {
space = b;
return space;
}
if (c * c >= a * a + b * b) {
space = b;
return space;
}
if (b * b >= a * a + c * c) {
space = c;
return space;
}
double p = (a + b + c) / 2;// 半周长
double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积
space = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)
return space;
}
// 计算两点之间的距离
private double lineSpace(int x1, int y1, int x2, int y2) {
double lineLength = 0;
lineLength = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)
* (y1 - y2));
return lineLength;
}
int y0) {
double space = 0;
double a, b, c;
a = lineSpace(x1, y1, x2, y2);// 线段的长度
b = lineSpace(x1, y1, x0, y0);// (x1,y1)到点的距离
c = lineSpace(x2, y2, x0, y0);// (x2,y2)到点的距离
if (c <= 0.000001 || b <= 0.000001) {
space = 0;
return space;
}
if (a <= 0.000001) {
space = b;
return space;
}
if (c * c >= a * a + b * b) {
space = b;
return space;
}
if (b * b >= a * a + c * c) {
space = c;
return space;
}
double p = (a + b + c) / 2;// 半周长
double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积
space = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)
return space;
}
// 计算两点之间的距离
private double lineSpace(int x1, int y1, int x2, int y2) {
double lineLength = 0;
lineLength = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)
* (y1 - y2));
return lineLength;
}
相关文章推荐
- 点到线段最短距离的算法
- 三维空间两直线/线段最短距离、线段计算算法
- as3 点到线段最短距离 函数
- 点到线段的最短距离
- 求空间中2条线段的最短距离(用osg+C++写的)
- 点到线段的最短距离
- 点到线段的最短距离
- 北航1033 Easy Problem 点到线段最短距离 三分法
- 点到线段的最短距离
- 点到线段的最短距离
- light1260Race Track【点到线段最短距离】
- 点到线段的最短距离算法
- BUAA 1033 (三分枚举计算 点到线段的最短距离)
- 计算点到线段最短距离的代码
- 点到线段的最短距离算法(转自 http://blog.sina.com.cn/s/blog_5d5c80840101bnhw.html)
- 点到线段的最短距离
- Codeforces Round #339 (Div. 2)-C(点到线段的最短距离)
- 三维空间两直线/线段最短距离、线段计算算法 【转】
- hdoj4741 求空间中不平行的两条直线的最短距离及最短线段与两直线的交点
- UVa 10263 Railway (点到线段的最短距离)