POJ - 3384 Feng Shui(半平面交)

链接 Feng Shui

题意

将两个半径为 r r r 的圆放入一个多边形中,两个圆占据最大面积时圆心坐标是多少;

思路

在多边形中放圆,最大圆的圆心一定在多边形内核中;

将多边形的每条边都内推 r r r,然后半平面交算内推后的多边形内核,选取内核上相距最远的两个点即可;

AC代码

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>

#define x first
#define y second

using namespace std;

const double eps = 1e-9;
const int N = 10010;
typedef pair<double, double> PDD;
const double pi = 3.1415926535898;

int T;
int n, cnt;
PDD p[N];

struct Line
{
    PDD st, ed;
} line[N];
PDD normal[N];
PDD ans[N];
Line lline[N];


PDD operator -(PDD a, PDD b)
{
    return make_pair(a.x - b.x, a.y - b.y);
}

PDD operator *(PDD a, double u)
{
    return make_pair(a.x * u, a.y * u);
}

PDD operator +(PDD a, PDD b)
{
    return make_pair(a.x + b.x, a.y + b.y);
}

int sign(double x)
{
    if (fabs(x) < eps) return 0;
    if (x < 0) return -1;
    return 1;
}

int dcmp(double x, double y)
{
    if (fabs(x - y) < eps) return 0;
    if (x < y) return -1;
    return 1;
}

double cross(PDD a, PDD b)
{
    return a.x * b.y - a.y * b.x;
}

double area(PDD a, PDD b, PDD c)
{
    return cross(b - a, c - a);
}

PDD get_intersect(PDD p, PDD v, PDD q, PDD w)
{
    PDD u = p - q;
    double t = cross(w, u) / cross(v, w);
    return make_pair(p.x + v.x * t, p.y + v.y * t);
}

double point_product(PDD a, PDD b)
{
    return a.x * b.x + a.y * b.y;
}

double get_len(PDD a, PDD b)
{
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    return sqrt(dx * dx + dy * dy);
}

double get_angle(Line a)
{
    return atan2(a.ed.y - a.st.y, a.ed.x - a.st.x);
}

bool cmp(Line a, Line b)
{
    double A = get_angle(a);
    double B = get_angle(b);

    if (!dcmp(A, B)) return area(a.st, a.ed, b.st) < 0;

    return A < B;
}

PDD get_intersect(Line a, Line b)
{
    return get_intersect(a.st, a.ed - a.st, b.st, b.ed - b.st);
}

bool on_right(Line a, Line b, Line c)
{
    PDD o = get_intersect(b, c);
    return sign(area(a.st, a.ed, o)) < 0;
}

void half_plane_intersection()
{
    int q[N];
    sort(lline, lline + cnt, cmp);

    int hh = 0, tt = -1;
    for (int i = 0; i < cnt; i ++)
    {
        if (i && !dcmp(get_angle(lline[i]), get_angle(lline[i - 1]))) continue;

        while (hh + 1 <= tt && on_right(lline[i], lline[q[tt]], lline[q[tt - 1]])) tt --;
        while (hh + 1 <= tt && on_right(lline[i], lline[q[hh]], lline[q[hh + 1]])) hh ++;

        q[++ tt] = i;
    }

    while (hh + 1 <= tt && on_right(lline[q[hh]], lline[q[tt]], lline[q[tt - 1]])) tt --;
    while (hh + 1 <= tt && on_right(lline[q[tt]], lline[q[hh]], lline[q[hh + 1]])) hh ++;

    q[++ tt] = q[hh];
    int k = 0;
    for (int i = hh; i < tt; i ++) ans[k ++] = get_intersect(lline[q[i]], lline[q[i + 1]]);
    int idx = 0, idy = 1;
    for (int i = 0; i < k; i ++)
    {
         for (int j = i + 1; j < k; j ++)
         {
            if (get_len(ans[i], ans[j]) > get_len(ans[idx], ans[idy]))
            {
                idx = i, idy = j;
            }
        }
    }

    printf("%.10lf %.10lf %.10lf %.10lf\n", ans[idx].x, ans[idx].y, ans[idy].x, ans[idy].y);

}

double len(PDD a)
{
    return sqrt(a.x * a.x + a.y * a.y);
}

PDD Normal(PDD a)
{
    double x = len(a);
    return make_pair(-a.y / x, a.x / x);
}


int main()
{
    double r;
    while (~scanf("%d %lf", &n, &r))
    {   
        cnt = 0;
        for (int i = 0; i < n; i ++) cin >> p[i].x >> p[i].y;
        for (int i = 0; i < n / 2; i ++) swap(p[i], p[n - i - 1]);
        for (int i = 0; i < n; i ++)
        {
            normal[cnt] = Normal(p[(i + 1) % n] - p[i]);//向量的向左垂直方向的单位向量
            line[cnt].st = p[i];
            line[cnt ++].ed =  p[(i + 1) % n];
        }

        for (int i = 0; i < cnt; i ++)
        {
            lline[i].st = line[i].st + normal[i] * r;
            lline[i].ed = line[i].ed + normal[i] * r;
        }

        half_plane_intersection();

    }   
    
}

——END

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半碗无糖蓝莓冻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值