lightoj 1240 计算机图论(计算三维点到线段的距离)
来源:互联网 发布:java迭代器的使用 编辑:程序博客网 时间:2024/05/19 18:17
Description
Given a segment in 3D space, identified by A(x1, y1, z1), B(x2, y2, z2) and another point P(x, y, z) your task is to find the minimum possible Euclidean distance between the point P and the segment AB.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing nine integers x1, y1, z1, x2, y2, z2, x, y, z. The magnitude of any integer will not be greater than 100.
Output
For each case, print the case number and the distance. Errors less than 10-6 will be ignored.
Sample Input
2
0 0 1 0 1 1 0 1 0
0 0 0 1 1 1 0 0 1
Sample Output
Case 1: 1
Case 2: 0.8164965809
代码如下:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>using namespace std;struct Point3D { //三维点或矢量 double x, y, z;};struct Line3D { // 三维的直线或线段 Point3D p1, p2;};double cross( Point3D &a,Point3D &b, Point3D &c){ return (a.x-c.x)*(b.x-c.x) + (a.y-c.y)*(b.y-c.y) +(a.z-c.z)*(b.z-c.z);}double dis(Point3D &a,Point3D &b){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);}int main(){ //freopen("in.txt","r",stdin); int T; Point3D p,p1,p2; scanf("%d",&T); for(int i=1;i<=T;i++){ scanf("%lf %lf %lf %lf %lf %lf %lf %lf %lf",&p1.x,&p1.y,&p1.z,&p2.x,&p2.y,&p2.z,&p.x,&p.y,&p.z); double d1 = dis(p,p1); double d2 = dis(p,p2); //cout << d1 << " " << d2 << endl; double ans; double c1 = cross(p,p1,p2); double c2 = cross(p,p2,p1); if ( c1 * c2 <= 0 )///判断垂足位置 ans = sqrt(double (min(d1,d2))); else { double d3 = dis(p1,p2); double s1 = sqrt(d1); double s2 = sqrt(d2); double s3 = sqrt(d3); double c = (s1+s2+s3)/2; double s = sqrt(c*(c-s1)*(c-s2)*(c-s3));///海伦公式 ans = 2*s/s3; } printf("Case %d: %.10lf\n",i,ans);} return 0;}
0 0
- lightoj 1240 计算机图论(计算三维点到线段的距离)
- 计算点到线段的最近距离
- 如何计算点到线段的距离
- 如何计算点到线段的最近距离
- 点到线段的距离
- 点到线段的距离
- 点到线段的距离
- 点到线段的距离
- 点到线段的距离
- 点到线段的距离
- 计算几何 点到线段的距离 点在简单多边形内 点到凸多边形的距离
- 实现 计算两个三维坐标点的 距离
- 点到线段的最小距离(C++)
- 点到线段的垂足距离
- 点到直线、线段的距离
- 点到线段之间的距离
- 点到线段的垂直距离
- 点到线段的最大最小距离
- VB Google Translation
- javascript常用插件
- sql server 2008 win7下安装检查,重启计算机 失败
- uva 502(模拟)
- oracle db中,某一种数据字典pct miss的计算公式
- lightoj 1240 计算机图论(计算三维点到线段的距离)
- SSH启动异常: java.lang.NoClassDefFoundError: javax/el/ExpressionFactory
- lombok配置问题
- instanceof
- The difference between register(寄存器), latch(锁存器) and flip-flop(触发器)
- HDU 1047 Integer Inquiry 大数相加 string解法
- http://www.doudoutxt.com/files/article/51465/728de201.html
- http://www.doudoutxt.com/files/article/51465/759z992.html
- List 的排序,重载greater<>实现