Algorithm Training Pollution solution (computational geometry)

Source: Internet
Author: User
Tags cos modulus

Problem description as an employee of the water pollution Management Department, you need to monitor pollutants that are intentionally dumped into rivers, lakes and oceans. One of your tasks is to estimate the impact of contaminants on different aquatic ecosystems (coral reefs, spawning grounds, etc.).


The model you are using for calculations is already illustrated in Figure 1. The coastline (the horizontal line in Figure 1) is the x-axis, and the source is at the origin (0, 0). The spread of pollution is semicircular, and polygons represent the affected ecosystems. You need to figure out what area of the ecosystem is contaminated, which is the dark blue part of the map. The input format input file contains only one set of test data.
The first behavior of each set of test data is two integers n (3 <= n <=), R (1 <= r <=), n represents the number of vertices of the polygon, and R indicates the radius of the contaminated area;
Next n rows, each line contains two integers XI ( -1500 <= XI <=), yi (0 <= Yi <=1500), representing the coordinates of each vertex, given in counter-clockwise order;
The data guarantees that the polygon is not self-intersection or touches itself, and no vertices are located on the arc. The output format output polygon is covered by the circle at the origin point and radius R.
The absolute error of the answer must not exceed 10^-3. Sample Input 6 10
-8 2
8 2
8 14
0 14
0 6
-8 14 Sample output 101.576437872 data size and convention there is about 30% data, n = 3,r <= 20;
There is an additional data of about 30%, n <= 10,r <= 100, the coordinate range is not more than 100;
There is an additional data of about 10%, n <= 100,r <= 150, the coordinate range is not more than 250;
There is another approximately 30% data, n <= 100,r <= 1000, the data exists gradient;
For 100% of data, the data range shown in the topic is met.

Exercises

#include <iostream> #include <math.h> #include <cstdio> #include <algorithm> #include < string> #include <queue> #include <cctype> #include <cstring> #include <map>using namespace std;const int n=1e2+5;const int m=n/2;int n,r;    int x[n],y[n];struct p{double x, y;    Double GetLength () {return sqrt (x*x+y*y);    } bool InCircle () {return x*x+y*y<=r*r;    } Double Cross (P &b) {return x*b.y-y*b.x;    }};d ouble Getarea (P &a,p &b) {double Degree=a.cross (b)/a.getlength ()/b.getlength ();    if (degree<-1) degree=-1;    if (degree>1) degree=1;    Degree=asin (degree); return R*R*DEGREE/2;}    Double cal (P &a,p &b) {bool in1 = a.incircle ();    BOOL in2 = B.incircle ();    if (in1&&in2) {return A.cross (b)/2;        }else if (in1!=in2) {P l=a;        P r=b;        P mid;            for (int i=0;i<40;i++) {mid=p{(l.x+r.x)/2, (L.Y+R.Y)/2};           if (Mid.incircle () ==in1) {     L=mid;            }else{R=mid;        }} if (in1) {return A.cross (mid)/2+getarea (MID,B);        }else{return Getarea (A,mid) +mid.cross (b)/2;        }}else{P l=a;        P r=b;        P mid;        P Midr;            for (int i=0;i<40;i++) {mid=p{(l.x+r.x)/2, (L.Y+R.Y)/2};            midr=p{(l.x+r.x)/2+ (r.x-l.x) *0.0001, (L.Y+R.Y)/2+ (R.Y-L.Y) *0.0001};            if (Mid.getlength () <midr.getlength ()) {r=mid;            }else{L=mid;        }} if (Mid.incircle ()) {return cal (A,mid) +cal (mid,b);        }else{return Getarea (A, b);    }}}int Main () {cin>>n>>r;    for (int i=0;i<n;i++) {cin>>x[i]>>y[i];    } X[n]=x[0];    Y[N]=Y[0];    Double ans=0;        for (int i=0;i<n;i++) {P a=p{x[i],y[i]};        P B=p{x[i+1],y[i+1]};    Ans+=cal (A, b);    } printf ("%lf\n", ans); return 0;}

  

Can't read the series ""

Use this question to complement some of the knowledge in the computational geometry

1. The tolerance theorem: To calculate the size of several sets of merge sets, we will first calculate the size of all the individual collections, then subtract the part that intersects all of the two sets, add the parts that intersect all the three sets, subtract the parts that intersect all of the four sets, and so on, and always calculate to the part where all the collections intersect.

2. Four-color theorem: The four-color problem is that "any map with only four colors can make a country with a common border a different color." This means that a map can be labeled in four colors without causing confusion.

3. dot Product:

Point: A (x1,y1), B (x2,y2) vector: Vector ab= (x2-x1, y2-y1) = (x, y);

Modulus of the Vector | ab| = sqrt (x*x+y*y);

The dot product of the vector: the result is x1*x2 + y1*y2. The result of the dot product is a numeric value.

The set meaning of the dot product: We do a vertical line with vector A to vector B, then | A | * COS (A, b) is a projection on vector b, where the dot product is the projection of one vector on another vector multiplied by another. and satisfy the Exchange law

Application: The angle of two vectors can be obtained according to the set meaning, cos (b) = (vector a * vector b)/(| A | * | b |) = X1*x2 + Y1*y2/(| A | * | B |)

4. Cross product:

The cross product of the vector: The result is the result of the x1*y2-x2*y1 cross product is also a vector, is perpendicular to the vector, a, a plane formed, if the three-dimensional coordinate is on the z axis, the result is its modulus.

Direction judgment: Right hand rule, (right hand half grip, thumb vertical upward, four fingers right vector a grip B, thumb direction is the direction of cross product)

The set meaning of the cross product: 1: The result is that A and B form the parallelogram area of the adjacent edge. 2: The result has a positive negative, there is a sin (a, b) and its angle, the angle is greater than 180 ° is negative. 3: Cross product does not meet the Exchange law

Application: (1: Through the results of positive and negative judgment between the two vectors of the cis-counterclockwise relationship if a X B > 0 means a in the clockwise direction of b if a x B < 0 means a in the counterclockwise direction of B if a x b = 0 means a in B collinear, but not sure whether the direction is the same

(2: Judge the zigzag turn, can be converted to judge the 3rd in the first two forming a straight counterclockwise direction, and then determine the turn.

(3: Judge a point on the side of a straight line, the same way as above.)

(4: Determine whether the point is on a line segment, you can use the cross-multiplication first to determine whether collinear, and then judge whether on it.

(5: Judging whether two straight lines want to cross the test) according to the point of judgment in the straight side of the line we can judge the two points on the other side of the line, of course, this is not enough, because we have to draw a picture that can only make a straight line, not line, So we have to make the same judgment on the other line, OK.

5. Convex Bag: Graham Scanning method

6. Pico Formula: (Calculates the area of the polygon)

S = 1/2x ((x1*y2-x2*y1) + ... + (Xk*yk+1-xk+1*yk) + ... + (Xn*y1-x1*yn))

It is important to note that if a series of points are calculated in a counter-clockwise order, then the positive area is calculated, and if it is clockwise, it is a negative area.

Algorithm Training Pollution solution (computational geometry)

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.