Hdu 2892 Area, hdu2892area

Source: Internet
Author: User
Tags acos

Hdu 2892 Area, hdu2892area

Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2892

Solution:

 

Returns the area of the intersection of a polygon and a circle.

 

Divides a Polygon into n triangles based on the center of the circle.

 

Next, we can find the area of the triangle and the circle.

 

 

 

Because one point of a triangle is the center of the circle, the other two points and circles of the triangle are as follows:

 

(1) The two points are in the circle, and the area of the triangle and the circle are equal to the area of the triangle.

 

(2) A point is located outside the circle, and a point is located in the circle. The area of the triangle and the circle intersection = Area of the small triangle + area of the slice

 

(3) The two points are both out of the circle and fall into several situations:

 

1. When a line segment composed of two points and the circle are 0 or 1 point, the area of the triangle and the circle is equal to the area of the slice.

 

2. When two points of the intersection of a line segment and a circle composed of two points, the area of the triangle and the Circle = large slice area + small triangle area-Small slice Area

 

1 # include <cmath> 2 # include <cstdio> 3 # include <vector> 4 # include <algorithm> 5 using namespace std; 6 7 # define MAXN 100000 + 10 8 # define PI acos (-1.0) 9 # define EPS 0.00000001 10 11 int dcmp (double x) {12 if (fabs (x) <EPS) 13 return 0; 14 return x <0? -1: 1; 15} 16 17 struct Point {18 double x, y; 19 Point (double x = 0, double y = 0): x (x ), y (y) {} 20}; 21 22 struct Circle {23 Point c; 24 double r; 25 Circle (Point c = Point (0, 0), double r = 0): c (c), r (r) {}26}; 27 28 typedef Point Vector; 29 30 Vector operator + (Vector A, Vector B) {31 return Vector (. x + B. x,. y + B. y); 32} 33 Vector operator-(Point A, Point B) {34 return Vector (. X-B. x,. y-B. y); 35} 36 Vector operator * (Vector A, double p) {37 return Vector (. x * p,. y * p); 38} 39 Vector operator/(Vector A, double p) {40 return Vector (. x/p,. y/p); 41} 42 43 double dot (Vector A, Vector B) {44 return. x * B. x +. y * B. y; 45} 46 47 double length (Vector A) {48 return sqrt (dot (A, A); 49} 50 51 double angle (Vector A, Vector B) {52 return acos (dot (A, B )/ Length (A)/length (B); 53} 54 55 double cross (Vector A, Vector B) {56 return. x * B. y-. y * B. x; 57} 58 59 Circle bomb; // coordinates and radius of bomb explosion 60 Point p [MAXN]; // Island Point 61 int n; // island Points 62 63 double point_line_distance (Point P, Point A, Point B) {// distance from A Point to A straight line 64 Vector AP = P-A, AB = B-; 65 return fabs (cross (AP, AB)/length (AB); 66} 67 68 Point point_line_projection (Point P, Point A, Point B) {// Point on A straight line Ing 69 Vector v = B-A; 70 return A + v * (dot (v, P-A)/dot (v, v )); 71} 72 73 int circle_line_intersect (Circle C, Point A, Point B, vector <Point> & v) {74 double dist = point_line_distance (C. c, A, B); 75 int d = dcmp (dist-C. r); 76 if (d> 0) {77 return 0; 78} 79 Point pro = point_line_projection (C. c, A, B); 80 if (d = 0) {81 v. push_back (pro); 82 return 1; 83} 84 double len = sqrt (C. r * C. r- Dist * dist); // ing theorem 85 Vector AB = B-A; 86 Vector l = AB/length (AB) * len; 87 v. push_back (pro + l); 88 v. push_back (pro-l); 89 return 2; 90} 91 92 bool point_on_segment (Point P, Point A, Point B) {// determine 93 Vector PA = A-P, PB = B-P; 94 return dcmp (cross (PA, PB )) = 0 & dcmp (dot (PA, PB) <= 0; 95} 96 97 double circle_delta_intersect_area (Circle C, Point A, Point B) {98 Vector CA = -C. c, CB = B-C. c; 99 double da = length (CA), db = length (CB); 100 101 da = dcmp (da-C. r), db = dcmp (db-C. r); 102 103 if (da <= 0 & db <= 0) {// 104 return fabs (cross (CA, CB) * 0.5 in the circle; 105} 106 107 vector <Point> v; 108 int num = circle_line_intersect (C, A, B, v); // The Relationship Between the circle and the straight line is 109 double carea = C. r * C. r * PI; 110 Point t; 111 if (da <= 0 & db> 0) {// point on the left in the circle the point on the right outside the circle 112 t = point_on_segment (v [0],, B )? V [0]: v [1]; 113 114 double area = fabs (cross (CA, t-C. c) * 0.5, an = angle (CB, t-C. c); 115 return area + carea * an/PI/2; 116} 117 if (da> 0 & db <= 0) {// The left point is outside the circle. the right point is in the circle. 118 t = point_on_segment (v [0], A, B )? V [0]: v [1]; 119 120 double area = fabs (cross (CB, t-C. c) * 0.5, an = angle (CA, t-C. c); 121 return area + carea * an/PI/2; 122} 123 // both vertices are out of the circle 124 if (num = 2) {125 double bigarea = carea * angle (CA, CB)/PI/2,126 smallarea = carea * angle (v [0]-C. c, v [1]-C. c)/PI/2,127 deltaarea = fabs (cross (v [0]-C. c, v [1]-C. c) * 0.5; 128 return bigarea + deltaarea-smallarea; 129} 130 return carea * Angle (CA, CB)/PI/2; // two vertices are both in the outer circle. One or two 131} 132 133 double circle_polygon_intersect_area () {// The Source polygon intersection area is 134 p [n] = p [0]; 135 double ans = 0; 136 for (int I = 0; I <n; I ++) {137 double area = circle_delta_intersect_area (bomb, p [I], p [I + 1]); 138 if (cross (p [I]-bomb. c, p [I + 1]-bomb. c) <0) {139 area =-area; 140} 141 ans + = area; 142} 143 return ans> 0? Ans:-ans; 144} 145 146 void solve () {147 scanf ("% d", & n); 148 for (int I = 0; I <n; I ++) {149 scanf ("% lf", & p [I]. x, & p [I]. y); 150} 151 printf ("%. 2lf \ n ", circle_polygon_intersect_area (); 152} 153 154 int main () {155 // freopen (" data. in "," r ", stdin); 156 double x, y, h, x1, y1, r; 157 while (~ Scanf ("% lf", & x, & y, & h) {158 scanf ("% lf", & x1, & y1, & r); 159 160 double t = sqrt (0.2 * h ); // h = 0.5 * G * t ^ 2 gravity Acceleration Formula 161 162 bomb = Circle (Point (x1 * t + x, y1 * t + y), r ); 163 164 solve (); 165} 166 return 0; 167}

 


