Poj 2540 hotter colder cut Polygon

Source: Internet
Author: User
/* Poj 2540 hotter colder cut a polygon using a vertical line of two points to cut the polygon. Based on the cold and hot data, determine the half and then output the area */# include <stdio. h> # include <math. h> const double EPS = 1e-8; const int n = 200; struct point {Double X, Y; point () {} Point (double A, double B ): X (A), y (B) {}} Dian [N]; point Jiao [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> Y inline bool mo_ll (Double X, Double Y) {return x <Y-EPS;} // x <y inline bool mo_ge (Double X, Double Y) {return x> Y-EPS;} // x> = y inline bool mo_le (Double X, Double Y) {return x <Y + EPS ;} // x <= y inline double mo_xmult (point P2, point P0, Point P1) // P1 returns a negative value on the left of P2 and a positive {return (p1.x-forwarded X) on the Right) * (p2.y-p0.y)-(p2.x-p0.x) * (p1.y-p0.y);} Point mo_intersection (point u1, point U2, point V1, Po Int 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 ;} ////////////// // method vector point mo_getfaxian (point xiang) {point; if (mo_ee (Xiang. x, 0) {. X = 1;. y = 0; return a;} else if (mo_ee (Xiang. y, 0) {. X = 0;. y = 1; return a;} else {. X = 1;. y =-1.0 * Xiang. x/x IANG. y; return a ;}/// returns 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]);} If (Ret <0) ret =-ret; return ret/2;} 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 main () {point Qian (0, 0); point cur, mid, end; char order [20]; int flag = 0; Jiao [0] = point (0, 0); Jiao [1] = point (10, 0 ); jiao [2] = point (10, 1 0); Jiao [3] = point (0, 10); int jiaodian = 4; while (scanf ("% lf", & cur. X )! = EOF) {scanf ("% lf % s", & cur. y, order); getchar (); If (Order [0] = 's' | flag = 1) {flag = 1; printf ("0.00 \ n"); continue;} mid. X = (cur. X + Qian. x)/2; mid. y = (cur. Y + Qian. y)/2; end = mo_getfaxian (point (cur. x-qian.x, cur. y-qian.y); end. X = mid. X + end. x; end. y = mid. Y + end. y; bool Zai = mo_gg (mo_xmult (end, cur, mid), 0); If (Order [0] = 'H' & Zai) | (Order [0] = 'C '&&(! Zai) {} else {point TEM = end; end = mid; Mid = TEM;} mo_banjiao_cut (Jiao, mid, end, jiaodian); double area = mo_area_polygon (Jiao, jiaodian); printf ("%. 2lf \ n ", area); Qian = cur;} 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.