poj-1279 Art Gallery

Source: Internet
Author: User

Test instructions

A polygon with n points is given, and the area of the polygon nucleus is obtained.

The core of the polygon is the set of points inside the polygon so that the points can see any vertex and edge of the polygon;

n<=1500;


Exercises

This should also be regarded as a kind of template problem;

The method of finding the polygon core is now O (NLOGN);

Sort all the lines by the polar angle (the atan2 () function in the math library) and then linearly in the double-ended queue;

In this way, the side set of the half plane intersection is obtained, and the point set is also obtained by passing.

For the area of the word will be directly cross-product sweep once, just fine;

But this problem is not the same place is not said that the vertex by what order given;

So you can first clockwise side, if the half-plane intersection no solution and then counterclockwise, there is no solution that polygon does not exist nuclear, the answer is 0;

The semi-planar intersection is no solution is the boundary set is less than three;

In order to save me two times direct sticky code = =, can write some functions to shorten the code length;


Code:


#include <math.h> #include <stdio.h> #include <string.h> #include <algorithm> #define N 2000using namespace Std;const double Eps=1e-8;const double inf=1e100;struct point{double x, y; Point () {}point (double _,double __): X (_), Y (__) {}void read () {scanf ("%lf%lf", &x,&y);} Friend point operator + (point A,point b) {return point (A.X+B.X,A.Y+B.Y);} Friend point operator-(point A,point b) {return point (A.X-B.X,A.Y-B.Y);} Friend double operator * (Point A,point b) {return a.x*b.x+a.y*b.y;} Friend double operator ^ (Point A,point B) {return a.x*b.y-a.y*b.x;} Friend Point operator * (double A,point b) {return point (A*B.X,A*B.Y);}} Poi[n],p[n];struct Line{point p,v;double Alpha; Line () {}; Line (Point _,point __) {p=_,v=__-_,alpha=atan2 (v.y,v.x);} friend bool operator < (line A,line b) {return a.alpha<b.alpha;} Friend Point Getp (line A,line b) {point u=a.p-b.p;double temp= (b.v^u)/(A.V^B.V); return a.p+temp*a.v;}} L[n],q[n];int St,en;bool Onleft (line l,point a)//strict Left {return (l.v^ (A-L.P)) >eps;} Void HPI (int n) {sort (l+1,l+n+1); q[st=en=1]=l[1];for (int i=2;i<=n;i++) {while (st<en&&! Onleft (L[i],p[en]) en--;while (st<en&&! Onleft (l[i],p[st+1]) st++;if (Fabs (q[en].alpha-l[i].alpha) >eps) q[++en]=l[i];elseq[en]=onleft (L[I],Q[EN].P)? q [En]:l[i];p [En]=getp (Q[en-1],q[en]);} while (st<en&&! Onleft (Q[st],p[en])) en--;} int main () {int c,t,n,m,i,j,k;double ans;scanf ("%d", &t), for (c=1;c<=t;c++) {scanf ("%d", &n), and for (i=1;i<= n;i++) Poi[i].read (); for (i=2;i<=n;i++) {l[i]=line (poi[i],poi[i-1]);} L[1]=line (Poi[1],poi[n]); HPI (n);p [St]=getp (Q[st],q[en]); for (i=st,ans=0;i<en;i++) {ans+=p[i]^p[i+1];} Ans+=p[en]^p[st];if (ans<0) ans=-ans;if (en-st+1<3) {for (i=2;i<=n;i++) {l[i]=line (poi[i-1],poi[i]);} L[1]=line (poi[n],poi[1]); HPI (n);p [St]=getp (Q[st],q[en]); for (i=st,ans=0;i<en;i++) {ans+=p[i]^p[i+1];} Ans+=p[en]^p[st];if (ans<0) ans=-ans;if (en-st+1<3) ans=0;} printf ("%.2lf\n", Ans/2+eps);} return 0;}



poj-1279 Art Gallery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.