首页 > 代码库 > acdream 1414 Geometry Problem

acdream 1414 Geometry Problem

Geometry Problem

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)     Special Judge
SubmitStatus

Problem Description

      Peter is studying in the third grade of elementary school. His teacher of geometry often gives him difficult home tasks.
      At the last lesson the students were studying circles. They learned how to draw circles with compasses. Peter has completed most of his homework and now he needs to solve the following problem. He is given two segments. He needs to draw a circle which intersects interior of each segment exactly once.
      The circle must intersect the interior of each segment, just touching or passing through the end of the segment is not satisfactory.
      Help Peter to complete his homework.
 

Input

      The input file contains several test cases. Each test case consists of two lines.
      The first line of the test case contains four integer numbers x11, y11, x12, y12— the coordinates of the ends of the first segment. The second line contains x21. y21, x22, y22 and describes the second segment in the same way.
      Input is followed by two lines each of which contains four zeroes these lines must not be processed.
      All coordinates do not exceed 102 by absolute value.

Output

      For each test case output three real numbers — the coordinates of the center and the radius of the circle. All numbers in the output file must not exceed 1010 by their absolute values. The jury makes all comparisons of real numbers with the precision of 10-4.

Sample Input

0 0 0 4
1 0 1 4
0 0 0 0
0 0 0 0

Sample Output

0.5 0 2

Hint



题解及代码:

      这道题目的做法 应该挺多的吧,我的解法不知道是不是对的,但是能AC(个人认为是对的)。

     首先我们从两条直线各取一个点,要求:两点的距离最短,之后我们把这两个点的中点作为圆的圆心,把圆心到两点的距离求出来记为r,然后求出圆心到另外两直线端点的距离r1,r2,半径R=(r+min(r1,r2))/2.0,就行了。


代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

struct Point
{
    double x,y;
    Point(){}
    Point(double X,double Y):x(X),y(Y) {}
}t;

struct Line
{
    Point l,r;
}a,b;

double dis(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

void init()
{
    double Dis[5];

    Dis[1]=dis(a.l,b.l);
    Dis[2]=dis(a.l,b.r);
    Dis[3]=dis(a.r,b.l);
    Dis[4]=dis(a.r,b.r);

    int p=1;
    for(int i=2;i<=4;i++)
    {
        if(Dis[i]<Dis[p]) p=i;
    }

    if(p==2)
    {
        t=b.r;b.r=b.l;b.l=t;
    }
    if(p==3)
    {
        t=a.l;a.l=a.r;a.r=t;
    }
    if(p==4)
    {
        t=a.l;a.l=a.r;a.r=t;
        t=b.r;b.r=b.l;b.l=t;
    }
}

int main()
{
    while(scanf("%lf%lf%lf%lf",&a.l.x,&a.l.y,&a.r.x,&a.r.y))
    {
        scanf("%lf%lf%lf%lf",&b.l.x,&b.l.y,&b.r.x,&b.r.y);

        if(!a.l.x&&!a.l.y&&!a.r.x&&!a.r.y&&!b.l.x&&!b.l.y&&!b.r.x&&!b.r.y)
            break;

        init();

        double x,y,r;
        x=(a.l.x+b.l.x)/2.0;
        y=(a.l.y+b.l.y)/2.0;
        r=dis(Point(x,y),a.l);

        double r1,r2;
        r1=dis(Point(x,y),a.r);
        r2=dis(Point(x,y),b.r);

        r=r+min(r1,r2);
        r/=2.0;
        printf("%.5lf %.5lf %.5lf\n",x,y,r);
    }
    return 0;
}







acdream 1414 Geometry Problem