正文
zoj 1450 Minimal Circle 最小覆盖圆
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=450
You are to write a program to find a circle which covers a set of points and has the minimal area. There will be no more than 100 points in one problem.
题意描述:找到一个最小圆能够包含到所有的二维坐标点。
算法分析:最小覆盖圆的做法。
//最小覆盖圆
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
#define exp 1e-10
#define PI 3.141592654
using namespace std;
const int maxn=+;
struct Point
{
double x,y;
Point(double x=,double y=):x(x),y(y){}
}an[maxn],d;//d:最小覆盖圆的圆心坐标
double r;//最小覆盖圆的半径
typedef Point Vector;
Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
Vector operator * (Vector A,double p) {return Vector(A.x*p , A.y*p); }
Vector operator / (Vector A,double p) {return Vector(A.x/p , A.y/p); }
int dcmp(double x)
{
if (fabs(x)<exp) return ;
return x< ? - : ;
}
double cross(Vector A,Vector B)
{
return A.x*B.y-B.x*A.y;
}
double dist(Vector A,Vector B)
{
double x=(A.x-B.x)*(A.x-B.x);
double y=(A.y-B.y)*(A.y-B.y);
return sqrt(x+y);
} void MiniDiscWith2Point(Point p,Point q,int n)
{
d=(p+q)/2.0;
r=dist(p,q)/;
int k;
double c1,c2,t1,t2,t3;
for (k= ;k<=n ;k++)
{
if (dist(d,an[k])<=r) continue;
if (dcmp(cross(p-an[k],q-an[k]))!=)
{
c1=(p.x*p.x+p.y*p.y-q.x*q.x-q.y*q.y)/2.0;
c2=(p.x*p.x+p.y*p.y-an[k].x*an[k].x-an[k].y*an[k].y)/2.0;
d.x=(c1*(p.y-an[k].y)-c2*(p.y-q.y))/((p.x-q.x)*(p.y-an[k].y)-(p.x-an[k].x)*(p.y-q.y));
d.y=(c1*(p.x-an[k].x)-c2*(p.x-q.x))/((p.y-q.y)*(p.x-an[k].x)-(p.y-an[k].y)*(p.x-q.x));
r=dist(d,an[k]);
}
else
{
t1=dist(p,q);
t2=dist(q,an[k]);
t3=dist(p,an[k]);
if (t1>=t2 && t1>=t3)
{
d=(p+q)/2.0;
r=dist(p,q)/2.0;
}
else if (t2>=t1 && t2>=t3)
{
d=(an[k]+q)/2.0;
r=dist(an[k],q)/2.0;
}
else
{
d=(an[k]+p)/2.0;
r=dist(an[k],p)/2.0;
}
}
}
}
void MiniDiscWithPoint(Point p,int n)
{
d=(p+an[])/2.0;
r=dist(p,an[])/2.0;
int j;
for (j= ;j<=n ;j++)
{
if (dist(d,an[j])<=r) continue;
else
{
MiniDiscWith2Point(p,an[j],j-);
}
}
} int main()
{
int n;
while (scanf("%d",&n)!=EOF && n)
{
for (int i= ;i<=n ;i++)
{
scanf("%lf%lf",&an[i].x,&an[i].y);
}
if (n==)
{
printf("%lf %lf\n",an[].x,an[].y);
continue;
}
r=dist(an[],an[])/2.0;
d=(an[]+an[])/2.0;
for (int i= ;i<=n ;i++)
{
if (dist(d,an[i])<=r) continue;
else
MiniDiscWithPoint(an[i],i-);
}
printf("%.2lf %.2lf %.2lf\n",d.x,d.y,r);
}
return ;
}