hdu 3681 计算几何

还是太粗心了阿,求直线上的点到两点间距离和的最小值

搞了好久阿,A点和B点有可能在直线的同侧也可能在异侧

View Code
#include<stdio.h>
#include<math.h>
const double eps = 1e-9;
struct point{
double x,y;
}A,B,C,A1,node;
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cross(point a,point b,point c)
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int main()
{
double k;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lf",&k);
scanf("%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y) ;
double b=C.y-k*C.x;
node.x=1,node.y=k+b;
double x=cross(node,C,A),y=cross(node,C,B);
if(x*y<0)
{
printf("%.2lf\n",dis(A,B));continue;
}
if(k==0)
{
A1.x=A.x;
A1.y=2*C.y-A.y;
}
else
{
double c=fabs(k*A.x-A.y+b);
double y1=(c-k*A.x-k*k*A.y-b)/(-k*k-1);
double y2=(-c-k*A.x-k*k*A.y-b)/(-k*k-1);
if(fabs(y1-A.y)>eps)
A1.y=y1;
else A1.y=y2;
A1.x=A.x-k*(A1.y-A.y);
}
// printf("%.2lf %.2lf\n",A1.x,A1.y);由于这里没注释掉WA了一下午,我汗死了
printf("%.2lf\n",dis(A1,B));
}
return 0;
}



你可能感兴趣的:(HDU)