Semi-planar intersection template

Source: Internet
Author: User

The half-plane to cross so tall name, in fact, is a straight line to cut a piece of area.

The half plane is a straight line to divide a plane into two parts, the plane of the side of the line is half plane, so the half plane is represented by a straight line, in fact, this is similar to the high school's linear programming. Linear programming is to give a few straight lines, to find the area of a few straight-line siege, the same way, half-plane is also the case.

The typical example is: Give a polygon (not necessarily convex, it may be concave), let the polygon inside the installation of a camera, whether there is a position can be photographed all the place.

In fact, the problem is that the convex core of the polygon, the convex core is in this convex region can be photographed all the situation. So now the problem is simple, just ask for a convex core on the line. The convex core of the method is actually quite simple

Steps:

1. First you can assume a large plane (here is for good understanding, in fact, when the problem is directly the original polygon on the line) as a square, then he will have four fixed points, the four vertices to join the point to concentrate,

2. Then traverse each edge of the given polygon, so that each edge to cut the large plane, this side can be expressed as a vector, it is a direction, if the clockwise traversal, then as long as the vector to the right of the point and the edge of the point on the left, cut the point on the side, because clockwise rotation, the left side is the outside of So to cut off, if it is the left side of the point, look for his last vertex and the next vertex, if they are inside this vector, it will require the point and just outside the point of the line and the cut vector intersection, and then put it to focus. If not, skip directly

3. Repeat the 2 process until each line is cut to the plane. In the end, if there is no point in the point set, it means that there is no such area, otherwise the point in the point set is all the vertices of that area.

Here are two types of templates, one with a vector, one with a linear equation (in fact, the line is still in the direction),

and POJ buy a gift two questions poj 1474, POJ 3335, POJ 1279

POJ 1474 (vector method)

/************************************************************************* > File Name:poj_1474.cpp > Author:howe_young > Mail: [email protected] > Created time:2015 Year May 03 Sunday 16:05 05 sec ************************************************************************/#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#defineEPS 1e-8using namespacestd;Const intMAXN = -;structpoint{Doublex, y;};structsegment{Point S, e;}; Point P[MAXN], PP[MAXN], POINTS[MAXN];//p to store the newly cut polygon, pp to hold the temporarily cut polygon, points store the most primitive polygonintN, Ctot, Kase;//N is the total number of vertices of the initial polygon, Ctot is the number of vertices of the newly cut polygonintSgnDoublex) {    if(Fabs (x) <EPS)return 0; returnX <0? -1:1;}DoubleX_multi (Point P1, point P2, point P3)//Vector Fork Multiplication{    return(p3.x-p1.x) * (P2.Y-P1.Y)-(p2.x-p1.x) * (P3.Y-p1.y);}BOOLOutside (segment seg, point P)//Determines whether a point is outside of a line segment, strictly outside, excluding boundaries{    returnX_multi (Seg.s, SEG.E, p) <-EPS;}BOOLInside (segment seg, point P)//determine if the point is inside the line, strictly inside, not including the boundary{    returnX_multi (Seg.s, SEG.E, p) >EPS;} Point Get_intersection (Point P1, point P2, point P3, point P4)//get the intersection of P1P2 and p3p4 of two straight lines{    DoubleA1, B1, C1, A2, B2, C2;    Point TMP; A1= (P2.Y-p1.y); B1= (p1.x-p2.x); C1= (p2.x * p1.y-p1.x *p2.y); A2= (P4.Y-p3.y); B2= (p3.x-p4.x); C2= (P3.Y * P4.X-P4.Y *p3.x); Tmp.x= (B1 * C2-B2 * C1)/(B2 * A1-B1 *A2); TMP.Y= (A1 * C2-C1 * A2)/(A2 * B1-A1 *B2); returntmp;}//use segment seg to cut the remaining polygons (actually p-stored polygons)voidCut (segment seg) {intI, tot =0;  for(i =1; I <= Ctot; i++)    {        if(!outside (SEG, p[i]))//If the current point is not outside of this line, including inside and on the boundary, this time does not need to cut, directly add the point to PP insidePp[++tot] =P[i]; Else//If you're outside, look for his two adjacent vertices .        {            if(Inside (SEG, p[i-1]))//find out if its last vertex is inside the polygon, not on the boundaryPp[++tot] = get_intersection (Seg.s, SEG.E, P[i], p[i-1]);//add it to the intersection of the line with PP            if(Inside (SEG, P[i +1]))//similarlyPp[++tot] = get_intersection (Seg.s, SEG.E, P[i], P[i +1]); }} Ctot= tot;//update the number of fixed points for the remaining polygonsPp[tot +1] = pp[1]; pp[0] =Pp[tot]; memcpy (P, pp,sizeof(PP));//copy pp to P}voidinit () { for(inti =1; I <= N; i++) P[i]= Points[i];//The remaining polygons are just beginning, or the entire polygonp[0] =P[n]; P[n+1] = p[1]; Ctot= N;//the first number of vertices equals n}voidSlove () {init ();    Segment tmp; Points[n+1] = points[1];//Important!     for(inti =1; I <= N; i++)//let each edge cut the original polygon{TMP.S=Points[i]; TMP.E= Points[i +1];    Cut (TMP); }    if(Ctot = =0)//If there is no polygon at the end and there is not a single point, then there is no convex nucleus .{printf ("Floor #%d\nsurveillance is impossible.\n\n", ++Kase); }    Elseprintf ("Floor #%d\nsurveillance is possible.\n\n", ++kase);}intMain () { while(~SCANF ("%d", &n) &&N) { for(inti =1; I <= N; i++) scanf ("%LF%LF", &points[i].x, &points[i].y);    Slove (); }    return 0;}
View Code

