[Ry] area of the circle and

Source: Internet
Author: User

In short, the area of the circle is a very beautiful algorithm. Although it seems that there is no strong scalability and practicality, it is a good question to train yourself to calculate the geometric code ability.

Starting from LI ShiCircular parallel solution report:

Imagine what methods would a person take if he did not use computer computing?

First, we need to perform some preprocessing. If one circle is completely surrounded by another circle, this circle can be deleted. After deletion, if a circle is an isolated circle and does not overlap any other circle, the area of the circle can be calculated and deleted. The rest of the work is the intersection.

For a circle, some other circles cover a section on its arc, that is, several intervals. Then, the covered parts of all circles are also several intervals. If K intervals are overwritten, K intervals are not covered. These 2 k intervals are separated by 2 k points.

 

We separate these parts with some directed strings. Directed refers to the clockwise direction of the circle,

 

After all the circles are processed in this way, we only look at these connections. What are the discoveries? It is not difficult to find that there must be two lines at any split point! One connection is connected and the other is connected. There are only two connections. This can be done. For example, if three circles pass through the same point, there must be a circle, and the arcs on both sides of the point are covered, that is, this point is not the split point of this circle!

If any split point has two lines, these lines form several polygon. These polygon are the area of the polygon we require. However, some areas in these polygon are negative. You only need to check whether the line of the polygon is clockwise or counterclockwise. The direction is positive and the inverse is negative.

 

 

The code is quite short:

Program EX3; <br/> type real = extended; <br/> const E = 1e-15; <br/> var Cr, CX, Cy, L, R: array [0 .. 300] of real; <br/> cover: array [0 .. 300] of Boolean; <br/> x1, x2, Y1, Y2, T1, dx, Dy, ANS, TMP, Dist: real; <br/> N, I, j, K, T, TT, SS: longint; </P> <p> procedure swap (var a, B: Real); begin TMP: = A;: = B; B: = TMP end; <br/> function eq (A, B: Real): Boolean; begin EQ: = ABS (a-B) <e end; <br/> function DIS (A, B, C, D: Real): real; <br/> begin DIS: = SQRT (sqr (a-c) + sqr (B-d) end; <br/> function Ji (X, Y: Real): real; <br/> begin <br/> If x <0 then Ji: = arctan (y/x) + PI else <br/> if y <0 then Ji: = arctan (y/x) + pI * 2 else Ji: = arctan (y/X); <br/> end; </P> <p> procedure sort (LL, rr: longint); var I, j: longint; <br/> begin <br/> I: = ll; J: = RR; T1: = R [(I + J)> 1]; <br/> repeat <br/> while R [I] <t1-e do Inc (I ); <br/> while R [J]> T1 + e do Dec (j ); <br/> If I <= J then begin <br/> swap (L [I], L [J]); swap (R [I], R [J]); <br/> Inc (I); Dec (j); <br/> end; <br/> until I> J; <br/> If ll <j then sort (LL, J); <br/> If I <RR then sort (I, RR); <br/> end; </P> <p> begin <br/> assign (input, 'circle. in '); reset (input); <br/> assign (output, 'circle. out'); rewrite (output); <br/> readln (n); <br/> for I: = 1 to n do readln (CX [I], CY [I], Cr [I]); <br/> for I: = 1 to n do begin <br/> T: = 0; <br/> for J: = 1 to n do if (I <> J) and not cover [J] Then begin <br/> Dist: = DIS (CX [I], CY [I], CX [J], CY [J]); <br/> If Dist + Cr [I] <CR [J] + E then cover [I]: = true else <br/> If Dist + Cr [J] <CR [I] + E then continue else <br/> If Dist <CR [I] + Cr [J] -E then begin <br/> Inc (t ); <br/> r [T]: = Ji (CX [J]-cx [I] + e, CY [J]-cy [I]); <br/> DX: = (sqr (Cr [I])-sqr (Cr [J]) + sqr (DIST)/2/Dist; <br/> DY: = SQRT (sqr (Cr [I])-DX * dx); <br/> L [T]: = R [T] + Ji (dx + e, Dy ); <br/> r [T]: = R [T]-ji (dx + e, Dy ); <br/> If R [T] <0 then begin <br/> r [T]: = R [T] + 2 * PI; <br/> L [T]: = L [T] + 2 * PI; <br/> end; <br/> If (t = 0) and not cover [I] Then ans: = ans + sqr (Cr [I]) * PI; <br/> If (t = 0) or cover [I] Then continue; <br/> sort (1, t); TT: = 0; SS: = 1; <br/> for J: = 1 to t do <br/> If (TT = 0) or (R [J]> L [TT] + E) then begin <br/> Inc (TT); L [TT]: = L [J]; R [TT]: = R [J]; <br/> end else if l [TT] <L [J] Then l [TT]: = L [J]; <br/> while (SS <= TT) and (L [TT]-2 * pi> r [ss]-E) do begin <br/> if l [TT]-2 * pI <L [ss] Then l [TT]: = L [ss] + 2 * PI; <br/> Inc (SS); <br/> end; <br/> for J: = ss to TT do begin <br/> If j <> TT then K: = J + 1 else K: = SS; <br/> DX: = R [k]-l [J]; <br/> If DX <0 then DX: = dx + 2 * PI; <br/> ans: = ans + sqr (Cr [I])/2 * (DX-sin (dx )); <br/> X1: = Cx [I] + Cr [I] * Cos (L [J]); <br/> Y1: = CY [I] + Cr [I] * sin (L [J]); <br/> X2: = Cx [I] + Cr [I] * Cos (R [k]); <br/> Y2: = CY [I] + Cr [I] * sin (R [k]); <br/> ans: = ans + (x1 * y2-x2 * Y1)/2; <br/> end; <br/> writeln (ANS: 0: 6); <br/> close (input); close (output ); <br/> end.

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.