Poj 3384 feng shui-returns the inner side of a polygon from the left side of the polygon and returns the semi-plane intersection.

Source: Internet
Author: User
/* Poj 3384 feng shui-returns the inner side of a polygon to the left side and returns the semi-plane intersection. Then, the farthest point is obtained. */# include <stdio. h> # include <math. h ># include <algorithm> using namespace STD; const double EPS = 1e-8; struct point {Double X, Y; point () {} Point (double A, double B ): X (A), y (B) {}} Dian [400 + 10]; point Jiao [400 + 10]; struct line {point S, E; double angle ;} xian [200 + 10]; int N, Yong; Double R; bool mo_ee (Double X, Double Y) {double ret = x-y; If (Ret <0) ret =-ret; I F (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 Lin E & 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 [400 + 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; so RT (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 semi-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;} inline double mo_distance (point P1, point P2) {return SQRT (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);} // returns the point mo_getfaxian (point xiang) {point A; 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/Xiang. y; return a ;}/// the line se is pushed to a certain distance from line Mo _ Line_tui (point S, point E, double tui) {Line ret; point fa = mo_getfaxian (point (e. x-s.x, E. y-s.y); If (mo_ll (mo_xmult (E, point (s. X + FA. x, S. Y + FA. y), S), 0) {fa. X =-fa. x; fa. y =-fa. y;} double Len = SQRT (Fa. x * Fa. X + FA. y * Fa. y); Len = Len/Tui; fa. X = fa. x/Len; fa. y = fa. y/Len; S. X = S. X + FA. x, S. y = S. Y + FA. y; E. X = E. X + FA. x, E. y = E. Y + FA. y; ret. S = s; ret. E = E; return ret;} int main () {int I, j; while (scanf ("% d", & N )! = EOF) {Yong = 0; scanf ("% lf", & R); for (I = 0; I <n; ++ I) {scanf ("% lf", & Dian [I]. x, & Dian [I]. y) ;}for (I = 0; I <n; ++ I) {line LL = mo_line_tui (Dian [(I + 1) % N], Dian [I], r); mo_hpi_addl (LL. s, ll. e);} int ret = mo_halfplaneintersect (Xian, N, Jiao); double min =-1, min2; int A, B; for (I = 0; I <ret; ++ I) {for (j = I + 1; j <ret; ++ J) {min2 = mo_distance (Jiao [I], Jiao [J]); if (Min <0 | min2> min) {min = min2; A = I; B = J ;}} printf ("%. 4lf %. 4lf %. 4lf %. 4lf \ n ", Jiao [A]. x, Jiao [A]. y, Jiao [B]. x, Jiao [B]. y);} 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.