POJ 1474 (Straight line method)

#include <iostream>#include<algorithm>#include<cmath>#include<stdio.h>using namespacestd;#defineExp 1e-8structpoint{Doublex; Doubley;};intKase;point points[ the];//record the beginning of the polygonPoint q[ the];//temporarily save newly-cut polygonsPoint p[ the];//save new Cut-out polygonsintn,ccnt;//n the original number of points, M is the number of newly cut polygonsDoublea,b,c;voidGetline (point x, point y)//Get straight ax+by+c==0{a= Y.y-x.y; b= x.x-y.x; C= y.x * x.y-x.x *y.y;} Point Get_intersection (Point x,point y)//get the intersection of straight lines ax+by+c==0 and points x and Y connected lines{    DoubleU = fabs (A * x.x + b * x.y +c); Doublev = fabs (A * y.x + b * y.y +c);    Point ans; Ans.x= (x.x * v + y.x * u)/(U +v); Ans.y= (x.y * v + y.y * u)/(U +v); returnans;}voidCut ()//cutting polygons with straight ax+by+c==0{    inttot=0, I;  for(i =1; I <= ccnt; i++)    {        if(A * p[i].x + b * p[i].y + c >=0)//the problem is that the clock is given .{//so a point on the right side of the line, then the value will be greater than or equal to 0, note that the line is a direction, this vector is better understoodQ[++tot] = P[i];//indicates that the point is still inside the cut polygon, leaving it        }        Else        {            if(A * p[i-1].x + b * p[i-1].y + C >0)//the point is not inside the polygon, but it is a straight line from the point adjacent to it{//The intersection of the ax+by+c==0 may be within the newly cut-out polygon,Q[++tot] = get_intersection (p[i-1], p[i]);//so keep the intersection            }            if(A * p[i+1].x + b * p[i+1].y + C >0) {q[++tot] = get_intersection (p[i+1], p[i]); }        }    }     for(i =1; I <= tot; i++) {P[i]=Q[i]; } P[tot+1] = q[1]; p[0] =Q[tot]; Ccnt=tot;}voidsolve () {inti;  for(i =1; I <= N; i++) {P[i]=Points[i]; } points[n+1] = points[1]; P[n+1] = p[1]; p[0] =P[n]; Ccnt=N;  for(i =1; I <= N; i++) {getline (points[i], points[i+1]);//determine the line ax+by+c==0 according to Point[i] and point[i+1]Cut ();//cutting polygons with straight ax+by+c==0    }    if(ccnt = =0) {printf ("Floor #%d\nsurveillance is impossible.\n\n", ++Kase); }    Else{printf ("Floor #%d\nsurveillance is possible.\n\n", ++Kase); }}intMain () {intCas,i;  while(~SCANF ("%d", &n) &&N) { for(i =1; I <= N; i++) {scanf ("%LF%LF", &points[i].x, &points[i].y);    } solve (); }    return 0;}
View Code

Semi-planar intersection template

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.