#include<bits/stdc++.h> using namespace std; const int M = 1e5+10 ; const double pi = acos(-1.0) ; int n ; double sx , sy ;//源点 double X[M] , Y[M] ; double minn = 1e18 , maxn = -1 ; double dist (int id) { return sqrt((X[id]-sx)*(X[id]-sx)+(Y[id]-sy)*(Y[id]-sy));} double segdist (int id1 , int id2) {//源点到多边形上两点id1,id2的最短距离 double a = Y[id2] - Y[id1] ; double b = X[id1] - X[id2] ; double c = X[id2]*Y[id1]-Y[id2]*X[id1] ; if ((X[id1]-X[id2])*(sx-X[id2])+(Y[id1]-Y[id2])*(sy-Y[id2]) < 0) return 1e18 ;//如果垂足落在线段外,返回无穷 if ((X[id2]-X[id1])*(sx-X[id1])+(Y[id2]-Y[id1])*(sy-Y[id1]) < 0) return 1e18;//利用点积钝角<0 return fabs(c+sy*b+sx*a) / sqrt(a*a+b*b) ; //利用叉积和面积法 } int main () { cin >> n >> sx >> sy ; for (int i = 0 ; i < n ; i ++) { scanf ("%lf%lf" , &X[i],&Y[i]) ; } for (int i = 0 ; i < n ; i ++) { maxn = max (maxn , dist(i)) ; minn = min (minn , dist(i)) ;//垂足落在线段外时,源点到线段的最短距离 minn = min (minn , segdist(i,(i+1)%n)) ; } cout.precision(12) ; cout << pi*(maxn*maxn-minn*minn) << endl ; return 0 ; }
时间: 2024-10-10 17:26:04