POJ 3525

/*
在一个多边形内切最大的圆,可以知道,当一个多形放大或缩小时,内切圆也随比例放大或缩小。
于是,在多边形各边向内压缩R时,最大圆半径必定减少R。那么,直至为半平面交空集时,得到R即为最大半径。

下面,想解释一下 坐标转换代码。坐标转换可以通过解方程求得。
设转换后为x,y.按多边形顺时针顺序排,有:
(x-x2)^2+(y-y2)^2=r^2;
(x-x2)*(x1-x2)+(y-y2)*(y1-y2)=0;
得:
(x-x2)^2=r^2*(y1-y2)^2/((x1-x2)^2+(y1-y2)^2);
此处最难确定的便 是符号的问题,而因为向内压缩,恰好有如代码的变换:
        tt.x = pts[i+1].y - pts[i].y;  //坐标转换代码 
        tt.y = pts[i].x - pts[i+1].x;  
        double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);  
        tt.x = tt.x * k;  
        tt.y = tt.y * k;  
        ta.x = pts[i].x + tt.x;  
        ta.y = pts[i].y + tt.y;  
        tb.x = pts[i+1].x + tt.x;  
        tb.y = pts[i+1].y + tt.y;
*/

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

using namespace std;
const double eps=1e-8;
const int MAXN=150;
struct point {
	double x,y;
};
point pts[MAXN],p[MAXN],q[MAXN];
int n,cCnt,curCnt;

int DB(double d){
    if(d>eps) return 1;
    if(d<-eps) return -1;
    return 0;
}
  
void initial(){
    for(int i=1;i<=n;i++)
    p[i]=pts[i];
    p[n+1]=p[1];
    p[0]=p[n];
    cCnt=n;
}
  
void getline(point x,point y,double &a,double &b,double &c){
    a = y.y - x.y;
    b = x.x - y.x;
    c = y.x * x.y - x.x * y.y;
}
  
point intersect(point x,point y,double a,double b,double c){
    double u = fabs(a * x.x + b * x.y + c);
    double v = fabs(a * y.x + b * y.y + c);
    point pt;
    pt.x=(x.x * v + y.x * u) / (u + v);
    pt.y=(x.y * v + y.y * u) / (u + v);
    return  pt;
}
  
void cut(double a,double b,double c){
    curCnt=0;
    for(int i=1;i<=cCnt;i++){
        if(DB(a*p[i].x+b*p[i].y+c) >=0) q[++curCnt] = p[i];
        else {
            if(DB(a*p[i-1].x + b*p[i-1].y + c )>0){
                q[++curCnt] = intersect(p[i],p[i-1],a,b,c);
            }
             if(DB(a*p[i+1].x + b*p[i+1].y + c )>0){
                q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
            }
        }
    }
    for(int i = 1; i <= curCnt; ++i)p[i] = q[i];
    p[curCnt+1] = q[1];p[0] = p[curCnt];
    cCnt = curCnt;
}

bool slove(double r){
    initial();
    for(int i=1;i<=n;i++){
    	point ta, tb, tt;  
        tt.x = pts[i+1].y - pts[i].y;  //坐标转换代码 
        tt.y = pts[i].x - pts[i+1].x;  
        double k = r / sqrt(tt.x * tt.x + tt.y * tt.y);  
        tt.x = tt.x * k;  
        tt.y = tt.y * k;  
        ta.x = pts[i].x + tt.x;  
        ta.y = pts[i].y + tt.y;  
        tb.x = pts[i+1].x + tt.x;  
        tb.y = pts[i+1].y + tt.y; 
        double a,b,c;
        getline(ta,tb,a,b,c);
        cut(a,b,c);
    }
    if(cCnt>=1) return true;
    return false;
}

int main(){
	while(scanf("%d",&n),n){
		for(int i=1;i<=n;i++)
		scanf("%lf%lf",&pts[i].x,&pts[i].y);
		for(int i=1;i<(n+1)/2;i++)
		swap(pts[i],pts[n-i]);
		pts[n+1]=pts[1];
		double r,mid;
		double h=1e10,l=0;
		while(l+eps<=h){
			mid=(l+h)/2;
			if(slove(mid)){
				r=mid; l=mid;
			}
			else h=mid;
		}
		printf("%.6lf\n",r);
	}
	return 0;
}

  

posted @ 2014-08-07 13:42  chenjunjie1994  阅读(156)  评论(0编辑  收藏  举报