链接 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();
}
}