codeforces613A 点到线段的距离

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liufengwei1/article/details/86577101

在网上找到了一个板子,直接运用了点积知道某两条向量的方向呈钝角,就可以得出是某个端点最近,否则就是直接运用叉积的方法,求垂线的高度。

其实还可以直接求垂足,然后坐标距离运算

#include<bits/stdc++.h>
#define maxl 100010

using namespace std;

const double PI=acos(-1.0);

struct point 
{
	double x,y;
	point(double a=0,double b=0)
	{
		x=a;y=b;
	}
	point operator - (const point &b)const
	{
		return point(x-b.x,y-b.y);
	}
	double operator * (const point &b)const
	{
		return x*b.x+y*b.y;
	}
	double operator ^ (const point &b)const
	{
		return x*b.y-y*b.x;
	}
};
struct line 
{
	point s,e;
	line(point a=point(),point b=point())
	{
		s=a;e=b;
	}
};
int n;
double r,R;
point P;
point a[maxl];

inline void prework()
{
	scanf("%d",&n);
	scanf("%lf%lf",&P.x,&P.y);
	for(int i=1;i<=n;i++)
		scanf("%lf%lf",&a[i].x,&a[i].y);
	a[n+1]=a[1];
}

inline double getlen(point a)
{
	return sqrt(a*a);
}

inline double PointToSegment(point a,point b,point c)
{
	point x=a-b;
	point y=c-b;
	point z=c-a;
	if((x*y)<0) return getlen(x);
	if((y*z)<0) return getlen(z);
	//return fabs((x^y)/getlen(y));
	point v=c-b;
	double t=((a-b)*v)/(v*v);
	point fot=point(b.x+t*v.x,b.y+t*v.y);
	return sqrt((fot.x-a.x)*(fot.x-a.x)+(fot.y-a.y)*(fot.y-a.y));
}

inline void mainwork()
{
	R=0;r=1e18;
	for(int i=1;i<=n;i++)
	{
		R=max(R,getlen(a[i]-P));
		r=min(r,PointToSegment(P,a[i],a[i+1]));
	}
}

inline void print()
{
	double ans=PI*(R*R-r*r);
	printf("%.8f",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/liufengwei1/article/details/86577101