Board Wrapping (calculation geometry for convex paukka vector rotation)

Source: Internet
Author: User
Tags cos in degrees sin



UVA-10652

Board Wrapping
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

[Submit]   [Go back] [Status]

Description

Problem B

Board Wrapping

Input: Standard Input
output: standard output

Time Limit: 2 seconds


The small sawmill in Mission, British Columbia, had developed a brand new-from-the-a-packaging for boards. By fixating the boards in special moulds, the board can dry efficiently in a drying.

Space is an issue though. The boards cannot is too close, because then the drying would be too slow. On the other hand, one wants to use the drying the efficiently.

Looking at it from a 2-d perspective, your task was to calculate the fraction between the space occupied by the Bo Ards to the total space occupied by the mould. Now, the mould are surrounded by an aluminium frame of negligible thickness, following the hull of the boards ' corners Tigh tly. The space occupied by the mould would thus is the interior of the frame.

Input

On the first line of input there is one integer,N <=, giving the number of test cases (moulds) in the input. After the line,NTest cases follow. Each test case starts with a line containing one integerN,1< N <=, which is the number of boards in the mould. ThenNLines follow, each with five floating point numbersx, Y, W, H, Jwhere0 <= x, y, W, H <=10000and–90°< J <=90°. Thexandyis the coordinates of the center of the Board andWandhis the width and height of the board, respectively.JThe angle between the height axis of the board to they-axis in degrees, positive clockwise. That is, ifj = 0The projection of the Board on thex-axis would beW. Of course, the boards cannot intersect.

Output

For every test case, output one line containing the fraction of the space occupied by the boards to the total space in per cent. Your output should has one decimal digit and is followed by a space and a percent sign (%).

Sample input Output for sample input

1

4

4 7.5 6) 3 0

8 11.5 6) 3 0

9.5 6 6) 3 90

4.5 3 4.4721) 2.2361 26.565

64.3%

Swedish National Contest

The sample Input and sample Output corresponds to the givenpicture


Test instructions: There are n blocks of rectangular wood, your task is to use an area as small as possible to surround them with a convex polygon, and calculate the board as a percentage of the entire packaging area;


Meaning: The geometry knowledge used is based on the level order of Andrew algorithm, the rotation of the vector and the area of the polygon to find geometric knowledge;


The following vector rotation knowledge is to copy others; And for the polygon area of the law is natural;

In a two-dimensional coordinate system, the rotation formula of a position vector can be introduced by the geometric meaning of trigonometric functions.

to the left | Turn right

As shown in the position vector r counterclockwise rotation angle B before and after the situation.

In the diagram on the left, we have a relationship:

x0 = | r| * CosA = CosA = x0/| r|

Y0 = | r| * SinA = SinA = y0/| r|

In the picture on the right, we have a relationship:

X1 = | r| * COS (A+B)

Y1 = | r| * Sin (a+b)

Where (x1, y1) is (x0, y0) the point of rotation angle B, that is, the position vector R Point of the last point. We expand the cos (a+b) and sin (a+b) to get:

X1 = | r| * (COSACOSB-SINASINB)

Y1 = | r| * (SINACOSB + cosasinb)

Now put CosA = x0/| r| and SinA = y0/|  r| Substituting the above formula to get:

X1 = | r| * (x0 * COSB/| r| -y0 * SINB/| r|) = x1 = X0 * cosb-y0 * sinb

Y1 = | r| * (y0 * COSB/| r| + x0 * Sinb/| r|) = y1 = x0 * sinb + y0 * COSB

This gives us the formula for counterclockwise rotation of the vector around the dots in two-dimensional coordinates. Rotate clockwise to turn the angle to negative:

x1 = x0 * cos (-B)-y0 * sin (-b) = x1 = X0 * COSB + y0 * SINB

y1 = x0 * sin (-B) + y0 * cos (-b) = Y1 =-x0 * sinb + y0 * COSB

Now I'm going to put this rotation formula in the form of a matrix, there is a concept I simply mention that each linear transformation in the plane or space (here is the rotation transformation) corresponds to a matrix, called the transformation matrix. The linear transformation of a point is accomplished by multiplying the matrix of the linear transformation. OK, hold on, or you'll get off the topic.

So the two-dimensional rotation transformation matrix is:

[CosA SinA] [Cosa-sina]
[-sina CosA] or [SinA CosA]

The rotation of the vector can be done by the matrix, e.g. I want the vector (x, y) to rotate counterclockwise around the origin point a:

[x, y] x [cosA SinA] = [X*cosa-y*sina x*sina+y*cosa]

[-sina CosA]

the vector after rotation is: [X*cosa-y*sina X*sina+y*cosa]


AC Code:


#include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #define PI ACOs (-1.0    ) #define EXP 1e-10using namespace std;struct point{double x, y;    Point (double x0 = 0, double y0 = 0)//constructor, initialize;        {x = x0;    y = y0;    the BOOL operator < (const point &ant) const//convex hull needs to be sorted by X and Y, note that there can be no repetition, and some things need to be heavy;        {if (ant.x! = x) return ant.x > x;    return ant.y > y; }};typedef Point Vector; Vector operator + (vector A, vector B)//custom overload + operation; {return Vector (a.x + b.x, b.y + a.y);} Vector operator-(vector A, vector B)//custom overload-operation; {return Vector (a.x-b.x, a.y-b.y);} Vector Rotate (vector A, double rad) {return vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (rad) + a.y * cos (RAD)); Double Change (Double j)//convert to radians {return j/180.0 * PI;} Double Cross (vector A, vector B)//Vector's fork product {return a.x * b.y-a.y * b.x;}  Class Convex{public:int convex (vector *p, int n, vector *ch)//Convex packet {sort (p,p + N);      int m = 0;  for (int i = 0; i < n; i++) {when (M > 1 && Cross (ch[m-1]-ch[m-2], P[i]-ch[m-2])            <= exp) m--;        ch[m++] = P[i];        } int k = m; for (int i = n-2; I >= 0; i--) {when (M > K && Cross (Ch[m-1]-ch[m-2], P[i]-ch[m            -2]) <= exp) m--;        ch[m++] = P[i];        } if (n > 1) m--;    return m;        } double Polygonarea (point *p, int n)//polygon Area {double area2 = 0.0;        for (int i = 1; i < n-1; i++) {area2 + = Cross (P[i]-p[0], P[i + 1]-p[0]);    } return 0.5 * Fabs (AREA2);    }} Hull;int Main () {int T;    Point p[2500],ch[2500];    scanf ("%d", &t);        while (t--) {int n,pc = 0;        Double area1 = 0.0;        scanf ("%d", &n);            for (int i = 0; i < n; i++) {double W,h,j,rad;            Point O; scanf ("%lf%lf%lf%lf%lf", &o.x, &AMP;O.Y, &w,&h,&j);            rad =-change (j);            P[pc++] = O + Rotate (vector (-W/2,-H/2), RAD),//Find out the coordinates of the four fixed-point of the plank p[pc++] = O + Rotate (vector (W/2,-H/2), RAD);            P[pc++] = O + Rotate (Vector (-W/2, H/2), RAD);            P[pc++] = O + Rotate (Vector (W/2, H/2), RAD); AREA1 + = w * h;//Cumulative board Area} int m = Hull.convex (p,pc,ch);        The convex bag is asked; Double area2 = Hull. Polygonarea (CH,M);    Seek area printf ("%.1lf%%\n", area1 * 100/area2); } return 0;}


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.