#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <time.h>
#include <set>
#include <list>
#include <iostream>
using namespace std;
#define ll long long
#define ull unsigned long long
#define cls(x) memset(x,0,sizeof(x))
#define clslow(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
const int maxn=1e7+1e6;
const double eps = 1e-8;
const double PI = acos(-1.0);
int dcmp(double x){//判断误差在eps范围内
if( x > eps ) return 1;
return x < -eps ? -1 : 0;
}
struct Point{
double x,y;
Point(){
x = y = 0;
}
Point(double a,double b){//给点赋值
x = a;y = b;
}
inline void input(){
scanf("%lf%lf",&x,&y);//输入点的座标
}
inline Point operator-(const Point &b)const{//求点a、b构成的向量
return Point(x - b.x,y - b.y);
}
inline Point operator+(const Point &b)const{
return Point(x + b.x,y + b.y);
}
inline Point operator*(const double &b)const{
return Point(x * b,y * b);
}
inline double dot(const Point &b)const{//点乘
return x * b.x + y * b.y;
}
inline double cross(const Point &b,const Point &c)const{//多边形一边上的两点与圆心形成的向量叉乘
return (b.x - x) * (c.y - y) - (c.x - x) * (b.y - y);
}
inline double Dis(const Point &b)const{//求圆心与点之间的距离
return sqrt((*this-b).dot(*this-b));
}
inline bool InLine(const Point &b,const Point &c)const{ //三点共线
return !dcmp(cross(b,c));
}
inline bool OnSeg(const Point &b,const Point &c)const{ //点在线段上,包括端点
return InLine(b,c) && (*this - c).dot(*this - b) < eps;
}
};
inline double min(double a,double b){
return a < b ? a : b;
}
inline double max(double a,double b){
return a > b ? a : b;
}
inline double Sqr(double x){
return x * x;
}
inline double Sqr(const Point &p){
return p.dot(p);
}
Point LineCross(const Point &a,const Point &b,const Point &c,const Point &d){
double u = a.cross(b,c) , v = b.cross(a,d);
return Point((c.x * v + d.x * u) / (u + v) , (c.y * v + d.y * u) / (u + v));
}
double LineCrossCircle(const Point &a,const Point &b,const Point &r,
double R,Point &p1,Point & p2){
Point fp = LineCross(r , Point(r.x+a.y-b.y , r.y+b.x-a.x) , a , b);
double rtol = r.Dis(fp);
double rtos = fp.OnSeg(a , b) ? rtol : min(r.Dis(a) , r.Dis(b));
double atob = a.Dis(b);
double fptoe = sqrt(R * R - rtol * rtol) / atob;
if( rtos > R - eps ) return rtos;
p1 = fp + (a - b) * fptoe;
p2 = fp + (b - a) * fptoe;
return rtos;
}
double SectorArea(const Point &r,const Point &a,const Point &b,double R){ //不大于180度扇形面积,r->a->b逆时针
double A2 = Sqr(r - a) , B2 = Sqr(r - b) , C2 = Sqr(a - b);
return R * R * acos( (A2 + B2 - C2) * 0.5 / sqrt(A2) / sqrt(B2)) * 0.5;
}
double TACIA(const Point &r,const Point &a,const Point &b,double R){
double adis = r.Dis(a) , bdis = r.Dis(b);
if( adis < R + eps && bdis < R + eps )//两点,也就是该边在圆内
return r.cross(a , b) * 0.5;
Point ta , tb;
if( r.InLine(a,b) ) return 0.0;//三点共线,则
double rtos = LineCrossCircle(a, b, r, R, ta, tb);
if( rtos > R - eps )
return SectorArea(r, a, b, R);
if( adis < R + eps )
return r.cross(a, tb) * 0.5 + SectorArea(r, tb, b, R);
if( bdis < R + eps )
return r.cross(ta, b) * 0.5 + SectorArea(r, a, ta, R);
return r.cross(ta, tb) * 0.5 + SectorArea(r, tb, b, R) + SectorArea(r, a, ta, R);
}
const int MAXN= 15;
Point p[MAXN];
double SPICA(int n,Point r,double R){
int i;
double ret = 0 , if_clock_t;
for( i = 0 ; i < n ; ++i ){
if_clock_t = dcmp(r.cross(p[i], p[(i + 1) % n]));//如果if_clock_t<0说明两个向量叉乘小于0,两个向量所成夹角为钝角,
//这两个向量与p[i]、p[i+1]的边形成的三角形在圆和多边形外。
if( if_clock_t < 0 )
ret -= TACIA(r, p[(i + 1) % n], p[i], R);
else ret += TACIA(r, p[i], p[(i + 1) % n], R);
}
return fabs(ret);
}
int main (){
// freopen("in.txt","r",stdin);
int n;
double r;
scanf("%d%lf",&n,&r);
Point st(0,0),np;
for(int i=0;i<n;i++){
p[i].input();
st.x+=p[i].x;
st.y+=p[i].y;
}
st.x/=n;st.y/=n;
double T=200,best=SPICA(n,st,r),d=0.999;
srand(time(NULL));
while(T>eps){
double RD=T*(2*rand()-RAND_MAX);
np.x=st.x+RD;
RD=T*(2*rand()-RAND_MAX);
np.y=st.y+RD;
double ans=SPICA(n,np,r);
double dE=ans-best;
if(dE>=0){
st=np;
best=ans;
}
T*=d;
}
printf("%.8lf\n",best);
return 0;
}
gym 101158J - Cover the Polygon with Your Disk [模拟退火+多边形与圆面积交]
最新推荐文章于 2019-09-27 13:11:03 发布