hdu 6097 Mindis(几何)

来源:互联网 发布:无限网络万能钥匙 编辑:程序博客网 时间:2024/06/06 03:47

因为PO = QO, 所以直接将PQ平移到平行于x轴的位置,这样比较好建立椭圆方程。

为什么会联系到椭圆呢,因为椭圆上任意一点到两焦点的距离等于定值,所以建立以PQ为焦点的椭圆,再求椭圆与圆相交时,最小的a,就求出答案了。

这里只知道c,需要二分一下b,b的范围就是r-h,h是圆心到PQ中点的距离。

http://www.cnblogs.com/chen9510/p/7341215.html#commentform

参考这篇博客的代码,读入挂好评。

代码:

#include <iostream>#include <stdio.h>#include <math.h>#include <algorithm>using namespace std;const double eps = 1e-10;double dis(double x1,double y1,double x2,double y2 ){    return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));}namespace IO {    const int MX = 4e7; //1e7占用内存11000kb    char buf[MX]; int c, sz;    void begin() {        c = 0;        sz = fread(buf, 1, MX, stdin);    }    inline bool read(int &t) {        while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;        if(c >= sz) return false;        bool flag = 0; if(buf[c] == '-') flag = 1, c++;        for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';        if(flag) t = -t;        return true;    }}int main(){//    IO::begin();    int t, i, j;    double x1, y1, x2, y2, r;    cin>>t;    while(t--)    {        int R, X1, Y1, X2, Y2;        scanf("%lf%lf%lf%lf%lf", &r, &x1, &y1, &x2, &y2);        double ll=0.0, x3=(x2+x1)*0.5, y3=(y2+y1)*0.5;        double h=dis(x3, y3, 0, 0), a, b, c;        double mid, rr=r-h, delt, A, B, C;        c=dis(x1, y1, x2, y2)*0.5;        c=c*c;        double ans1, ans2;        int ok;        for(int i=0; i<40; i++)        {            mid=(ll+rr)*0.5;            b=mid*mid;            a=b+c;            A=a-b;            B=-2.0*a*h;            C=b*r*r+a*h*h-a*b;            delt=(B*B-4.0*A*C);            ok=0;            if(delt>=-eps)            {                ans1=(-B+sqrt(B*B-4.0*A*C))*0.5/A;                ans2=(-B-sqrt(B*B-4.0*A*C))*0.5/A;                ans1=ans1*ans1;                ans2=ans2*ans2;                if(r*r-ans1>=-eps || r*r-ans2>=-eps)ok=1;            }            if(ok)            {                rr=mid;            }            else ll=mid;        }        printf("%.10f\n", sqrt(a)*2.0);    }    return 0;}


原创粉丝点击