Polygon core template

Source: Internet
Author: User

Xie Yong drew a picture and then solved the problem...

It is essentially a problem of "semi-plane interaction.

Straight cut polygon. The common part is the polygon core.

Here we find a good template:
Http://blog.csdn.net/accry/article/details/6070621

Http://blog.csdn.net/candy20094369/article/details/6703940

 

 

# Include <iostream> # include <cstdio> # include <cmath> # include <vector> # include <cstring> # include <algorithm> # include <string> # include <set> # include <ctime> # define Cl (ARR, val) memset (ARR, Val, sizeof (ARR) # define rep (I, n) for (I) = 0; (I) <(N ); ++ (I) # define for (I, L, H) for (I) = (l); (I) <= (h ); ++ (I) # define Ford (I, H, L) for (I) = (h); (I)> = (l); -- (I )) # define L (x) <1 # define R (x) <1 | 1 # define mid (L, R) (L + r)> 1 # define min (x, y) x <Y? X: y # define max (x, y) x <Y? Y: X # define e (x) (1 <(x) const double EPS = 1e-8; typedef long ll; using namespace STD; const int maxn = 110; struct point {Double X; Double Y; point (double A = 0, double B = 0): X (A), y (B) {} void input () {scanf ("% lf", & X, & Y) ;}; point [maxn], p [maxn], Q [maxn]; // The vertex (clockwise) of the read polygon, p is the array of the polygon vertices that are finally cut, and the vertex int ccnt, N of the saved core; // at this time, ccnt is the number of vertices for the final cut polygon and the number of saved vertices. inline int dbcmp (Double X) {// If (x> EPS) return 1; else if (x <-EPS) Return-1; return 0;} inline void Getline (point X, point y, double & A, double & B, double & C) {// point X, Y determines a straight line A = y. y-X. y; B = x. x-y. x; C = y. x * X. y-X. x * Y. y;} inline point intersect (point X, point y, double A, double B, double C) {// calculate the intersection of A, B, C, and a straight line formed by X and Y. double U = FABS (A * X. X + B * X. Y + C); Double V = FABS (A * Y. X + B * Y. Y + C); Return Point (X. x * V + Y. x * u)/(U + V), (X. y * V + Y. y * u)/(U + V);} inline void cut (double A, double B, double C) {// As shown in, cut int cur = 0, I; for (I = 1; I <= ccnt; ++ I) {If (dbcmp (A * P [I]. X + B * P [I]. Y + C)> = 0) Q [++ cur] = P [I]; // C may be too small due to precision problems, so some points should have been on the right, not on else {If (dbcmp (A * P [I-1]. X + B * P [I-1]. Y + C)> 0) // If P [I-1] is on the right of the line, // P [I], the intersection of a straight line formed by P [I-1] with a known straight line acts as a vertex of the core (in this way, the area of the core may be reduced due to precision issues) Q [++ cur] = intersect (P [I], p [I-1], a, B, c); If (dbcmp (A * P [I + 1]. X + B * P [I + 1]. Y + C)> 0) Q [++ cur] = intersect (P [I], p [I + 1], a, B, c );}} for (I = 1; I <= cur; ++ I) P [I] = Q [I]; P [cur + 1] = Q [1]; P [0] = P [cur]; ccnt = cur;} void solve () {// note: the default point is clockwise. if the subject is not clockwise, the normalization direction is int I; for (I = 1; I <= N; ++ I) {double A, B, C; Getline (point [I], point [I + 1],, b, c); cut (A, B, C);} If (ccnt = 0) puts ("no"); else puts ("yes ");} void Init () {int I; for (I, 1, n) point [I]. input (); point [n + 1] = point [1]; // initialize P [], ccnt for (I, 1, n) P [I] = point [I]; P [n + 1] = P [1]; P [0] = P [N]; ccnt = N ;} int main () {// freopen ("data. in "," r ", stdin); int t; scanf (" % d ", & T); While (t --) {scanf (" % d ", & N); Init (); solve ();} return 0 ;}

 

 

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.