链接 A Round Peg in a Ground Hole
题意
给出一些点,问可不可以构成凸包,给出一个圆的半径和圆心坐标,问圆是否在凸包内;
思路
- 首先判断是否是凸包
- 判断圆心是否在凸包内
- 判断圆和凸包的关系,用点到直线距离和半径比较
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-8;
const int N = 10010;
typedef pair<double, double> PDD;
const double pi = 3.1415926535898;
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;
}
double cross(PDD a, PDD b)
{
return a.x * b.y - a.y * b.x;
}
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);
}
int n;
double r, rx, ry;
PDD p[N];
bool judge()
{
double ans = 0;
for (int i = 0; i < n; i ++)
{
double res = sign(cross(p[(i + 1) % n] - p[i], p[(i + 2) % n] - p[(i + 1) % n]));
if (!ans) ans = sign(res);
if (ans * sign(res) < 0) return true;
}
return false;
}
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(PDD a, PDD b, PDD c)
{
return acos(point_product(c - a, b - a) / (get_len(a, b) * get_len(a, c)));
}
bool judge_in()
{
double ans = 0;
for (int i = 0; i < n; i ++)
{
double tmp = cross(p[i] - make_pair(rx, ry), p[(i + 1) % n] - make_pair(rx, ry));
if (sign(tmp) < 0)
{
ans -= get_angle(make_pair(rx, ry), p[i], p[(i + 1) % n]);
}
else ans += get_angle(make_pair(rx, ry), p[i], p[(i + 1) % n]);
}
if (sign(ans) == 0) return false;
if (sign(ans + pi) == 0 || sign(ans - pi) == 0)
{
if (sign(r) == 0) return true;
}
if (sign(ans + 2 * pi) == 0 || sign(ans - 2 * pi) == 0) return true;
else
{
if (sign(r) == 0) return true;
}
return false;
}
bool judge_fit()
{
for (int i = 0; i < n; i ++)
{
double ans = fabs(cross(p[i] - make_pair(rx, ry), p[(i + 1) % n] - make_pair(rx, ry)) / get_len(p[i], p[(i + 1) % n]));
if (sign(ans - r) < 0) return false;
}
return true;
}
int main()
{
while (cin >> n)
{
if (n < 3) break;
cin >> r >> rx >> ry;
for (int i = 0; i < n; i ++) cin >> p[i].x >> p[i].y;
if (judge())
{
puts("HOLE IS ILL-FORMED");
}
else
{
bool flag1 = judge_fit(), flag2 = judge_in();
if (flag1 && flag2) puts("PEG WILL FIT");
else puts("PEG WILL NOT FIT");
}
}
}