CodeForces 613A Peter and Snow Blowe

一道基础计算几何,关键是求点到线段的最短距离,注意旋转点保证不在多边形内部

#include 
#include 
#include 
using namespace std;
const double pi = acos(-1);

struct P
{
    double x, y;
    P() {}
    P(int _x, int _y) {x = _x; y = _y;}
}p, t[100005];
P operator - (P a, P b) {return P(a.x - b.x, a.y - b.y);}
double dis(P a, P b) {return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));}
double dot(P a, P b) {return a.x * b.x + a.y * b.y;}
double cross(P a, P b) {return a.x * b.y - a.y * b.x;}

double get_min(P a, P b)
{
    if(dot(b - a, p - a) < 0) return dis(a, p);
    if(dot(a - b, p - b) < 0) return dis(b, p);
    return fabs(cross(b - a, p - a)) / dis(a, b);
}

int main()
{
    int n;
    scanf("%d%lf%lf", &n, &p.x, &p.y);
    for(int i = 0; i < n; i++)
        scanf("%lf%lf", &t[i].x, &t[i].y);
    t[n] = t[0];

    double Max = 0, Min = 1e18;

    for(int i = 0; i < n; i++)
        Max = max(Max, dis(p, t[i]));

    for(int i = 0; i < n; i++)
        Min = min(Min, get_min(t[i], t[i + 1]));

    printf("%.8f", pi * Max * Max - pi * Min * Min);

    return 0;
}


你可能感兴趣的:(CodeForces 613A Peter and Snow Blowe)