Description
Input
Output
Sample Input
input | output |
---|---|
8 -6 8 6 0 0 7 |
1.00 3.00 |
#include<stdio.h> #include<cmath> #include<stdlib.h> #include<string.h> #define eps 1e-6 struct point { double x,y; }; //以下是求两点到线段最短距离的模版,请见: http://www.shabiyuan.com/?p=234 double distance(point p1,point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } double xmult(point p1,point p2,point p0) { return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y); } point intersection(point u1,point u2,point v1,point v2) { point ret = u1; double t = ((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x)) /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x)); ret.x+=(u2.x-u1.x)*t; ret.y+=(u2.y-u1.y)*t; return ret; } point ptoseg(point p,point l1,point l2) { point t=p; t.x+=l1.y-l2.y; t.y+=l2.x-l1.x; if(xmult(l1,t,p)*xmult(l2,t,p)>eps) { return distance(p,l1)<distance(p,l2)?l1:l2; } return intersection(p,t,l1,l2); } double max(double a,double b) { return a>b?a:b; } int main() { point a,b,c; double l; double lena,lenb,lenc; double minlen,maxlen; scanf("%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&l); minlen = distance(ptoseg(c,a,b),c)-l; maxlen = max(distance(c,a),distance(c,b))-l; if(a.x==b.x&&a.y==b.y)//判断线段两端点是不是重合 { minlen = distance(c,a)-l; maxlen = distance(c,a)-l; } if(minlen<0)//当距离小的时候,当然是不要加长了,变成0,下同。 { minlen = 0; } if(maxlen<0) { maxlen = 0; } printf("%.2lf\n%.2lf\n",minlen,maxlen); return 0; }