Reprinted please indicate the source, thank youHttp://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove
Question: Starting from to a certain point, it will tell you whether the target point is closer or farther. Each step is taken to find the possible range area of the target.
Http://poj.org/problem? Id = 2540
Two points are given, which are closer to each other, that is, the area is separated by a vertical line, so that you can determine the scope of the solution.
First, let's get the vertical line equation according to the two points. yy, but my approach may be more traditional. Then we need to consider the case where the slope does not exist.
With the equation, pay attention to the plus and minus of the inequality, that is, the direction of the region.
Add a new half plane to cut the original convex polygon.
NOTE: If same is displayed, it indicates that the target is on the center vertical line, and the question requires an area. Therefore, the answer is 0. All subsequent outputs should be 0.
When the area is 0, it indicates that no feasible area can be found in the previous group, and all subsequent answers should be 0.
Poj, zoj, and fzu all have this question, and the data is somewhat different. You can try it all.
# Include <iostream> # include <fstream> # include <iomanip> # include <cstdio> # include <cstring> # include <algorithm> # include <cstdlib> # include <cmath> # include <set> # include <map> # include <queue> # include <stack> # include <string> # include <vector> # include <sstream> # include <cassert> # define ll long # define EPS 1e-8 # define INF 10000 # define zero () FABS (a) <EPS # define n 20005 using namespace STD; struct point {Double X, Y; PO INT () {} Point (double Tx, double ty) {x = TX; y = ty ;}} pre, cur, P [105], TP [105]; double xmul (point P0, Point P1, point P2) {return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p1.y-Snapshot y) * (p2.x-Snapshot X );} // two points are given to calculate the standard void get_midperpendicular (point P1, point P2, double & A, double & B, double & C) of the center vertical line) {// The Center vertical line slope is 0 if (zero (p1.x-p2.x) A = 0, B = 1; // The Center vertical line slope does not exist else if (zero (p1.y-p2.y )) a = 1, B = 0; // generally, else B = p2.y-p1.y, A = p2.x-p1.x; C =-A * (p2.x + p1.x)/2- B * (p2.y + p1.y)/2;} Point get_intersection (point P1, point P2, double A, double B, double C) {double U = FABS (A * p1.x + B * p1.y + C), V = FABS (A * p2.x + B * p2.y + C ); return Point (p1.x * V + p2.x * u)/(U + V), (p1.y * V + p2.y * u)/(U + V ));} void cut (double A, double B, double C, point P [], Int & CNT) {int TMP = 0; For (INT I = 1; I <= CNT; I ++) {If (A * P [I]. X + B * P [I]. Y + C>-EPS) TP [+ + TMP] = P [I]; else {If (A * P [I-1]. X + B * P [I-1]. Y + C> EPS) TP [++ TMP] = get_intersec Tion (P [I-1], p [I], a, B, c); If (A * P [I + 1]. X + B * P [I + 1]. Y + C> EPS) TP [++ TMP] = get_intersection (P [I + 1], p [I], a, B, c );}} for (INT I = 1; I <= TMP; I ++) P [I] = TP [I]; P [0] = TP [TMP]; P [TMP + 1] = P [1]; CNT = TMP;} double get_area (point P [], int N) {double area = 0; for (INT I = 2; I <n; I ++) area + = xmul (P [1], p [I], p [I + 1]); return FABS (area)/2.0;} int main () {pre. X = pre. y = 0; char STR [10]; P [1]. X = 0; P [1]. y = 0; P [2]. X = 0; P [2]. y = 10; P [3]. X = 10; P [3]. Y = 10; P [4]. X = 10; P [4]. y = 0; P [0] = P [4]; P [5] = P [1]; int CNT = 4; double region = 1.0; while (scanf ("% lf % s", & cur. x, & cur. y, STR )! = EOF) {double A, B, C; get_midperpendicular (PRE, cur, A, B, C); // determine the inequality, make the region range AX + by + C> 0 if (strcmp (STR, "colder") = 0) {If (A * pre. X + B * pre. Y + C <-EPS) {A =-A; B =-B; C =-C ;}} else if (strcmp (STR, "hotter") = 0) {If (A * cur. X + B * cur. Y + C <-EPS) {A =-A; B =-B; C =-C ;}} else area = 0; // If same is displayed or the previous area is 0, the subsequent values are 0 if (zero (area) {printf ("0.00 \ n"); continue;} Cut (, b, C, p, CNT); Area = get_area (p, CNT); printf ("%. 2f \ n ", area); Pre = cur;} return 0 ;}