码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 1450

时间:2014-08-02 01:30:52      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   for   ar   div   amp   log   

最小圆覆盖

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>

using namespace std;
const double eps=0.00000001;
struct point {
	double x,y;
}p[110];

struct circle{
	point cent;
	double rad;
}cir;
int n;

double TriangleArea(point t1,point t2,point t3){
	point p1,p2;
	p1.x=t1.x-t3.x; p1.y=t1.y-t3.y;
	p2.x=t2.x-t3.x; p2.y=t2.y-t3.y;
	return fabs(p1.x*p2.y-p2.x*p1.y)/2;
}

double dist(point p1,point p2){
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

void TriangleCircle(point ta,point tb,point tc){
	double a=dist(ta,tb);
	double b=dist(tb,tc);
	double c=dist(tc,ta);
	cir.rad=(a*b*c)/TriangleArea(ta,tb,tc)/4;
	double xa=ta.x; double ya=ta.y;
	double xb=tb.x; double yb=tb.y;
	double xc=tc.x; double yc=tc.y;
	double c1=(xa*xa+ya*ya-xb*xb-yb*yb)/2;
	double c2=(xa*xa+ya*ya-xc*xc-yc*yc)/2;
	cir.cent.x=(c1*(ya-yc)-c2*(ya-yb))/((xa-xb)*(ya-yc)-(xa-xc)*(ya-yb));
	cir.cent.y=(c1*(xa-xc)-c2*(xa-xb))/((ya-yb)*(xa-xc)-(ya-yc)*(xa-xb));
}

void slove(){
	random_shuffle(p,p+n);
	cir.cent=p[0]; cir.rad=0;
	for(int i=1;i<n;i++){
		if(dist(p[i],cir.cent)-eps>cir.rad	){
			cir.cent=p[i]; cir.rad=0;
			for(int j=0;j<i;j++){
				if(dist(p[j],cir.cent)-eps>cir.rad	){
					cir.cent.x=(p[j].x+p[i].x)/2;
					cir.cent.y=(p[i].y+p[j].y)/2;
					cir.rad=dist(p[j],p[i])/2;
					for(int k=0;k<j;k++){
						if(dist(p[k],cir.cent)-eps>cir.rad	){
							TriangleCircle(p[i],p[j],p[k]);
						}
					}
				}
			}
		}
	}
}

int main(){
	while(scanf("%d",&n)!=EOF){
		if(n==0) break;
		for(int i=0;i<n;i++){
			scanf("%lf%lf",&p[i].x,&p[i].y);
		}
		slove();
		printf("%0.2f %0.2f %0.2f\n",cir.cent.x,cir.cent.y,cir.rad);
	}
	return 0;
}

  

ZOJ 1450,布布扣,bubuko.com

ZOJ 1450

标签:blog   os   io   for   ar   div   amp   log   

原文地址:http://www.cnblogs.com/jie-dcai/p/3885931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!