HDU 1115 Lifting the Stone:求多邊形重心

來源:互聯網
上載者:User

HDU 1115:http://acm.hdu.edu.cn/showproblem.php?pid=1115

大意:給你個n,有n個點,然後給你n個點的座標,求這n個點形成的多邊形的重心的座標。

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

struct point{    double x, y;} P[1000010];  struct line{    point a, b;} ;  double xmult(point p1, point p2, point p){    return (p1.x-p.x)*(p2.y-p.y) - (p2.x-p.x)*(p1.y-p.y);}  point intersection(line u,line v){    point ret=u.a;    double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))            /((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));    ret.x+=(u.b.x-u.a.x)*t;    ret.y+=(u.b.y-u.a.y)*t;    return ret;}  point barycenter(point a,point b,point c){    line u,v;    u.a.x=(a.x+b.x)/2;    u.a.y=(a.y+b.y)/2;    u.b=c;    v.a.x=(a.x+c.x)/2;    v.a.y=(a.y+c.y)/2;    v.b=b;    return intersection(u,v);}  ///多邊形重心point Barycenter(int n,point* p){    point ret,t;    double t1=0,t2;    int i;    ret.x=ret.y=0;    for (i=1;i<n-1;i++)        if (fabs(t2=xmult(p[0],p[i],p[i+1]))>eps){            t=barycenter(p[0],p[i],p[i+1]);            ret.x+=t.x*t2;            ret.y+=t.y*t2;            t1+=t2;        }    if (fabs(t1)>eps)        ret.x/=t1,ret.y/=t1;    return ret;}  int T;int n;  void Solve(){    scanf("%d", &T);    while(T--)    {        scanf("%d", &n);        for(int i = 0; i < n; ++i)        {            scanf("%lf%lf", &P[i].x, &P[i].y);        }        point t = Barycenter(n, &P[0]);        printf("%.2lf %.2lf\n", t.x+eps, t.y+eps);    }}  HDU 1115

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.