ACdream 1414(计算几何)

题目链接:点击打开链接

分析:本题求解两个线段,用一个圆与其相交,与每条线段有且只有一个交点,求解圆的坐标和半径。只需找到四个端点之间的距离最小的时候,求解这两点中点就是圆心,半径为其距离一半多一些;

代码:

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <utility>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;
const double pi=acos(-1);
const int maxn=100005;
const int INF=0x3f3f3f;
const double eps=1e-10;
int dcmp(double x){
    if(fabs(x)<eps)return 0;
    if(x>0)return 1;
    return -1;
}//精度为eps的比较
struct Point{
    double x,y;
}p[maxn],a[maxn],b[maxn];

double dis(Point a,Point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}//两点距离

int main(){
    Point a,b,c,d;
    while(scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y)!=EOF){
        scanf("%lf%lf%lf%lf",&c.x,&c.y,&d.x,&d.y);
        if(a.x==0&&a.y==0&&b.x==0&&b.y==0&&c.x==0&&c.y==0&&d.x==0&&d.y==0)break;
        Point ans1,ans2;
        double ans=INF;
        if(ans>dis(a,c)){
            ans=dis(a,c);
            ans1=a;
            ans2=c;
        }
        if(ans>dis(a,d)){
            ans=dis(a,d);
            ans1=a;
            ans2=d;
        }
        if(ans>dis(b,c)){
            ans=dis(b,c);
            ans1=b;
            ans2=c;
        }
        if(ans>dis(b,d)){
            ans=dis(b,d);
            ans1=b;
            ans2=d;
        }
        Point sum;
        sum.x=(ans1.x+ans2.x)/2;
        sum.y=(ans1.y+ans2.y)/2;
        printf("%.4f %.4f %.4f\n",sum.x,sum.y,ans/2+0.001);
    }
    return 0;
}


你可能感兴趣的:(计算几何)