标签:
题意:给两个线段,要求找一个圆(输出圆心和半径),使得圆和每个线段的内部都有且只有一个交点。。
解法:枚举两个线段两两个点,这样可以得到4对点,找到距离最近的一对,它们的中点就是圆心,距离一半再加上eps就是半径,这题eps = 1e-4。。
Code
//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const double eps = 1e-4;
inline int sgn(double x){
if(fabs(x) < eps) return 0;
else return x > 0? 1 : -1;
}
struct Point{
double x, y;
Point(){};
Point(double x1, double y1){
x = x1, y = y1;
}
}p[100];
typedef Point Vector;
Vector operator - (Vector a, Vector b){
return Vector(a.x - b.x, a.y - b.y);
}
double Length(Vector v){
return sqrt(v.x * v.x + v.y * v.y);
}
Point getp(){
int x,y;
scanf("%d%d",&x,&y);
return Point(x, y);
}
Point ans;
double d;
void update(Point p1, Point p2){
if(sgn(Length(p1 - p2) - d) < 0){
d = Length(p1 - p2);
ans = Point((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}
}
int main(){
freopen("geometry.in","r",stdin);
freopen("geometry.out","w",stdout);
while(true){
for(int i = 1; i <= 4; i++) p[i] = getp();
bool alz = 1;
for(int i = 1; i <= 4 && alz == 1; i++) if(p[i].x != 0 || p[i].y != 0) alz = 0;
if(alz) break;
d = 1e9;
update(p[1], p[3]);
update(p[2], p[3]);
update(p[1], p[4]);
update(p[2], p[4]);
d = d * 0.5;
d = d + 0.001;
printf("%.10f %.10f %.10f\n",ans.x,ans.y,d);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/uestc_peterpan/article/details/47844581