Poj 3130 How I Mathematician Wonder What You Are! -Check whether the polygon has cores.

Source: Internet
Author: User

/* Poj 3130 How I Mathematician Wonder What You Are! -Check whether the polygon has cores */# include <stdio. h> # include <math. h> const double eps = 1e-8; const int N = 103; struct point {double x, y;} dian [N]; inline bool mo_ee (double x, double y) {double ret = x-y; if (ret <0) ret =-ret; if (ret <eps) return 1; return 0;} inline bool mo_gg (double x, double y) {return x> y + eps;} // x> yinline bool mo_ll (double x, double y) {return x <y-eps ;} // x <yinline bool mo_ge (double x, double y) {return x> y-eps;} // x> = yinline bool mo_le (double x, double y) {return x <y + eps;} // x <= yinline double mo_xmult (point p2, point p0, point p1) // p1 returns a negative value on the left of p2, return the positive {return (p1.x-p0.x) * (p2.y-p0.y)-(p2.x-p0.x) * (p1.y-p0.y);} point mo_intersection (point u1, point u2, point v1, point v2) {point ret = u1; double t = (u1.x-v1.x) * (v1.y-v2.y)-(u1.y-v1.y) * (v1.x-v2.x )) /(u1.x-u2.x) * (v1.y-v2.y)-(u1.y-u2.y) * (v1.x-v2.x); ret. x + = (u2.x-u1.x) * t; ret. y + = (u2.y-u1.y) * t; return ret ;} /////////////// // obtain the semi-plane intersection point mo_banjiao_jiao [N * 2] by the cutting method. point mo_banjiao_jiao_temp [N * 2]; void mo_banjiao_cut (point * ans, point qian, point hou, int & nofdian) {int I, k; for (I = k = 0; I <nofdian; ++ I) {double a, B; a = mo_xmult (hou, ans [I], qian); B = mo_xmult (hou, ans [(I + 1) % nofdian], qian); if (mo_ge (a, 0 )) // clockwise <= 0 {mo_banjiao_jiao_temp [k ++] = ans [I];} if (mo_ll (a * B, 0 )) {mo_banjiao_jiao_temp [k ++] = mo_intersection (qian, hou, ans [I], ans [(I + 1) % nofdian]) ;}} for (I = 0; I <k; ++ I) {ans [I] = mo_banjiao_jiao_temp [I];} nofdian = k;} int mo_banjiao (point * dian, int n) {int I, nofdian; nofdian = n; for (I = 0; I <n; ++ I) {mo_banjiao_jiao [I] = dian [I];} for (I = 0; I <n; ++ I) // I starts from 0 {mo_banjiao_cut (mo_banjiao_jiao, dian [I], dian [(I + 1) % n], nofdian ); if (nofdian = 0) {return nofdian ;} /// // int main () {int t, I, n; while (scanf ("% d", & n), n) {for (I = 0; I <n; ++ I) {scanf ("% lf ", & dian [I]. x, & dian [I]. y);} int ret = mo_banjiao (dian, n); if (ret = 0) {printf ("0 \ n ");} else {printf ("1 \ n") ;}} return 0 ;}
/* Why ret <3? */# Include <stdio. h> # include <math. h ># include <algorithm> using namespace std; const double eps = 1e-8; struct point {double x, y;} dian [20000 + 10]; point jiao [203]; struct line {point s, e; double angle;} xian [20000 + 10]; int n, yong; bool mo_ee (double x, double y) {double ret = x-y; if (ret <0) ret =-ret; if (ret <eps) return 1; return 0;} bool mo_gg (double x, double y) {return x> y + eps;} // x> y bool mo_ll (double x, double y) {return x <y-eps ;} // x <y bool mo_ge (double x, double y) {return x> y-eps;} // x> = y bool mo_le (double x, double y) {return x <y + eps;} // x <= y point mo_intersection (point u1, point u2, point v1, point v2) {point ret = u1; double t = (u1.x-v1.x) * (v1.y-v2.y)-(u1.y-v1.y) * (v1.x-v2.x)/(u1.x-u2.x) * (v1.y-v2.y)-(u1.y-u2.y) * (v1.x-v2.x); ret. x + = (u2.x-u1.x) * t; ret. y + = (u2.y-u1.y) * t; return ret;} double mo_xmult (point p2, point p0, point p1) // p1 returns a negative value on the left of p2, return positive {return (p1.x-p0.x) * (p2.y-p0.y)-(p2.x-p0.x) * (p1.y-p0.y);} void mo_HPI_addl (point a, point B) on the right) {xian [yong]. s = a; xian [yong]. e = B; xian [yong]. angle = atan2 (B. y-a.y, B. x-a.x); yong ++;} // half plane bool mo_HPI_cmp (const line & a, const line & B) {if (mo_ee (. angle, B. angle) {return mo_gg (mo_xmult (B. e,. s, B. s), 0);} else {return mo_ll (. angle, B. angle) ;}} int mo_HPI_dq [20000 + 10]; bool mo_HPI_isout (line cur, line top, line top_1) {point jiao = mo_intersection (top. s, top. e, top_1.s, top_1.e); return mo_ll (mo_xmult (cur. e, jiao, cur. s), 0); // If the clockwise direction is mo_gg} int mo_HalfPlaneIntersect (line * xian, int n, point * jiao) {int I, j, ret = 0; sort (xian, xian + n, mo_HPI_cmp); for (I = 0, j = 0; I <n; I ++) {if (mo_gg (xian [I]. angle, xian [j]. angle) {xian [++ j] = xian [I] ;}} n = j + 1; mo_HPI_dq [0] = 0; mo_HPI_dq [1] = 1; int top = 1, bot = 0; for (I = 2; I <n; I ++) {while (top> bot & mo_HPI_isout (xian [I], xian [mo_HPI_dq [top], xian [mo_HPI_dq [top-1]) top --; while (top> bot & mo_HPI_isout (xian [I], xian [mo_HPI_dq [bot], xian [mo_HPI_dq [bot + 1]) bot ++; mo_HPI_dq [++ top] = I; // current half-plane inbound stack} while (top> bot & mo_HPI_isout (xian [mo_HPI_dq [bot], xian [mo_HPI_dq [top], xian [mo_HPI_dq [top-1]) top --; while (top> bot & mo_HPI_isout (xian [mo_HPI_dq [top], xian [mo_HPI_dq [bot], xian [mo_HPI_dq [bot + 1]) bot ++; mo_HPI_dq [++ top] = mo_HPI_dq [bot]; for (ret = 0, I = bot; I <top; I ++, ret ++) {jiao [ret] = mo_intersection (xian [mo_HPI_dq [I + 1]. s, xian [mo_HPI_dq [I + 1]. e, xian [mo_HPI_dq [I]. s, xian [mo_HPI_dq [I]. e);} return ret;} int main () {int I; while (scanf ("% d", & n), n) {yong = 0; for (I = 0; I <n; ++ I) {scanf ("% lf", & dian [I]. x, & dian [I]. y) ;}for (I = 0; I <n; ++ I) {mo_HPI_addl (dian [I], dian [(I + 1) % n]);} int ret = mo_HalfPlaneIntersect (xian, n, jiao); if (ret <3) {printf ("0 \ n ");} else {printf ("1 \ n") ;}} return 0 ;}

 

Related Article

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.