半平面交uvaliva3890

来源:互联网 发布:生物数据分析软件 编辑:程序博客网 时间:2024/04/28 17:56
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;struct pointt{double x, y;pointt(double x, double y) :x(x), y(y){}pointt(){}};pointt operator+(pointt a, pointt b){return pointt(a.x + b.x, b.y + a.y);}pointt operator-(pointt a, pointt b){return pointt(a.x - b.x, a.y - b.y);}pointt operator*(pointt a, double t){return pointt(a.x*t, a.y*t);}double operator*(pointt a, pointt b){return a.x*b.x + a.y*b.y;}double dot(pointt a, pointt b){return a.x*b.y - a.y*b.x;}double length(pointt a){return sqrt(a*a);}struct linee{pointt p1, vec,verti;double arc;linee(pointt a, pointt b) :p1(a), vec(b - a){arc = atan2(vec.y, vec.x);double l = length(vec);verti.x = -vec.y / l; verti.y = vec.x / l;}linee(){}bool operator<(linee b){return arc < b.arc;}};bool isonleft(pointt a, linee b){pointt c = a - b.p1;return dot(c, b.vec) <= 0;}pointt inter(linee a, linee b){pointt c = a.p1 - b.p1;if (fabs(dot(b.vec, a.vec)) < 1e-10)return pointt(1000000000, 1000000000);double t = dot(c, b.vec) / dot(b.vec, a.vec);return a.p1 + a.vec*t;}linee realline[2000];linee line[2000];pointt point[2000];int halfmat(linee *l, int n){sort(l, l + n);int first = 0;int  last = 0;realline[first] = l[0];for (int i=1; i < n; i++){while ((last - first)>0 && !isonleft(point[last - 1], l[i]))last--;while ((last - first) > 0 && !isonleft(point[first], l[i]))first++;if (fabs(dot(l[i].vec, realline[last].vec)) < 1e-10)continue;realline[++last] = l[i];if (last - first > 0)point[last - 1] = inter(realline[last], realline[last - 1]);}while ((last - first) > 0 && !isonleft(point[last - 1], realline[first]))last--;//if ((last - first) > 0)point[last] = inter(l[first], l[last]);if ((last - first)<=1)return 0;elsereturn 1;}pointt temp[2000];int main(){int n;while (scanf("%d", &n) && n != 0){for (int i = 0; i < n; i++){scanf("%lf%lf", &temp[i].x, &temp[i].y);if (i>0)line[i-1] = linee(temp[i - 1], temp[i]);}line[n - 1] = linee(temp[n - 1], temp[0]);double left = 0; double right = 20000; double mid;linee templine[2000];while ((right - left) > 1e-10){//cout << right << endl; mid = (right + left) / 2;for (int i = 0; i < n; i++){templine[i] = line[i];templine[i].p1 = templine[i].p1 + templine[i].verti*mid;}int kind = halfmat(templine, n);if (kind)left = mid;elseright = mid;}printf("%.6lf\n", right);}}
终于写过一次了2333333!!以前都wa的啊QAQ半平面交的数据真的是不好构造啊!!!!!
原创粉丝点击