Help me explain this question (HDU)

Bare BFS, but pay attention to the following mark, with x, y, t 3D mark. Because some coordinates have been used to reset the bomb multiple times ..
Paste my code ..
# Include <stdio. h>
Struct queue
{
Int x;
Int y;
Int time;
Int t;
} A [200000];
Int si, sj, ei, ej, n, m, d [4] [2] = {-, 0,-, 1 };
Int map [10] [10], hash [10] [10] [10];
Int bfs ()
{
Int I, x1, y1, t1, head, tail;
A [0]. x = si;
A [0]. y = sj;
A [0]. time = 0;
A [0]. t = 6;
Head = 0; tail = 1;
While (head <tail)
{
// Printf ("\ n % d \ n", a [head]. x, a [head]. y, a [head]. time, a [head]. t );
// Printf ("------------------------------------- \ n ");

For (I = 0; I <4; I ++)
{
If (a [head]. t <2)
Break;
X1 = a [head]. x + d [I] [0];
Y1 = a [head]. y + d [I] [1];
If (map [x1] [y1] = 4)
T1 = 6;
Else
T1 = a [head]. T-1;
If (hash [x1] [y1] [t1])
Continue;
If (x1 <0 | x1> = n | y1 <0 | y1> = m)
Continue;
If (map [x1] [y1] = 0)
Continue;
A [tail]. x = x1;
A [tail]. y = y1;
A [tail]. t = t1;
A [tail]. time = a [head]. time + 1;
Hash [x1] [y1] [a [tail]. t] = 1; // note the mark here.
// Printf ("% d \ n", a [tail]. x, a [tail]. y, a [tail]. time, a [tail]. t );
If (x1 = ei & y1 = ej)
Return (a [head]. time + 1 );
Tail ++;
}
// Printf ("\ n ");
Head ++;
}
Return 0;
} ...... Remaining full text>


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.