Poj 1279 art gallery-calculate the area of the polygon Core

Source: Internet
Author: User
/* Poj 1279 art gallery-calculate the area of the polygon core */# 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);} 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;} // calculate the Polygon Area Double mo_area_polygon (point * Dian, int N) {int I; point yuan; yuan. X = yuan. y = 0; double ret = 0; for (I = 0; I <n; ++ I) {RET + = mo_xmult (Dian [(I + 1) % N], yuan, Dian [I]);} return ret;} int main () {int I, iofcase = 1, t; scanf ("% d", & T ); while (t --) {scanf ("% d", & N); Yong = 0; for (I = 0; I <n; ++ I) {scanf ("% lf", & Dian [I]. x, & Dian [I]. y);} double area = mo_area_polygon (Dian, n); If (area <0) // if it is clockwise {for (I = 0; I <n; ++ I) {mo_hpi_addl (Dian [(I + 1) % N], Dian [I]) ;}} else {for (I = 0; I <n; ++ I) {mo_hpi_addl (Dian [I], Dian [(I + 1) % N]) ;}} int ret = mo_halfplaneintersect (Xian, N, Jiao); Area = mo_area_polygon (Jiao, RET); If (area <0) Area =-area; Area = Area/2; printf ("%. 2lf \ n ", area);} 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.