POJ 3384 Feng Shui (半平面交,内切圆,题目给的样例有问题)

题目大意:
顺时针给出凸多边形的一些点,以及给出圆的半径。
求在这个多边形内能放的两个圆的最大面积的圆心点,要求圆在多边形内,可以重叠。

思路:内切圆的圆心都在凸多边形的核内

所以先将所有的边向内平移r的距离,然后在处理后的凸多边形的核里找到最远点对。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

const double eps=1e-8;
const double inf=1e9;
const int MAXN=210;
int m;//保存多边形的点数
int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
struct point
{
    double x,y;
};

point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点
void getline(point x,point y,double &a,double &b,double   &c) //两点x、y确定一条直线a、b、c为其系数
{
    a = y.y - x.y;
    b = x.x - y.x;
    c = y.x * x.y - x.x * y.y;
}
void initial()
{
    for(int i = 1; i <= m; ++i)p[i] = points[i];
    p[m+1] = p[1];
    p[0] = p[m];
    cCnt = m;
}
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(a*p[i].x + b*p[i].y + c >= 0)q[++curCnt] = p[i];
        else
        {
            if(a*p[i-1].x + b*p[i-1].y + c > 0)
            {
                q[++curCnt] = intersect(p[i],p[i-1],a,b,c);
            }
            if(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;
}
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
void solve(double r)
{
    initial();
    for(int i = 1; i <= m; ++i)
    {
        point ta, tb, tt;
        tt.x = points[i+1].y - points[i].y;
        tt.y = points[i].x - points[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 = points[i].x + tt.x;
        ta.y = points[i].y + tt.y;
        tb.x = points[i+1].x + tt.x;
        tb.y = points[i+1].y + tt.y;
        double a,b,c;
        getline(ta,tb,a,b,c);
        cut(a,b,c);
    }
}
void GuiZhengHua()  //规整化方向,逆时针变顺时针,顺时针变逆时针
{
    for(int i = 1; i < (m+1)/2; i ++)
        swap(points[i], points[m-i]);
}
double Length(point a,point b)
{
    double x = a.x-b.x;
    double y = a.y-b.y;
    return sqrt(x*x+y*y);
}
int main()
{
    double r;
    while(scanf("%d%lf",&m, &r)!=EOF)
    {
        for(int i = 1; i<=m; i++) scanf("%lf%lf", &points[i].x, &points[i].y);
        points[m+1]=points[1];
        solve(r);
        double maxn = -1;
        point a, b;
        for(int i =1; i<=cCnt; i++)
        {
            for(int j = 1; j<=cCnt; j++)
            {
                if(Length(p[i],p[j])>maxn)
                {
                    maxn = Length(p[i],p[j]);
                    a = p[i];
                    b = p[j];
                }
            }
        }
        printf("%.4f %.4f %.4f %.4f\n", a.x, a.y, b.x, b.y);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值