soj1209- 最短的距离(精度问题)
1209: 最短的距离
Time Limit: 1 Sec Memory Limit: 128 MB Submit: 149 Solved: 5 [Submit][Status][Web Board]Description
如图所示:
这里有一个平行于X轴的长方形(红色标出)和一个点(x,y);你能帮UnkelTao计算下点到长方形的最短距离吗?
Input
多组测试数据;
每组数据含有10个实数,前八个分别表示长方形的四个坐标x0,y0,x1,y1,x2,y2,x3,y3;后两个表示点的坐标x,y; 其中每个数的绝对值均小于10000并且矩阵的四个坐标点均按顺时针或者逆时针给出。
保证输入图形为矩阵。.
Output
输出最短的距离值,结果保留2位小数。输出格式见样例
Sample Input
0 0 0 2 2 2 2 0 1 3 0 0 0 2 2 2 2 0 3 3
Sample Output
1.00 1.41
HINT
输入输出最好使用%lf
----------------------------------------------------
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<fstream>
using namespace std;
double eps=0.00000001;
double x1,x2,yy1,y2;
double x,y;
double sqr(double a)
{
return a*a;
}
double minn(double a,double b,double c,double d)
{
if (a-b<eps) b=a;
if (b-c<eps) c=b;
if (c-d<eps) d=c;
return d;
}
double deal()
{
double t,t1,t2,t3,t4;
if (x-x1<=eps)
{ t1=x-x1;t2=x2-x;
t
if (y-y2>=eps) return sqrt(sqr(x1-x)+sqr(y-y2));
if (y-yy1<=eps) return sqrt(sqr(x1-x)+sqr(yy1-y));
return x1-x+eps;
}
if (x-x2>=eps)
{
if (y-y2>=eps) return sqrt(sqr(x2-x)+sqr(y-y2));
if (y-yy1<=eps) return sqrt(sqr(x2-x)+sqr(yy1-y));
return x-x2+eps;
}
if (y-y2>=eps) return (y-y2+eps);
if (y-yy1<=eps) return (yy1-y+eps);
3=y-yy1;t4=y2-y;
return minn(t1,t2,t3,t4)+eps;
}
int main()
{
double t,a,b;
// freopen("data.in","r",stdin);
// freopen("a.out","w",stdout);
while (scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&yy1,&a,&b,&x2,&y2,&a,&b,&x,&y)!=EOF)
{
if (x1-x2>=eps) {t=x1;x1=x2;x2=t;}
if (yy1-y2>=eps) {t=yy1;yy1=y2;y2=t;}
printf("%.2lf\n",deal());
}
}