Ultraviolet A 10652 Board Wrapping

Source: Internet
Author: User

Question:

Returns the clockwise rotation angle of multiple rectangles.

Ratio of rectangular area to convex hull Area

Validate template questions

 

 

// Big white p263 # include <cmath> # include <cstdio> # include <cstring> # include <string> # include <queue> # include <functional> # include <set> # include <iostream> # include <vector> # include <algorithm> using namespace std; const double eps = 1e-8; // precision const int INF = 1 <29; const double PI = acos (-1.0); int dcmp (double x) {// judge that double is equal to 0 or... If (fabs (x) <eps) return 0; else return x <0? -1:1;} struct Point {double x, y; Point (double x = 0, double y = 0): x (x), y (y ){}}; typedef Point Vector; typedef vector <Point> Polygon; Vector operator + (Vector a, Vector B) {return Vector (. x + B. x,. y + B. y);} // Vector + Vector = Vector operator-(Point a, Point B) {return Vector (. x-b.x,. y-b.y);} // point-point = Vector operator * (Vector a, double p) {return Vector (. x * p,. y * p);} // Vector * real number = Vector operator/(Vector a, double p) {retur N Vector (. x/p,. y/p);} // vector/real number = vector bool operator <(const Point & A, const Point & B) {return dcmp (. x-B.x) <0 | (dcmp (. x-B.x) = 0 & dcmp (. y-B.y) <0);} bool operator = (const Point & a, const Point & B) {return dcmp (. x-b.x) = 0 & dcmp (. y-b.y) = 0;} bool operator! = (Const Point & a, const Point & B) {return a = B? False: true;} struct Segment {Point a, B; Segment () {} Segment (Point _ a, Point _ B) {a = _ a, B = _ B ;} bool friend operator <(const Segment & p, const Segment & q) {return p. a <q. a | (p. a = q. a & p. B <q. b);} bool friend operator = (const Segment & p, const Segment & q) {return (p. a = q. a & p. B = q. b) | (p. a = q. B & p. B = q. a) ;}}; struct Circle {Point c; double r; Circle () {}circle (Point _ c, double _ r): c (_ c ), r (_ r) {} Point point (do Uble a) const {return Point (c. x + cos (a) * r, c. y + sin (a) * r);} bool friend operator <(const Circle & a, const Circle & B) {return. r <B. r ;}}; struct Line {Point p; Vector v; double ang; Line () {}line (const Point & _ p, const Vector & _ v ): p (_ p), v (_ v) {ang = atan2 (v. y, v. x);} bool operator <(const Line & L) const {return ang <L. ang ;}}; double Dot (Vector a, Vector B) {return. x * B. x +. y * B. y;} // | a | * | B | * cos θ vertex product double Le Ngth (Vector a) {return sqrt (Dot (a, a) ;}// | a | Vector length double Angle (Vector a, Vector B) {return acos (Dot (a, B)/Length (a)/Length (B);} // Vector angle θ double Cross (Vector a, Vector B) {return. x * B. y-a.y * B. x ;}// the area of the parallelogram enclosed by the Cross product vector, double Area2 (Point a, Point B, Point c) {return Cross (B-a, c-);} // double DegreeToRadius (double deg) {return deg/180 * PI;} double GetRerotateAngle (Vector a, Vector B) {// vector a rotates theta clockwise to obtain the side of vector B. To double tempa = Angle (a, Vector (1, 0); if (. y <0) tempa = 2 * PI-tempa; double tempb = Angle (B, Vector (1, 0); if (B. y <0) tempb = 2 * PI-tempb; if (tempa-tempb)> 0) return tempa-tempb; else return tempa-tempb + 2 * PI ;} double torad (double deg) {return deg/180 * PI;} // angular to radian Vector Rotate (Vector a, double rad) {// returns Vector (. x * cos (rad)-. y * sin (rad),. x * sin (rad) +. y * cos (rad);} Vector Normal (Vector a) {// count Returns Vector (-. y/L,. x/L);} Point GetLineProjection (Point p, Point a, Point B) {// projection Vector v = B-a on a straight line; return a + v * (Dot (v, p-a)/Dot (v, v);} Point GetLineIntersection (Point p, Vector v, Point q, Vector w) {// returns the Vector u = p-q; double t = Cross (w, u)/Cross (v, w ); return p + v * t;} int ConvexHull (Point * p, int n, Point * sol) {// calculate the convex hull sort (p, p + n ); int m = 0; for (int I = 0; I <n; I ++ ){ While (m> 1 & Cross (sol [M-1]-sol [m-2], p [I]-sol [m-2]) <= 0) m --; sol [m ++] = p [I];} int k = m; for (int I = n-2; I> = 0; I --) {while (m> k & Cross (sol [M-1]-sol [m-2], p [I]-sol [m-2]) <= 0) m --; sol [m ++] = p [I];} if (n> 0) m --; return m;} double Heron (double a, double B, double c) {// Helen formula double p = (a + B + c)/2; return sqrt (p * (p-a) * (p-B) * (p-c);} bool SegmentProperIntersection (Point a1, Point a2, Point b1, Point b2) {// specifies the intersection of line segments to determine the double C1 = Cross (a2-a1, b1-a1), c2 = Cross (a2-a1, b2-a1); double c3 = Cross (b2-b1, a1-b1), c4 = Cross (b2-b1, a2-b1 ); return dcmp (c1) * dcmp (c2) <0 & dcmp (c3) * dcmp (c4) <0;} double CutConvex (const int n, Point * poly, const Point a, const Point B, vector <Point> result [3]) {// returns a straight line a B cutting a convex polygon vector <Point> points; Point p; point p1 = a, p2 = B; int cur, pre; result [0]. clear (); result [1]. clear (); result [2]. clear (); if (n = 0) return 0; d Ouble tempcross; tempcross = Cross (P2-P1, poly [0]-p1); if (dcmp (tempcross) = 0) pre = cur = 2; else if (tempcross> 0) pre = cur = 0; else pre = cur = 1; for (int I = 0; I <n; I ++) {tempcross = Cross (P2-P1, poly [(I + 1) % n]-p1); if (dcmp (tempcross) = 0) cur = 2; else if (tempcross> 0) cur = 0; else cur = 1; if (cur = pre) {result [cur]. push_back (poly [(I + 1) % n]);} else {p1 = poly [I]; p2 = poly [(I + 1) % n]; p = GetLineIntersection (p1, P2-P1, a, B-a); poin Ts. push_back (p); result [pre]. push_back (p); result [cur]. push_back (p); result [cur]. push_back (poly [(I + 1) % n]); pre = cur ;}} sort (points. begin (), points. end (); if (points. size () <2) {return 0;} else {return Length (points. front ()-points. back () ;}} double DistanceToSegment (Point p, Segment s) {// distance from a Point to a line Segment if (s. a = s. b) return Length (p-s.a); Vector v1 = s. b-s.a, v2 = p-s.a, v3 = p-s. B; if (dcmp (Dot (v1, v2) <0) return L Ength (v2); else if (dcmp (Dot (v1, v3)> 0) return Length (v3); else return fabs (Cross (v1, v2 )) /Length (v1);} bool isPointOnSegment (Point p, Segment s) {return dcmp (Cross (s. a-p, s. b-p) = 0 & dcmp (Dot (s. a-p, s. b-p) <0;} int isPointInPolygon (Point p, Point * poly, int n) {// relationship between Point and polygon position int wn = 0; for (int I = 0; I <n; I ++) {Point & p2 = poly [(I + 1) % n]; if (isPointOnSegment (p, segment (poly [I], p2) return-1; // point on the boundary int k = dcmp (Cross (p2-poly [I], p-poly [I]); int d1 = dcmp (poly [I]. y-p.y); int d2 = dcmp (p2.y-p. y); if (k> 0 & d1 <= 0 & d2> 0) wn ++; if (k <0 & d2 <= 0 & d1> 0) wn --;} if (wn) return 1; // point in the internal else return 0; // Point in the external} double PolygonArea (Point * p, int n) {// polygon directed area double area = 0; for (int I = 1; I <n-1; I ++) area + = Cross (p [I]-p [0], p [I + 1]-p [0]); return area/2 ;} int GetLineCircleIntersection (Line L, Circle C, Point & p1, Point & p2) {// The number of intersection points returned by the intersection of the Circle and the straight Line Double a = L. v. x, B = L. p. x-C. c. x, c = L. v. y, d = L. p. y-C.c.y; double e = a * a + c * c, f = 2 * (a * B + c * d), g = B * B + d * d-C. r * C. r; double delta = f * f-4 * e * g; if (dcmp (delta) <0) return 0; // if (dcmp (delta) = 0) {// tangent p1 = p1 = C. point (-f/(2 * e); return 1 ;}// intersection p1 = (L. p + L. v * (-f-sqrt (delta)/(2 * e); p2 = (L. p + L. v * (-f + sqrt (delta)/(2 * e); return 2;} double rotating_calipers (Point * ch, int n) // rotate the card shell {int q = 1; double ans = 0; ch [n] = ch [0]; for (int p = 0; p <n; p ++) {while (Cross (ch [q + 1]-ch [p + 1], ch [p]-ch [p + 1])> cross (ch [q]-ch [p + 1], ch [p]-ch [p + 1]) q = (q + 1) % n; ans = max (ans, max (Length (ch [p]-ch [q]), length (ch [p + 1]-ch [q + 1]);} return ans;} Polygon CutPolygon (Polygon poly, Point a, Point B) {// use a-> B to cut the Polygon and return the left Polygon newpoly; int n = poly. size (); for (int I = 0; I <n; I ++) {Point c = poly [I]; Point d = poly [(I + 1) % n]; if (dcmp (Cros S (B-a, c-a)> = 0) newpoly. push_back (c); if (dcmp (Cross (B-a, c-d ))! = 0) {Point ip = GetLineIntersection (a, B-a, c, d-c); if (isPointOnSegment (ip, Segment (c, d) newpoly. push_back (ip) ;}} return newpoly;} int GetCircleCircleIntersection (Circle c1, Circle c2, Point & p1, Point & p2) {double d = Length (c1.c-c2.c ); if (dcmp (d) = 0) {if (dcmp (c1.r-c2.r) = 0) return-1; // returns 0 when two circles overlap ;} if (dcmp (c1.r + c2.r-d) <0) return 0; if (dcmp (fabs (c1.r-c2.r)-d)> 0) return 0; double a = Angle (c2.c-c1.c, V Ector (); double da = acos (c1.r * c1.r + d * d-c2.r * c2.r)/(2 * c1.r * d )); p1 = c1.point (a-da); p2 = c1.point (a + da); if (p1 = p2) return 1; return 2 ;} // -------------------------------------------- // ---------------------------------------- // use int main () {int T; Point P [2500], ch [2500]; scanf ("% d", & T ); While (T --) {int n, idx = 0; scanf ("% d", & n); double ans = 0; for (int I = 0; I <n; I ++) {double x, y, w, h, j, ang; scanf ("% lf", & x, & y, & w, & h, & j); ang =-torad (j); Point o (x, y ); P [idx ++] = o + Rotate (Vector (-w/2,-h/2), ang ); P [idx ++] = o + Rotate (Vector (w/2,-h/2), ang ); P [idx ++] = o + Rotate (Vector (-w/2, h/2), ang ); P [idx ++] = o + Rotate (Vector (w/2, h/2), ang); ans + = w * h;} int m = ConvexHull (P, idx, ch); ans = ans * 100/PolygonArea (ch, M); printf ("%. 1lf % \ n ", ans);} return 0 ;} // big white p263 # include <cmath> # include <cstdio> # include <cstring> # include <string> # include <queue> # include <functional> # include <set> # include <iostream> # include <vector> # include <algorithm> using namespace std; const double eps = 1e-8; // precision const int INF = 1 <29; const double PI = acos (-1.0); int dcmp (double x) {// judge that double is equal to 0 or... If (fabs (x) <eps) return 0; else return x <0? -1:1;} struct Point {double x, y; Point (double x = 0, double y = 0): x (x), y (y ){}}; typedef Point Vector; typedef vector <Point> Polygon; Vector operator + (Vector a, Vector B) {return Vector (. x + B. x,. y + B. y);} // Vector + Vector = Vector operator-(Point a, Point B) {return Vector (. x-b.x,. y-b.y);} // point-point = Vector operator * (Vector a, double p) {return Vector (. x * p,. y * p);} // Vector * real number = Vector operator/(Vector a, double p) {return Vector (. X/p,. y/p);} // vector/real number = vector bool operator <(const Point & A, const Point & B) {return dcmp (. x-B.x) <0 | (dcmp (. x-B.x) = 0 & dcmp (. y-B.y) <0);} bool operator = (const Point & a, const Point & B) {return dcmp (. x-b.x) = 0 & dcmp (. y-b.y) = 0;} bool operator! = (Const Point & a, const Point & B) {return a = B? False: true;} struct Segment {Point a, B; Segment () {} Segment (Point _ a, Point _ B) {a = _ a, B = _ B ;} bool friend operator <(const Segment & p, const Segment & q) {return p. a <q. a | (p. a = q. a & p. B <q. b);} bool friend operator = (const Segment & p, const Segment & q) {return (p. a = q. a & p. B = q. b) | (p. a = q. B & p. B = q. a) ;}}; struct Circle {Point c; double r; Circle () {}circle (Point _ c, double _ r): c (_ c ), r (_ r) {} Point point (double a) const {Return Point (c. x + cos (a) * r, c. y + sin (a) * r);} bool friend operator <(const Circle & a, const Circle & B) {return. r <B. r ;}}; struct Line {Point p; Vector v; double ang; Line () {}line (const Point & _ p, const Vector & _ v ): p (_ p), v (_ v) {ang = atan2 (v. y, v. x);} bool operator <(const Line & L) const {return ang <L. ang ;}}; double Dot (Vector a, Vector B) {return. x * B. x +. y * B. y ;}// | a | * | B | * cos θ vertex product double Length (Vector a) {return sqr T (Dot (a, a);} // | a | Vector length double Angle (Vector a, Vector B) {return acos (Dot (a, B) /Length (a)/Length (B);} // Vector angle θ double Cross (Vector a, Vector B) {return. x * B. y-a.y * B. x ;}// the area of the parallelogram enclosed by the Cross product vector, double Area2 (Point a, Point B, Point c) {return Cross (B-a, c-);} // double DegreeToRadius (double deg) {return deg/180 * PI;} double GetRerotateAngle (Vector a, Vector B) {// Vector a rotates theta clockwise to obtain the direction of Vector B. double tempa = Angle (a, Vector (1, 0); if (. y <0) tempa = 2 * PI-tempa; double tempb = Angle (B, Vector (1, 0); if (B. y <0) tempb = 2 * PI-tempb; if (tempa-tempb)> 0) return tempa-tempb; else return tempa-tempb + 2 * PI ;} double torad (double deg) {return deg/180 * PI;} // angular to radian Vector Rotate (Vector a, double rad) {// returns Vector (. x * cos (rad)-. y * sin (rad),. x * sin (rad) +. y * cos (rad);} Vector Normal (Vector a) {// calculates the unit of Normal double L = Length (a); return Vector (-. y /L,. x/L);} Point GetLineProjection (Point p, Point a, Point B) {// projection Vector v = B-a on a straight line; return a + v * (Dot (v, p-a)/Dot (v, v);} Point GetLineIntersection (Point p, Vector v, Point q, Vector w) {// returns the Vector u = p-q; double t = Cross (w, u)/Cross (v, w ); return p + v * t;} int ConvexHull (Point * p, int n, Point * sol) {// calculate the convex hull sort (p, p + n ); int m = 0; for (int I = 0; I <n; I ++) {while (m> 1 & Cross (sol [M-1]-sol [m-2], p [I]-sol [m-2]) <= 0) m --; s Ol [m ++] = p [I] ;}int k = m; for (int I = n-2; I> = 0; I --) {while (m> k & Cross (sol [M-1]-sol [m-2], p [I]-sol [m-2]) <= 0) m --; sol [m ++] = p [I];} if (n> 0) m --; return m;} double Heron (double a, double B, double c) {// Helen formula double p = (a + B + c)/2; return sqrt (p * (p-a) * (p-B) * (p-c);} bool SegmentProperIntersection (Point a1, Point a2, Point b1, Point b2) {// determine the intersection of line segments with the regular intersection of double c1 = Cross (a2-a1, b1-a1), c2 = Cross (a2-a1, b2-a1); double c3 = Cross (b2-b1, a1-b1), c 4 = Cross (b2-b1, a2-b1); return dcmp (c1) * dcmp (c2) <0 & dcmp (c3) * dcmp (c4) <0 ;} double CutConvex (const int n, Point * poly, const Point a, const Point B, vector <Point> result [3]) {// vector of the convex polygon <Point> points; Point p; Point p1 = a, p2 = B; int cur, pre; result [0]. clear (); result [1]. clear (); result [2]. clear(); if (n = 0) return 0; double tempcross; tempcross = Cross (P2-P1, poly [0]-p1); if (dcmp (tempcross) = 0) pre = cur = 2; el Se if (tempcross> 0) pre = cur = 0; else pre = cur = 1; for (int I = 0; I <n; I ++) {tempcross = Cross (P2-P1, poly [(I + 1) % n]-p1); if (dcmp (tempcross) = 0) cur = 2; else if (tempcross> 0) cur = 0; else cur = 1; if (cur = pre) {result [cur]. push_back (poly [(I + 1) % n]);} else {p1 = poly [I]; p2 = poly [(I + 1) % n]; p = GetLineIntersection (p1, P2-P1, a, B-a); points. push_back (p); result [pre]. push_back (p); result [cur]. push_back (p); result [cur]. push_back (poly [(I + 1) % N]); pre = cur ;}} sort (points. begin (), points. end (); if (points. size () <2) {return 0;} else {return Length (points. front ()-points. back () ;}} double DistanceToSegment (Point p, Segment s) {// distance from a Point to a line Segment if (s. a = s. b) return Length (p-s.a); Vector v1 = s. b-s.a, v2 = p-s.a, v3 = p-s. B; if (dcmp (Dot (v1, v2) <0) return Length (v2); else if (dcmp (Dot (v1, v3)> 0) return Length (v3); else return fabs (Cross (v1, v2)/Length (v1);} bool isPointOn Segment (Point p, Segment s) {return dcmp (Cross (s. a-p, s. b-p) = 0 & dcmp (Dot (s. a-p, s. b-p) <0;} int isPointInPolygon (Point p, Point * poly, int n) {// relationship between Point and polygon position int wn = 0; for (int I = 0; I <n; I ++) {Point & p2 = poly [(I + 1) % n]; if (isPointOnSegment (p, segment (poly [I], p2) return-1; // point on the boundary int k = dcmp (Cross (p2-poly [I], p-poly [I]); int d1 = dcmp (poly [I]. y-p.y); int d2 = dcmp (p2.y-p. y); if (k> 0 & d1 <= 0 & d2> 0) wn ++; if (k <0 & d2 <= 0 & d1> 0) w N --;} if (wn) return 1; // Point in the internal else return 0; // Point in the external} double PolygonArea (Point * p, int n) {// polygon directed area double area = 0; for (int I = 1; I <n-1; I ++) area + = Cross (p [I]-p [0], p [I + 1]-p [0]); return area/2;} int GetLineCircleIntersection (Line L, circle C, Point & p1, Point & p2) {// returns the number of intersections between a Circle and a straight line. double a = L. v. x, B = L. p. x-C. c. x, c = L. v. y, d = L. p. y-C.c.y; double e = a * a + c * c, f = 2 * (a * B + c * d), g = B * B + d * d-C. r * C. r; double delta = F * f-4 * e * g; if (dcmp (delta) <0) return 0; // if (dcmp (delta) = 0) {// tangent p1 = p1 = C. point (-f/(2 * e); return 1 ;}// intersection p1 = (L. p + L. v * (-f-sqrt (delta)/(2 * e); p2 = (L. p + L. v * (-f + sqrt (delta)/(2 * e); return 2;} double rotating_calipers (Point * ch, int n) // rotate the card shell {int q = 1; double ans = 0; ch [n] = ch [0]; for (int p = 0; p <n; p ++) {while (Cross (ch [q + 1]-ch [p + 1], ch [p]-ch [p + 1])> cross (ch [q]-ch [p + 1], ch [p]-ch [p + 1]) q = (q + 1) % n; ans = max (ans, max (Le Ngth (ch [p]-ch [q]), Length (ch [p + 1]-ch [q + 1]);} return ans ;} polygon CutPolygon (Polygon poly, Point a, Point B) {// use a-> B to cut the Polygon returns the left Polygon newpoly; int n = poly. size (); for (int I = 0; I <n; I ++) {Point c = poly [I]; Point d = poly [(I + 1) % n]; if (dcmp (Cross (B-a, c-a)> = 0) newpoly. push_back (c); if (dcmp (Cross (B-a, c-d ))! = 0) {Point ip = GetLineIntersection (a, B-a, c, d-c); if (isPointOnSegment (ip, Segment (c, d) newpoly. push_back (ip) ;}} return newpoly;} int GetCircleCircleIntersection (Circle c1, Circle c2, Point & p1, Point & p2) {double d = Length (c1.c-c2.c ); if (dcmp (d) = 0) {if (dcmp (c1.r-c2.r) = 0) return-1; // returns 0 when two circles overlap ;} if (dcmp (c1.r + c2.r-d) <0) return 0; if (dcmp (fabs (c1.r-c2.r)-d)> 0) return 0; double a = Angle (c2.c-c1.c, Vector (); double da = acos (c1.r * c1.r + d * d-c2.r * c2.r) /(2 * c1.r * d); p1 = c1.point (a-da); p2 = c1.point (a + da); if (p1 = p2) return 1; return 2;} // -------------------------------------- // -------------------------------------------- // returns // calls int main () {int T; Point P [2500], ch [2500]; scanf ("% d", & T); while (T --) {int n, idx = 0; scanf ("% d", & n); double ans = 0; for (int I = 0; I <n; I ++) {double x, y, w, h, j, ang; scanf ("% lf", & x, & y, & w, & h, & j); ang =-torad (j ); point o (x, y); P [idx ++] = o + Rotate (Vector (-w/2,-h/2), ang ); P [idx ++] = o + Rotate (Vector (w/2,-h/2), ang ); P [idx ++] = o + Rotate (Vector (-w/2, h/2), ang ); P [idx ++] = o + Rotate (Vector (w/2, h/2), ang); ans + = w * h;} int m = ConvexHull (P, idx, ch); ans = ans * 100/PolygonArea (ch, m); printf ("%. 1lf % \ n ", ans);} 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.