Poj 3130 how I mathematician wonder what you are! Semi-plane intersection to check whether the polygon kernel exists

Source: Internet
Author: User

Define a polygon called a star polygon. This polygon is a polygon with a kernel. Give some polygon and ask if it is a star polygon.


Idea: solve with semi-plane intersection. PS: The Code of my first polygon kernel is incorrect .. You must check this. This is the problem of the previous code that I found only after reading the senior code. This does not need to be fine-tuned and is an accurate value, never go to the previous article !!!!

Because the connections between all vertices in the kernel and all vertices on the graph cannot be blocked by edges, to satisfy any side, the vertices that satisfy the kernel must be on the left of the straight line where the edge is located, therefore, you can perform semi-plane intersection of the straight lines of the edges that constitute the polygon. Because the kernel of a polygon can also be a vertex. For example:

In this case, the kernel of the entire graph is dot l. Therefore, when determining the cross product in the onleft function, you must determine whether the cross product is greater than or equal to 0. In this way, a vertex can be saved in the final answer of the semi-plane intersection.


Code:

#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 110#define EPS 1e-9#define DCMP(a)(fabs(a) < EPS ? true:false)using namespace std;struct Point{double x,y;Point(double _ = 0.0,double __ = 0.0):x(_),y(__) {}Point operator -(const Point &a)const {return Point(x - a.x,y - a.y);}Point operator +(const Point &a)const {return Point(x + a.x,y + a.y);}Point operator *(double a)const {return Point(x * a,y * a);}void operator += (const Point &a) {x += a.x, y += a.y;}void Read() {scanf("%lf%lf",&x,&y);}}point[MAX],p[MAX],polygon[MAX];struct Line{Point p,v;double alpha;bool operator <(const Line &a)const {return alpha < a.alpha;}Line(Point _,Point __):p(_),v(__) {alpha = atan2(v.y,v.x);}Line() {}}line[MAX],q[MAX];int cases;int points,lines;inline void Initialize();inline void MakeLine(const Point &a,const Point &b);inline double Cross(const Point &a,const Point &b);inline bool OnLeft(const Line &l,const Point &p);inline bool HalfPlaneIntersection();inline Point GetIntersection(const Line &a,const Line &b);int main(){while(scanf("%d",&points),points) {Initialize();for(int i = 1;i <= points; ++i)point[i].Read();for(int i = 1;i < points; ++i)MakeLine(point[i],point[i + 1]);MakeLine(point[points],point[1]);sort(line + 1,line + lines + 1);bool ans = HalfPlaneIntersection();if(ans)puts("1");elseputs("0");}return 0;}inline void Initialize(){lines = 0;}inline void MakeLine(const Point &a,const Point &b){line[++lines] = Line(a,b - a);}inline double Cross(const Point &a,const Point &b){return a.x * b.y - a.y * b.x;}inline bool OnLeft(const Line &l,const Point &p){return Cross(l.v,p - l.p) >= 0;}inline Point GetIntersection(const Line &a,const Line &b){Point u = a.p - b.p;double temp = Cross(b.v,u) / Cross(a.v,b.v);return a.p + a.v * temp;}inline bool HalfPlaneIntersection(){int front = 1,tail = 1;q[1] = line[1];for(int i = 2;i <= lines; ++i) {while(front < tail && !OnLeft(line[i],p[tail - 1]))--tail;while(front < tail && !OnLeft(line[i],p[front]))++front;if(DCMP(Cross(line[i].v,q[tail].v)))q[tail] = OnLeft(q[tail],line[i].p) ? line[i]:q[tail];elseq[++tail] = line[i];if(front < tail)p[tail - 1] = GetIntersection(q[tail],q[tail - 1]);}while(front < tail && !OnLeft(q[front],p[tail - 1]))--tail;if(tail - front <= 1)return false;return tail > front;}


Poj 3130 how I mathematician wonder what you are! Semi-plane intersection to check whether the polygon kernel exists

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.