codeforces613A 点到线段的距离

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

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

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值