凸包模板 三点不共线点圆半径

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct P{
    double x,y;
}p[101],stack[101];
/*
double getradiusby3point(double x1,double y1,double x2,double y2,double x3,double y3)
{
	double x,y;
	double a11,a12,a21,a22,b1,b2;
	double d,d1,d2;
	a11=2*(x3-x2);
	a12=2*(y3-y2);
	a21=2*(x2-x1);
	a22=2*(y2-y1);
	
	b1=x3*x3-x2*x2+y3*y3-y2*y2;
	b2=x2*x2-x1*x1+y2*y2-y1*y1;
	
	d=a11*a22-a12*a21;
	d1=b1*a22-a12*b2;
	d2=a11*b2-b1*a21;
	x=d1/d;
	y=d2/d;
	return (x1-x)*(x1-x)+(y1-y)*(y1-y);
}*/

double Mul(P p1,P p2,P p3) 	//叉乘 
{    
   return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x); 
}
double dis(P a,P b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
int cmp(const void *a,const void *b)
{
    P * c = (P *)a;
    P * d = (P *)b;
    double k = Mul(p[0],*c,*d);
    if(k<0 || (!k && dis(*c,p[0]) > dis(*d,p[0]) ) )
        return 1;
    return -1;
}
void tubao(int n,int &top)
{
    int i;
    top = 2;
    stack[0] = p[0];
    stack[1] = p[1];
    stack[2] = p[2];
    for(i=3;i<=n;i++)
    {
        while(Mul(stack[top-1],stack[top],p[i])<=0 && top>=2)
            top --;
        top ++;
        stack[top] = p[i];
    }
}
double maxx(double a,double b){
	if(a<b) return b;
	else return a;
}
double radius(P a, P b, P c){
 int A=(b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y);
 int B=(a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y);
 int C=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
 double tmp=(double)(A+B-C)/(2*sqrt((double)A)*sqrt((double)B));
 tmp=sqrt(1.0-tmp*tmp);
 return sqrt((double)C)/(2*tmp);
}       
double deal(P a, P b, P c) //求不公线三点子函数 
{
    double ab, bc, ac, A, B, C;
    ab=dis(a, b);
    bc=dis(b, c);
    ac=dis(a, c);
    A=ab*ab;
    B=bc*bc;
    C=ac*ac;
    if(A>=B+C)
        return ab/2;
    else if(B>=A+C)
        return bc/2;
    else if(C>=A+B)
        return ac/2;
    else
    {
        return radius(a, b, c);
    }
}

int main()
{
    int i,top,tar,n;
    double x,y;
    P temp;
    while(scanf("%d",&n),n)
    {
        tar = 0;
        x = y = 0x7FFFFFFF;
        for(i=0;i<n;i++)
        {
            scanf("%lf %lf",&p[i].x,&p[i].y);
            if(p[i].x<x || p[i].x==x && p[i].y<y)
            {
                x = p[i].x;
                y = p[i].y;
                tar = i;
            }
        }
        if(n==1)
            puts("0.50");
        else if(n==2)
            printf("%.2lf\n",dis(p[0],p[1])/2+0.500);
        else
        {
            temp = p[tar];
            p[tar] = p[0];
            p[0] = temp;
            qsort(p+1,n-1,sizeof(p[0]),cmp);
            p[n] = p[0];
            tubao(n,top);
            double l=0,ans=0;
            for(i=0;i<top-2;i++)
            	for(int j=i+1;j<top-1;j++)
            		for(int k=j+1;k<top;k++){
				ans=maxx(deal(stack[i],stack[j],stack[k]),ans);
            		}
            printf("%.2lf\n",ans+0.500);
        }
    }
    return 0;
}

你可能感兴趣的:(凸包模板 三点不共线点圆半径)