简单几何 CF Gym 100338B Geometry Problem:

题目连接:http://codeforces.com/gym/100338    

题目描述:

Problem B. Geometry Problem
Input file: geometry.in
Output file: geometry.out
Time limit: 1 second
Memory limit: 64 megabytes
Peter is studying in the third grade of elementary school. His teacher of geometry often gives him d
home tasks.
At the last lesson the students were studying circles. They learned how to draw circles with com
Peter has completed most of his homework and now he needs to solve the following problem. He i
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
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
ends of the first segment. The second line contains x21, y21, x22, y22 and describes the second segm
the same way.
Input is followed by two lines each of which contains four zeroes these lines must not be processe
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
circle. All numbers in the output file must not exceed 1010 by their absolute values. The jury ma
comparisons of real numbers with the precision of 104.
Example
geometry.in
0 0 0 4
1 0 1 4
0 0 0 0
0 0 0 0
0.5 0 2

geometry.out
0.5 0 2

题目大意:平面上有两条线段,求一个圆,与两条直线各有一个交点(不包括端点),输出一组可行解的圆心位置与半径。

解法:找到不在同一线段上最近的两个端点,以该两点为直径,求出圆心位置,半径就是最短距离/2 + 1e-3.

代码:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
long double dis[4];
long double disc(double x,double y,double x3,double y3)
{
    long double r=sqrt((x-x3)*(x-x3) + (y-y3) * (y - y3));
    return r;
}
long double xc,yc,r;

double x11[2];
double y11[2];
double x22[2],y22[2];
int main()
{
    freopen("geometry.in","r",stdin);
    freopen("geometry.out","w",stdout);

    while(1)
    {
        cin>>x11[0]>>y11[0]>>x11[1]>>y11[1];
        cin>>x22[0]>>y22[0]>>x22[1]>>y22[1];
        if(x11[0]==0&&y11[0]==0&&x11[1]==0&&y11[1]==0&&x22[0]==0&&y22[0]==0&&x22[1]==0&&y22[1]==0)break;
        dis[0]=disc(x11[0],y11[0],x22[0],y22[0]);
        dis[1]=disc(x11[0],y11[0],x22[1],y22[1]);
        dis[2]=disc(x11[1],y11[1],x22[0],y22[0]);
        dis[3]=disc(x11[1],y11[1],x22[1],y22[1]);
        long double minx=990000000;
        int u=-1;
        for(int i=0;i<=3;i++)
        {
            //cout<<dis[i]<<endl;
            if(minx>dis[i]){minx=dis[i];u=i;}
        }
        if(u==0){xc=(x11[0]+x22[0])/2;yc=(y11[0]+y22[0])/2;}
        else if(u==1){xc=(x11[0]+x22[1])/2;yc=(y11[0]+y22[1])/2;}
        else if(u==2){xc=(x11[1]+x22[0])/2;yc=(y11[1]+y22[0])/2;}
        else if(u==3){xc=(x11[1]+x22[1])/2;yc=(y11[1]+y22[1])/2;}

        cout<<xc<<" "<<yc<<" "<<dis[u]/2+0.001<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值