uva–10652 Board wrapping[Convex bag]

Source: Internet
Author: User
Tags acos cos

Test instructions

Gives the midpoint, length, width and clockwise angle of n rectangles. Lets you wrap them up with the smallest convex polygon to calculate the percentage of the rectangular area that occupies the convex polygon.

With the algorithm of convex hull given by large petition, each vertex of the rectangle is convex and the area of convex hull is calculated.

#include"bits/stdc++.h"using namespacestd;Const intMAXN = the;Const Doubleeps=1e-8;Const DoublePI = ACOs (-1.0);structPoint {Doublex, y; Point (Doublex =0.0,Doubley =0.0): X (x), Y (y) {}};typedef point Vector; Pointoperator+(Point A, point B) {returnPoint (a.x+b.x, a.y+b.y);} Pointoperator-(Point A, point B) {returnPoint (a.x-b.x, a.y-b.y);} Pointoperator* (Point A,Doublep) {returnPoint (A.x*p, a.y*p);} Pointoperator/(Point A,Doublep) {returnPoint (a.x/p, a.y/p);}BOOL operator< (Constpoint& A,Constpoint&b) {returna.x<b.x | | (a.x==b.x && a.y<b.y);}intDCMP (Doublex) {if(Fabs (x) <eps)return 0;returnx<0?-1:1;}BOOL operator== (Constpoint& A,ConstPoint &b) {returnDCMP (a.x-b.x) = =0&& dcmp (a.y-b.y) = =0;}DoubleDot A, point B) {returna.x*b.x+a.y*b.y;}DoubleCross (Point A, point B) {returna.x*b.y-a.y*b.x;}DoubleLength (Point A) {returnsqrt (Dot (a,a));} Vector Normal (vector A) {returnVector (-A.Y, a.x)/Length (A);}DoubleAngle (vector A, vector B) {returnACOs (Dot (b)/length (A)/Length (B)); Vector Rotate (vector A,Doublerad) {    returnVector (A.x*cos (RAD)-a.y*sin (RAD), A.x*sin (RAD) +a.y*cos (RAD));} DoublePolygonarea (point* p,intN) {DoubleArea =0.0;  for(inti =1; I < n1; i++) Area+ = Cross (p[i]-p[0],p[i+1]-p[0]); returnarea/2.0;} Point P[MAXN], CH[MAXN];BOOLCMP (Point A, point B) {if(dcmp (a.x-b.x) = =0)returnDCMP (A.Y-A.Y) <=0; returnDCMP (a.x-b.x) <0;}//output n points, output CH as the points contained in the convex package//If you do not want a point on the edge <= to < Enter No DuplicatesintConvexhull (point* p,intN, point*ch) {sort (p, p+ N);//Compare x First, compare y dictionary order    intm =0;  for(inti =0; I < n; i++) {         while(M >1&& Cross (ch[m-1]-ch[m-2], p[i]-ch[m-2]) <=0) m--; Ch[m++] =P[i]; }    intK =m;  for(inti = n-2; I >=0; i--) {         while(M > K && Cross (ch[m-1]-ch[m-2], p[i]-ch[m-2]) <=0) m--; Ch[m++] =P[i]; }    if(N >1) m--; returnm;}intMainintargcChar Const*argv[]) {    intT; scanf ("%d", &T);  while(t--) {        intN, cnt =0; scanf ("%d", &N); DoubleARA =0.0;  for(inti =0; i < N; i++) {            Doublex, Y, W, H, J; scanf ("%LF%LF%LF%LF%LF", &x, &y, &w, &h, &j); DoubleAng =-(j/180.0)*PI; Point O=Point (x, y); P[cnt+ +] = O + Rotate (Vector (w/2, h/2), ANG); P[cnt+ +] = O + Rotate (Vector (-w/2, h/2), ANG); P[cnt+ +] = O + Rotate (Vector (w/2,-h/2), ANG); P[cnt+ +] = O + Rotate (Vector (-w/2,-h/2), ANG); ARA+ = w*h; }        intm =convexhull (P, CNT, CH); DoubleA =polygonarea (CH, m); printf ("%.1LF%%\n", ara* -/A); }    return 0;}

uva–10652 Board wrapping[Convex bag]

Related Article

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.