URAL 1348(幾何)(解題報告)


Goat in the Garden 2

Description

A goat is tied to a peg (in a point C) in a garden with a strong rope of the length L (i.e. a goat may eat a grass that is not farther than Lmeters from the peg). There is a bed of pineapples that he loves very much. The bed is a line segment with the ends A and B.

Humph… We wonder, how much the goat is to stretch the roap in order to reach at least one pine apple? And all the pineapples?

Input

There are points’ A, B and C coordinates and a length of the rope L in the input. All the numbers are integer, L ≥ 0, all the coordinates don’t exceed 10000 by the absolute value. The numbers are separated with spaces or line feeds.

Output

The first line should contain the minimal length that the goat is to elongate the rope in order to reach the pineapples bed. The second line should contain the minimal length that the goat is to elongate the rope in order to eat all the pineapples from the bed. All the numbers are to be outputted within two digits after a decimal point.

Sample Input

input

output

8 -6 8 6

0 0 7

3.00

思路:求點到線段的最短和最長距離;:

有幾個公式的應用:直線方程:Ax+By+C=0;點到直線的距離:d=|Ax+By+C|/(sqrt(A*A+B*B));

#

include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
const double eps=1e-8;
int is_online(double x1,double y1,double x2,double y2,double x,double y)//判斷(x,y)是否在線段上 
{
if(((x-x1)*(x-x2)+(y-y1)*(y-y2))<=eps)//(注意精度問題) 
return 1;
else
return 0;
}
int main()
{
    int i,j;
    double A,B,C1,C2,d1,d2,l,d;
    double a1,a2,b1,b2,c1,c2,x,y;
    while(scanf("%lf%lf%lf%lf",&a1,&a2,&b1,&b2)!=EOF)
    {scanf("%lf%lf%lf",&c1,&c2,&l);
    A=b2-a2;
    B=a1-b1;
    C1=a1*(a2-b2)-a2*(a1-b1);//Ax+By=C1表示直線L1方程 
    C2=A*c2-B*c1;//Bx-Ay+C2=0表示過圓心且垂直於直線L1的方程 
    //printf("%.2lf %.2lf %.2lf\n",A,B,C);
    if(A==0&&B==0)
    d1=d2=sqrt((a1-c1)*(a1-c1)+(a2-c2)*(a2-c2));//兩點重合的情況 
    else
    { d=sqrt((a1-c1)*(a1-c1)+(a2-c2)*(a2-c2));
      d2=sqrt((b1-c1)*(b1-c1)+(b2-c2)*(b2-c2));
    y=(A*C2-B*C1)/(A*A+B*B);//(x,y)表示L1於L2的交點 
    x=(A*C1+B*C2)/(A*A+B*B)*(-1.0);
    if(is_online(a1,a2,b1,b2,x,y))
    d1=fabs(A*c1+B*c2+C1)/(sqrt(A*A+B*B));//點到直線的共識 
    else
    d1=(d<d2? d:d2);
    d2=(d2>d? d2:d);
    }
    //printf("%.2lf %.2lf",x,y);
    if(d2<=l)
    d2=0;
    else
    d2=d2-l; 
    if(d1<=l)
    d1=0;
    else
    d1=d1-l;
    printf("%.2lf\n%.2lf\n",d1,d2);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章