Poj 3335 rotating scoreboard)

Source: Internet
Author: User

Question Link

Question: Give You A polygon and ask if such a point exists inside the polygon so that it can see any point on the polygon boundary.

Idea: returns the polygon kernel through semi-plane intersection.

Semi-flat Materials

Polygon Kernel Algorithm

What is the polygon kernel?

The core of a simple polygon is a point set inside the polygon. Any point in the set is connected to a point on the boundary of the polygon within the polygon. It is a collection of camera locations that can be monitored in all places by placing a camera head in a house, that is, the polygon core.

 

For example, the first graph has a kernel, such as the black dot, and the second graph does not have a kernel. No matter where it is, there is always a region that cannot be seen.

 

So how can we obtain this kernel interval? The general algorithm is to use two straight lines to continuously cut the polygon and cut to the end, that is, the kernel interval.

We all know that a straight line can cut the plane into two regions. Assume that the linear equation is

Ax + by + c = 0, then the two planes can be expressed as AX + by + C> = 0 and AX + by + C <0

 

How can we use a program to cut a polygon in a straight line?

The process is as follows:

1. Store the original polygon point set in a clockwise or counterclockwise order.

2. Take two consecutive points in order to form a straight line. Use this line to cut the original polygon.

First, let me assume that the point is stored clockwise,

In this case, the point set of a polygon is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

 

Take point 1 and form a straight line AX + by + c = 0 with point 2. At this time, the point in the vertex concentration is brought into the equation AX + by + C at one time, the obtained values are greater than or equal to 0, indicating that all vertices are on the same side of the straight line, and the point set remains unchanged.

 

Take points 2 and 3 to form a straight line. Similarly, the points in the vertex set are sequentially brought into the equation AX + by + C. At this time, the results of the 4 and 5 points are less than 0, the value of other points is still greater than or equal to 0. At this time, the points 4 and 5 are cut out of the polygon. Therefore, only {1, 2, 3, 6, 7, 8, 9, 10 are left, x}, (x is the intersection of line 23 and line 56)

 

And so on, always execute to point 10 and point 1, then the kernel set will be obtained.

 

It is worth noting that the figures in this example are special, all at right angles. If the figures are casual, when a point is determined to be out of the polygon range, we should also consider whether there is any intersection between the straight line formed by the two adjacent points and the ax + by + C. If there is an intersection, these intersections should be added to the updated vertex set, for example, after a straight line composed of vertices 2 and vertices 3 is executed in the example, the point set is {1, 2, 3, 6, 7, 8, x}, where 3 and X are the result.

 

Also, why is it enough to execute all vertices one by one and construct a new point set from the remaining vertices? The answer is that the point is clockwise or clockwise ~~~

1 # include <stdio. h> 2 # include <string. h> 3 # include <iostream> 4 # include <math. h> 5 6 using namespace STD; 7 8 struct node 9 {10 double X; 11 Double Y; 12} p [110], temp [110], newp [110]; // P is each point of the first polygon, temp is each point of the polygon temporarily stored in the intermediate process, and newp is each point of the cut polygon 13 int N, newn; // original points, points after cutting 14 double A, B, C; // three coefficients of the linear equation 15 16 void Getline (node X, node y) // obtain the linear equation AX + by + c = 017 {18 A = y. y-x.y; 19 B = x. x-y.x; 20 C = y. X * X. y-y. y * X. x; 21} 22 node intersect (node X, node y) // calculates the intersection of a straight line determined by X and Y and AX + by + c = 0. 23 {24 double U = A * X. X + B * X. Y + C; 25 Double V = A * Y. X + B * Y. Y + C; 26 node T; 27 T. X = (X. x * V + Y. x * u)/(U + V); // y. y-x.y = u + V; Y. y-t.y = V; Y. y-x.y = u; 28 t. y = (X. y * V + Y. y * u)/(U + V); 29 return t; 30} 31 void cut () 32 {33 int cutn = 0; 34 for (INT I = 1; I <= newn; I ++) 35 {36 IF (A * newp [I]. X + B * newp [I]. Y + C> = 0) // all vertices are greater than 0, indicating that all vertices are on the other side of this line. One side, so don't cut 37 temp [++ cutn] = newp [I]; 38 else39 {40 if (A * newp [I-1]. X + B * newp [I-1]. Y + C> 0) 41 temp [++ cutn] = intersect (newp [I-1], newp [I]); // Add the new intersection to 42 if (A * newp [I + 1]. X + B * newp [I + 1]. Y + C> 0) 43 temp [++ cutn] = intersect (newp [I + 1], newp [I]); 44} 45} 46 for (INT I = 1; I <= cutn; I ++) 47 newp [I] = temp [I]; 48 newp [cutn + 1] = temp [1]; // The precursor and successor 49 newp [0] = temp [cutn]; 50 newn = cutn; 51} 52 53 void solv E () 54 {55 for (INT I = 1; I <= N; I ++) 56 {57 newp [I] = P [I]; 58} 59 p [n + 1] = P [1]; 60 newp [n + 1] = newp [1]; 61 newp [0] = newp [N]; 62 newn = N; 63 for (INT I = 1; I <= N; I ++) 64 {65 Getline (P [I], p [I + 1]); // traverse two adjacent points sequentially from the beginning. 66 cut (); 67} 68 69} 70 int main () 71 {72 int t; 73 scanf ("% d", & T); 74 while (t --) 75 {76 scanf ("% d", & N); 77 for (INT I = 1; I <= N; I ++) 78 scanf ("% lf", & P [I]. x, & P [I]. y); 79 solve (); 80 If (newn = 0) puts ("no"); 81 else puts ("yes"); 82} 83 return 0; 84}
View code

 

Poj 3335 rotating scoreboard)

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.