UVA 10652 Board Wrapping (computational geometry basis, convex hull)

Source: Internet
Author: User

Topic Link: Portal

Analysis:

Nothing to say is to ask for a convex bag. Can be used as a template,

The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <    Algorithm>using namespace Std;const Double eps = 1e-10;//judgment symbol, improved accuracy int dcmp (double x) {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) {} bool operator < (const point& a) const{if (a.x! = x) r        Eturn x < a.x;    Return y < a.y;    } bool operator = = (Const point B) const{return dcmp (x-b.x) ==0&&dcmp (Y-B.Y) ==0; }};typedef Point Vector; Vector operator + (vector A, vector B) {return vector (a.x+b.x, a.y+b.y);} Vector operator-(point A, point B) {return vector (a.x-b.x, a.y-b.y);} Vector operator * (vector A, double p) {return vector (a.x*p, a.y*p);} Vector operator/(vector A, double p) {return vector (a.x/p, a.y/p);}  Double Dot (vector A, vector B) {//Shang by return a.x*b.x + a.y*b.y; A*b*cos (A, B)}double Length (Vector a) {return sqrt (Dot (A, a));    The length of the vector}double Angle (vector A, vector b) {return ACOs (Dot (A, b)/Length (a)/length (b)); The angle of the vector}double Cross (vector A, vector B) {//fork product return a.x*b.y-a.y*b.x;} The/** vector (x, y) rotates a degree counterclockwise around the starting point. X ' = X*cosa-y*sinay ' = X*sina + y*cosa**/vector Rotate (vector a,double a) {return vector (A.x*cos (a)-a.y*sin (a), a.x*s In (a) +a.y*cos (a)); Double trans (double ang) {return Ang/180*acos (-1.0);} Cross product, can be used to determine the direction and the area of double crosses (point a,point b,point c) {return (c.x-a.x) * (B.Y-A.Y)-(b.x-a.x) * (C.Y-A.Y);}    To find the area of a polygon double S (point p[],int N) {double ans = 0;    P[n] = p[0];    for (int i=1; i<n; i++) ans + = Cross (p[0],p[i],p[i+1]);    if (ans < 0) ans =-ans; return ans/2.0;} /** the two-dimensional convex hull Andrew algorithm, will all the points x small to large (x equal, y small to large) sorting to delete the duplicate point, get a sequence p1,p2 ..., and then put P1,P2 into the convex hull, starting from the P3 when the new point and the direction of the left (can be used to determine the direction of cross product) continue, otherwise, Delete the point that recently joined the convex hull until the new point is left.    **/int convexhull (Point *p,int n,point *stack) {sort (p,p+n);    N=unique (P,p+n)-p;    int m=0;        for (int i=0; i<n; i++) {//If you do not want the input points on the sides of the convex hull to have the two equals sign removedwhile (M>1&&cross (stack[m-2],p[i],stack[m-1]) <=0) m--;    Stack[m++]=p[i];    } int k=m;        for (int i=n-2, i>=0; i--) {while (M>k&&cross (stack[m-2],p[i],stack[m-1]) <=0) m--;    Stack[m++]=p[i];    } if (n>1) m--; return m;} Vector p[2500];    Vector St[2500];int Main () {int t,n;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Double x,y,w,h,a;        int num = 0;        Double tot = 0;            for (int i=0; i<n; i++) {scanf ("%lf%lf%lf%lf%lf", &x,&y,&w,&h,&a);            Double ang =-trans (a);            Point o = Point (x, y);            p[num++] = O + Rotate (point (-W/2,-H/2), ANG);            p[num++] = O + Rotate (point (W/2,-H/2), ANG);            p[num++] = O + Rotate (point (-W/2,H/2), ANG);            p[num++] = O + Rotate (point (W/2,H/2), ANG);        Tot+=w*h;        } int m = Convexhull (p,num,st);        Double area = S (st,m);   printf ("%.1lf%%\n", Tot*100/area); } return 0;} /****144 7.5 6 3 08 11.5 6 3 09.5 6 6 3 904.5 3 4.4721 2.2361 26.565****/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 10652 Board Wrapping (computational geometry basis, convex hull)

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.