【CodeForces 613A】Peter and Snow Blower

时间:2023-03-09 20:25:32
【CodeForces 613A】Peter and Snow Blower

题意

给出原点(不是(0,0)那个原点)的坐标和一个多边形的顶点坐标,求多边形绕原点转一圈扫过的面积(每个顶点到原点距离保持不变)。

分析

多边形到原点的最小距离和最大距离构成的两个圆之间的圆环就是所求面积。

判断最大距离一定在顶点上,最小距离可能在点上也可能在边上。

如果原点到一个顶点的连线和它所在的边构成钝角,那么最小距离在点上,否则最小距离就是顶点和该边构成三角形的原点所在的高,可以用海伦公式求三角形面积,然后求高。

不过我用的方法是点到直线的距离公式。

然后π用 acos(-1.0) 表示。

代码

#include <stdio.h>
#include<iostream>
#include <cmath>
#define dd double
using namespace std; int n;
dd far,nea=1e19,t,P=acos(-1.0); struct po
{
double x,y;
} p[]; dd P_P(po a,po b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
} dd P_L(po a,po b,po c)//point c --> line ab
{
dd l1=P_P(a,b);
dd l2=P_P(a,c);
dd l3=P_P(b,c); if(l2*l2 >= (l1*l1+l3*l3))
return l3; if(l3*l3>=(l2*l2+l1*l1))
return l2; dd A=a.y-b.y;
dd B=b.x-a.x;
dd C=a.x*b.y - a.y*b.x;
//line ab: Ax+By+C=0;
dd D=A*c.x + B*c.y + C; return fabs(D*D/(A*A+B*B));
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y); for(int i=;i<=n;i++)
{
if (i<n) nea = min( P_L(p[i], p[i+], p[]), nea);
else nea = min( P_L(p[n], p[], p[]), nea); far=max(far,P_P(p[],p[i]));
}
printf("%.18lf\n",P*(far-nea)); return ;
}