11177 (intersection of Convex Polygon and circle)

Source: Internet
Author: User
Tags acos

11177 (intersection of Convex Polygon and circle)

The coordinates of n points with a convex n-edge shape are given in clockwise or counterclockwise order, and then a center is placed at (0, 0) the intersection area of the circular and convex n-edges is greater than or equal to R.
Question: This question is just a mess, and all kinds of details are wrong .. After one day of repair and modification, I still don't know why the OnSegment function should be written as that... Clearly unable to determine whether the vertex is online
It is difficult to draw a picture to think about the relationship between each side of the n-edge shape and the circle. If the two sides of the edge are inside the circle, the two sides correspond to the area of a triangle, if a point is inside the circle (including the boundary) outside the circle, a triangle is added with a slice. If both two points are outside the circle, if there are two intersections with the circle, it is two slices and one triangle. If there is no intersection with the circle, it is one slice.

# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Using namespace std; const double eps = 1e-9; const double PI = acos (-1); int dcmp (double x) {if (fabs (x) <eps) return 0; return x> 0? 1:-1 ;}struct Point {double x, y; Point (double a = 0, double B = 0): x (a), y (B ){}}; typedef Point Vector; Vector operator + (const Vector & a, const Vector & B) {return Vector (. x + B. x,. y + B. y);} Vector operator-(const Vector & a, const Vector & B) {return Vector (. x-B. x,. y-B. y);} Vector operator * (const Vector & a, double & B) {return Vector (. x * B,. y * B);} Vector operator/(cons T Vector & a, double & B) {return Vector (. x/B,. y/B);} bool operator = (const Vector & a, const Vector & B) {return! Dcmp (a. x-B. x )&&! Dcmp (. y-B. y);} bool operator <(const Vector & a, const Vector & B) {return. x <B. x | (. x = B. x &. y <B. y);} double Dot (const Vector & a, const Vector & B) {return. x * B. x +. y * B. y;} double Length (const Vector & a) {return sqrt (Dot (a, a);} double Cross (const Vector & a, const Vector & B) {return. x * B. y-. y * B. x;} double Angle (const Vector & a, const Vector & B) {return acos (Dot (a, B)/Length (a)/Length (B ));} struct Line {Point p; Vector v; double ang; Line () {} Line (Point a, Vector B): p (a), v (B) {ang = atan2 (B. y, B. x);} bool operator <(const Line & L) const {return ang <L. ang;} Point point (double a) {return p + v * a ;}}; struct Circle {double r; Point c; Circle () {} Circle (Point, double B): c (a), r (B) {} Point point (double a) {return Point (c. x + cos (a) * r, c. y + sin (a) * r) ;}}; bool isPointInCircle (Point P, Circle C) {return dcmp (Length (C. c-P)-C. r) <= 0;} bool OnSegment (Point p, Point p1, Point p2) {return dcmp (Dot (p1-p, p2-p) <= 0; // This is hard to understand} void SegmentCircleIntersection (Point p1, Point p2, Circle C, Point * sol, int & num) {double t1, t2; double a = (p1-C. c ). x, B = (p2-p1 ). x, c = (p1-C. c ). y, d = (p2-p1 ). y; double e = B * B + d * d, f = 2 * (a * B + c * d), g = a * a + c * c-C. r * C. r; double delta = f * f-4 * e * g; Point p; num = 0; if (dcmp (delta) <0) return; else if (dcmp (delta) = 0) {t1 = t2 = (-f)/(2 * e); p = p1 + (p2-p1) * t1; if (OnSegment (p, p1, p2) sol [num ++] = p;} else {t1 = (-f-sqrt (delta)/(2 * e ); p = p1 + (p2-p1) * t1; if (OnSegment (p, p1, p2) sol [num ++] = p; t2 = (-f + sqrt (delta)/(2 * e); p = p1 + (p2-p1) * t2; if (OnSegment (p, p1, p2) sol [num ++] = p ;}} double FanArea (Circle C, Vector v1, Vector v2) {double rad = Angle (v1, v2 ); if (dcmp (rad-PI) = 0) rad = 0; return C. r * C. r * rad * 0.5;} double GetCommonArea (Point p1, Point p2, Point p3, Circle C) {int flag1 = isPointInCircle (p2, C); int flag2 = isPointInCircle (p3, c); int num; Point sol [5]; if (flag1 + flag2 = 2) return fabs (Cross (p2-p1, p3-p1) * 0.5; if (flag1 + flag2 = 1) {SegmentCircleIntersection (p2, p3, C, sol, num); if (flag1) return FanArea (C, p3-p1, sol [0]-p1) + fabs (Cross (p2-p1, sol [0]-p1) * 0.5; return FanArea (C, p2-p1, sol [0]-p1) + fabs (Cross (p3-p1, sol [0]-p1) * 0.5;} SegmentCircleIntersection (p2, p3, C, sol, num); // note that the two endpoints correspond to different intersections. if (num = 2) return FanArea (C, p2-p1, sol [0]-p1) + fabs (Cross (sol [0]-p1, sol [1]-p1) * 0.5 + FanArea (C, p3-p1, sol [1]-p1 ); return FanArea (C, p2-p1, p3-p1);} const int N = 55; Point P [N]; double R; int n; double CommonArea (Circle C) {double res = 0; for (int I = 0; I <n; I ++) res + = GetCommonArea (C. c, P [I], P [I + 1], C); return res;} int main () {int cas = 1; while (scanf (% d, & n) = 1 & n) {scanf (% lf, & R); for (int I = 0; I <n; I ++) scanf (% lf, & P [I]. x, & P [I]. y); P [n] = P [0]; double l = 0, r = 1e9; while (dcmp (r-l)> 0) {double mid = (l + r) * 0.5; if (dcmp (CommonArea (Circle (Point (0, 0), mid)-R)> = 0) r = mid; else l = mid;} printf (Case % d: %. 2lf, cas ++, l);} 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.